Call Javascript function from C# code behind

Asked By srilatha janakaraj
27-Feb-06 06:31 PM
Earn up to 0 extra points for answering this tough question.
Hi, This is how my table looks like <table> <TR id="row1" style="display:none"> <td>lblName1, lblID1, lblStatus1</td> <td> File1</td> </tr> <TR id="row2" style="display:none"> <td>lblName2, lblID2, lblStatus2</td> <td> File2</td> </tr> ... </table> 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 = ""; } } </script> 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
27-Feb-06 06:33 PM
Example: Response.Write("<script>show_rows();</script>");

  Here's an example

Asked By Ramya T
27-Feb-06 06:36 PM
http://www.codeproject.com/aspnet/ClientServer.asp

  Great example

Asked By srilatha janakaraj
27-Feb-06 07:05 PM
Thanks.. I will give this a try.
  Question
Asked By srilatha janakaraj
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
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 = '';" & _ "}" & _ "</script>") If you want more information see this linkl http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspnet-usingjavascript.asp
  Call Java script from a function and not a button
Asked By srilatha janakaraj
28-Feb-06 01:20 AM
Most of these examples in the replies talk about calling a javascript function from a button's event. like [CODE] <input type="submit" name="Button1" value="Button" id="Button1" onclick="AlertHello()" /> [/CODE] 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
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
15-Sep-08 01:22 PM

hi,how can we obtain the javascript variable text in to asp.net 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 + "; ";

}

alert(nodetext);

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

return (nodetext);

}

}

 

 

  re: hi
RS Emenu replied to shailesh joge
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.

http://www.a2zmenu.com/csharp/Calling%20JavaScript%20function%20from%20CSharp.aspx
Create New Account