Click here to Skip to main content
Licence 
First Posted 21 Dec 2004
Views 39,149
Bookmarked 18 times

Emit Client-Side Script

By | 21 Dec 2004 | Article
The article describes how can we use the Page methods RegisterClientScriptBlock/RegisterStartupScript to emit client-side JavaScript to popup a window.

Introduction

A very simple method to popup a window on button click is, write the JavaScript code at the client side. Following code demonstrates this method.

<form>
    <input type="button" value="Click Me!" onClick="window.open('hello.html')">
</form>

The disadvantages of this method are:

  1. Code is visible at client side.
  2. Code and HTML are not separate. Thus maintenance is tedious.

With ASP.NET these disadvantages disappear. Let's take a look how this can be done with ASP.NET.

The popup html page

The first page that we will create is the 'hello.html' page. This is the page that will be popped up in the new window.

<html>
  <body>
   <h1>Hello, World!</h1>
  </body>
</html>

The aspx Page

Let us create an ASP.NET Web Form with a single button.

<form runat="server">
  <asp:button id="button1" onclick="button1_click" 
             runat="server" Text="Click Me!"></asp:button>
</form>

Now we will add the C# script code to handle our button click event. Off course, you can create this with the code-behind facility.

<script language="C#" runat="server">
  string buildScript(string id, string output_page)
  {
      string myScript = "\n";
   
    myScript += "<script language="'javascript'" id='" + id + "'>\n";
       myScript += "window.open('" + output_page + "');\n";
       myScript += "</" + "script>\n";
   
       return myScript;
  }
  void button1_click(object sender, EventArgs e)
  {
       string script_id = "popup";
       string myScript = buildScript(script_id, "hello.html");
       if(!IsStartupScriptRegistered(script_id))
       {
             RegisterStartupScript(script_id,myScript);
       }
  }
</script>
<form runat="server">
    <asp:button id="button1" onclick="button1_click" 
         runat="server" Text="Click Me!"></asp:button>
</form>

Let us call this page 'popupexample.aspx'.

The entire thing explained!

Our function buildScript builds the script that will be emitted to the client side. button1_click handles the button.OnClick event. The Page.RegisterStartupScript and Page.RegisterClientScriptBlock are used to emit the script to the client side. With Page.RegisterClientScriptBlock, the client-side script is emitted just after the opening tag of the Page object's <form runat="server"> element. With Page.RegisterStartupScript, the client-side script is emitted just before the closing tag of the Page object's <form runat="server"> element.

Page.IsClientScriptBlockRegistered: Call this method before calling Page.RegisterClientScriptBlock/Page.RegisterStartupScript to avoid unnecessarily assembling the client-side script. This is particularly important if the script requires a large amount of server resources to create.

References

MSDN.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jitesh Patil

Web Developer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalreal world problem PinmemberJVMFX7:30 1 Apr '05