Click here to Skip to main content
12,755,975 members (37,216 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 21 Dec 2004

Emit Client-Side Script

, 21 Dec 2004
Rate this:
Please Sign up or sign in to vote.
The article describes how can we use the Page methods RegisterClientScriptBlock/RegisterStartupScript to emit client-side JavaScript to popup a window.


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.

    <input type="button" value="Click Me!" onClick="'hello.html')">

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.

   <h1>Hello, World!</h1>

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>

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 += "'" + 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");
<form runat="server">
    <asp:button id="button1" onclick="button1_click" 
         runat="server" Text="Click Me!"></asp:button>

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.




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
No Biography provided

You may also be interested in...

Comments and Discussions

Generalreal world problem Pin
JVMFX1-Apr-05 8:30
memberJVMFX1-Apr-05 8:30 
QuestionHuh?? Pin
ConradC23-Dec-04 15:28
memberConradC23-Dec-04 15:28 
AnswerRe: Huh?? Pin
Jitesh Patil23-Dec-04 17:18
memberJitesh Patil23-Dec-04 17:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170217.1 | Last Updated 22 Dec 2004
Article Copyright 2004 by Jitesh Patil
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid