Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

call javascript function from c# serverside code 1

Status
Not open for further replies.

seanbo

Programmer
Jun 6, 2003
407
GB
how can i call a javascript function fom my c# codebehind?

____________________________________________________
If you like a post, show you care by giving it a <censored>.
 
Hi Seanbo

As the Javascript can only be executed on the client machine you cannot &quot;call&quot; it as such from your code-behind. You can however render a call to the function to the response stream and get the client to execute when the page loads. The framework provides classes for handling this in Page.RegisterClientScriptBlock.

Basically you need to build a string of clientscript (including the tags) and add this to the response something like below.
Code:
string sScript = &quot;my script block including tags&quot;;
if(!Page.IsClientScriptBlockRegistered(&quot;scriptblockidentifier&quot;)){
      Page.RegisterClientScriptBlock(&quot;scriptblockidentifier&quot;, sScript);
}

HTH

Rob




Go placidly amidst the noise and haste, and remember what peace there may be in silence - Erhmann 1927
 
but doesn't that just add sScript to my page, so that it will run on render?

i already have the script on my page. it looks like this:

<script language=&quot;javascript&quot;>
function displayPopup(text)
{
alert(text);
}
</script>

at the moment, when somebody fills out my form then presses okay, i change some text on a label to say thankyou. i would rather call this function to say thankyou in a pop up bax.

____________________________________________________
If you like a post, show you care by giving it a <censored>.
 
sorry, i get it now.

____________________________________________________
If you like a post, show you care by giving it a <censored>.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top