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

Asked By srilatha janakaraj
27-Feb-06 06:31 PM
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  You have to emit client script into the page

27-Feb-06 06:33 PM
Example:

Response.Write("<script>show_rows();</script>");

Here's an example  Here's an example

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

Great example  Great example

27-Feb-06 07:05 PM
Thanks.. I will give this a try.
Question  Question
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  You can use the RegisterClientScriptBlock
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  Call Java script from a function and not a button
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  It theroetically seems impossible
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  hi
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);

}

}

 

 

  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
help
Visual Studio .net .NET Framework Hi NG, ich habe vor längerer Zeit mit Visual Studio .Net 2003 gearbeitet und überlege momentan auf einen neueren Stand upzudaten. Ein Visual Studio .Net 2008 scheint es nicht zu geben. Habe zumindest mit googeln nichts gefunden. Was
Wise for Visual Studio.NET Wise for Visual Studio.NET By Peter A. Bromberg, Ph.D. To "Print This Page" Link Peter Bromberg Wise for Visual Studio .NET is a total and complete installation development system for creating and editing Windows® Installer
Visual Studio versioning . . . . how to tell? .NET Framework To my knowledge, Visual studio 6 was released in 1998, then Visual Studio .NET 2002 is VS 7, then Visual Studio .NET 2003 is VS 7.1, then Visual
visual studio.net 2003 and Access 2007 database .NET Framework Hi I am currently using Visual Studio.Net 2003 running on Windows Server 2000 operating system. I have used Visual Studio.net 2003 connecting to Access 2002 databases in the pass with great success. Now
Is Visual Studio self-hosting ? .NET Framework Does Microsoft use Visual Studio IDE, Visual Studio Debugger, Visual Studio Linker and Visual Studio compiler for developing Visual Studio ? Or is Visual Studio not