Page 1 of 1

ASP.Net - Calling JavaScript from Code Behind

#1 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Posted 11 August 2010 - 03:50 PM

.Net gives us the ability to call javascript code from the code behind. This means that you don’t have to write the javascript code in the “Source” of the aspx page.

Just for an example, let’s say that you have a button on a form that just want to popup an alert that says “HEY” when it is clicked.

protected void btnHey_Click(object sender, EventArgs e)
{
     StringBuilder sb = new StringBuilder();
     sb.Append("<script language='javascript'>alert('HEY');</script>");

     // if the script is not already registered
     if (!Page.ClientScript.IsClientScriptBlockRegistered(Page.GetType(), "HeyPopup"))
          ClientScript.RegisterClientScriptBlock(Page.GetType(), "HeyPopup", sb.ToString());
}



Let's say that there is already a javascript method in the ASPX page. To run that method, you would use similar code, but with one difference:

// javascript method in ASPX page
<script language="javascript" type="text/javascript">
    function ShowMessage(myMessage){
        alert(myMessage);
    }
</script>

// C# code
protected void btnHey_Click(object sender, EventArgs e)
{
   StringBuilder sb = new StringBuilder();
   sb.Append("ShowMessage('hey');");

   // if the script is not already registered
   if (!Page.ClientScript.IsClientScriptBlockRegistered(Page.GetType(), "HeyPopup"))
       // notice that I added the boolean value as the last parameter
       ClientScript.RegisterClientScriptBlock(Page.GetType(), "HeyPopup", sb.ToString(), true);



Is This A Good Question/Topic? 2
  • +

Replies To: ASP.Net - Calling JavaScript from Code Behind

#2 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Posted 12 August 2010 - 06:01 AM

Hey eclipsed4utoo,

Just a question, is C#/VB script not server side script and Java Script is client side script?

As far as i know that is the case, so if it is, why make a round trip to the server just to call client side script again?

Would it not be faster to just include it in the Page-Behind (i mean executing).

It seems like you call the Button Click event, to inject code into the page, 1 extra step...

Correct me if I am wrong...
Was This Post Helpful? 0
  • +
  • -

#3 Guest_benkoch*


Reputation:

Posted 16 August 2010 - 02:56 AM

Hi

I modified your code to read thus. But no alert is displayed for the 'Second' string.
What modification u need to display the second string?

protected void Button1_click(...){
ShowError("First"); // displayed
ShowError("Second); // NOT displayed
}

protected void ShowError(String mseg)
{
StringBuilder sb = new StringBuilder();
sb.Append("ShowMessage('" + mseg + "');");

// if the script is not already registered

if (!Page.ClientScript.IsClientScriptBlockRegistered(Page.GetType(), "HeyPopup"))
// notice that I added the boolean value as the last parameter
ClientScript.RegisterClientScriptBlock(Page.GetType(), "HeyPopup", sb.ToString(), true);
}



thanks
Was This Post Helpful? 0

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Posted 18 August 2010 - 06:44 AM

View Postmotcom, on 12 August 2010 - 09:01 AM, said:

Hey eclipsed4utoo,

Just a question, is C#/VB script not server side script and Java Script is client side script?

As far as i know that is the case, so if it is, why make a round trip to the server just to call client side script again?

Would it not be faster to just include it in the Page-Behind (i mean executing).

It seems like you call the Button Click event, to inject code into the page, 1 extra step...

Correct me if I am wrong...


Yes, Javascript is client-side. However, there maybe times where you need to do server code, then run some type of javascript. This is how you would do it.

It certainly doesn't mean that you should use this code for displaying a simple message box. I used that since it's a very simply example.

View Postbenkoch, on 16 August 2010 - 05:56 AM, said:

Hi

I modified your code to read thus. But no alert is displayed for the 'Second' string.
What modification u need to display the second string?

protected void Button1_click(...){
ShowError("First"); // displayed
ShowError("Second); // NOT displayed
}

protected void ShowError(String mseg)
{
StringBuilder sb = new StringBuilder();
sb.Append("ShowMessage('" + mseg + "');");

// if the script is not already registered

if (!Page.ClientScript.IsClientScriptBlockRegistered(Page.GetType(), "HeyPopup"))
// notice that I added the boolean value as the last parameter
ClientScript.RegisterClientScriptBlock(Page.GetType(), "HeyPopup", sb.ToString(), true);
}



thanks


Remember how the internet works. Your browser posts a request to the server, the server does work, then sends back HTML for the browser to display. These messages don't get sent back to the browser until after the postback has completed. It does NOT send them at the time when the code is executed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1