.NET Framework Bookmark and Share   
 index > JScript for the .NET Framework > Calling JS function within C#
 

Calling JS function within C#

Hello all

I have a quick question for you.

I have got a .js file in my visual studio which contains soe functions.I am wondering if it is possible to trigger a function

in C# when it required.

thanks,

Hamed.

Hamedn
Consider you have the following function in JS file:

functiongetAlert()
{
alert('hi');
}

define the script file before the html tag of the aspx by adding the following line:

<scriptsrc="FILENAME"type="text/javascript"></script>

at your code behind (for example page load), add the following line

Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"callScript",
"getAlert();",true);

You will find that on page load the function getAlert() is called. You can do the same in any method in the code behind.


Khaled Moawad --- http://kholyos.blogspot.com
Khaled Moawad
Consider you have the following function in JS file:

functiongetAlert()
{
alert('hi');
}

define the script file before the html tag of the aspx by adding the following line:

<scriptsrc="FILENAME"type="text/javascript"></script>

at your code behind (for example page load), add the following line

Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"callScript",
"getAlert();",true);

You will find that on page load the function getAlert() is called. You can do the same in any method in the code behind.


Khaled Moawad --- http://kholyos.blogspot.com
Khaled Moawad
Hi,

C# code :

Page.RegisterStartupScript("Script","<script language='javascript'>functiongetAlert(){alert('HI')};</script>")

Regards,
ANKIT


ANKIT BHATNAGAR
ANKIT BHATNAGAR

You can use google to search for other answers

Custom Search

More Threads

• Is there a scripting engine component?
• urg - redirect to logout screen
• window.open automatically converts "%26" to "&". Need to some way to stop this.
• GridView - OnClientClick
• Numbering records in a Grid
• onClientClick
• How to send multiple simultaneous callbacks with .NET script callback model?
• passing parameter from popup to parent
• Submitting a parent form from a child window in IE 7
• Can't find dynamic div on Javascript