Visual Studio .NET - Call Javascript function from C# code behind

Asked By srilatha janakaraj on 27-Feb-06 06:31 PM

This is how my table looks like

<TR id="row1" style="display:none">
   <td>lblName1, lblID1, lblStatus1</td>
   <td> File1</td>

<TR id="row2" style="display:none">
   <td>lblName2, lblID2, lblStatus2</td>
   <td> File2</td>


I have  a javascript function that will make  a specific # of my table rows from invisible to visible.

function show_rows($num) {
	//Loop through rows until $num...set display style accordingly
	for (var $i=1; $i<=$num; $i++) {
		//Change the style to make visible
		document.getElementById('row'+$i).style.display = "";

My C# code behind has a function called "Build_BulkUpload" which will receive the number of users chosen for BUlk Upload.

Depending on that number, I want to turn certain # of table rows  from invisible to visible.

So how can I call the "show_rows" javascript function from my C# function "Build_BulkUpload" ??

You have to emit client script into the page - Asked By Peter Bromberg on 27-Feb-06 06:33 PM



Here's an example - Asked By Ramya T on 27-Feb-06 06:36 PM

Great example - Asked By srilatha janakaraj on 27-Feb-06 07:05 PM

Thanks.. I will give this a try.
Question - Asked By srilatha janakaraj on 27-Feb-06 07:31 PM
In the sample code, to call a javascript function from C# code behind,
its like

private void Page_Load(object sender, System.EventArgs e)
    btnOK.Attributes.Add("onclick","return ReqField1Validator()");

In my case, I want to call the java script function from the "Build_BulkUpload" function in my C# code behind..

I dont want to bind it with a button's click event, because this java script must be called in the page build function and not when the button is clicked...

is this possible?
You can use the RegisterClientScriptBlock - Asked By Venkat K on 27-Feb-06 10:10 PM
method to get the function onto the page.

Try this writing in the page load event
Page.RegisterClientScriptBlock("MyScript", _
            "<script language=javascript>" & _
            "if (document.images) {" & _
            "MyButton = new Image;" & _
            "MyButtonShaded = new Image;" & _
            "MyButton.src = 'button1.jpg';" & _
            "MyButtonShaded.src = 'button2.jpg';" & _
            "}" & _
            "else {" & _
            "MyButton = '';" & _
            "MyButtonShaded = '';" & _
            "}" & _

If you want more information see this linkl
Call Java script from a function and not a button - Asked By srilatha janakaraj on 28-Feb-06 01:20 AM
Most of these examples in the replies talk about calling a javascript function from a button's event.

<input type="submit" name="Button1" value="Button" 
             id="Button1" onclick="AlertHello()" />

I dont want that. Instead I want to call the JS function from another function.

public void Build_BulkUpload(int n)
   //display n number of rows - call a JS function 

Is it posible to call a JS function from a C# function and not a button's event?
It theroetically seems impossible - Asked By Pankaj Sharma on 28-Feb-06 05:29 AM
It is not possible to call client side function from within a code behind function.

Code behind functions are compiled and dumped in an assembly in Bin folder of an application. When ASP.NET processes a web form, code behind functions' assembly is linked and functions are called dynamically. That's why there specified various code behind attributes in @page directive of a web form.

i think it is not feasible to directly call a js function from within a C# function.
hi - shailesh joge replied to Peter Bromberg on 15-Sep-08 01:22 PM

hi,how can we obtain the javascript variable text in to code,can u pls help me ...i want syntax how to call following nodetext in C#.NEt code...

my code is like...

function GetSelectedNodes()


var tree = <%= RadTree1.ClientID%>;

var selectedNodes = tree.GetSelectedNodes();

var nodetext = "";

var temp="";

if (selectedNodes != 0)


for (var i=0; i<selectedNodes.length; i++)


nodetext = nodetext + selectedNodes[i].Text + "; ";



documnent.getElementById('hdnX').Value= nodetext ;

return (nodetext);



RS Emenu replied to shailesh joge on 10-Sep-10 06:27 AM
You can ClientScriptManager clss method RegisterClientScriptBlock or RegisterStartupScript method to invoke JavaScript Method.

Refer to below links for more details about the same.