• Now Online : 55
  • admin@codemyne.net

Introduction

Creating client script in server code is useful when the contents of the client script depend on information that is not available until run time. Add client script dynamically to web pages when the page has finshed loading or when user want to submit the page.

The following methods are used to add client script dynamically

  1. RegisterClientScriptBlock
  2. RegisterStartupScript
  3. RegisterClientScriptInclude
  4. RegisterOnSubmitStatement

RegisterClientScriptBlock

The RegisterClientScriptBlock method places the JavaScript directly after the opening < form> element in the page. That is this method adds a script block to the top of the page

Note that the script might be rendered into the page before all the elements are finished; therefore, you might not be able to reference all the elements on the page from the script.

To determine whether the client script is registered with the System.Web.UI.Page object using the specified key and type or not, IsClientScriptBlockRegistered() method is used.

An example for this method is given below. Copy and Paste the given code into Page_Load event.

// Define the name and type of the client scripts on the page.
String csname1 = "PopupScript";       
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the client script is already registered.
if (!cs.IsClientScriptBlockRegistered(cstype, csname1))
{
    String cstext1 = "alert('Welcome to Codemyne');";
    cs.RegisterClientScriptBlock(cstype, csname1, cstext1, true);
}

RegisterStartupScript

The RegisterStartupScript method places the JavaScript at the bottom of the ASP.NET page right before the closing < /form> element.That is this method adds a script block into the page that executes when the page finishes loading but before the page's onload event is raised.

The script is typically not created as an event handler or function; it generally includes only the statements you want to execute once.

Call the IsStartupScriptRegistered method to determine whether a startup script with a given key and type pair is already registered and avoid unnecessarily attempting to add the script.

The script blocks are not guaranteed to be output in the order they are registered. If the order of the script blocks is important, use a StringBuilder object to gather the scripts together in a single string, and then register them all in a single client script block.

An example for this method is given below.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ClientScriptManager Example - www.codemyne.net</title>
<script runat="server" >
protected void Page_Load(object sender, EventArgs e)
{
    // Define the name and type of the startup scripts on the page.
    Type cstype = this.GetType();
            
    StringBuilder cstext2 = new StringBuilder();
    string csname2 = "FocusTextBox";
    cstext2.Append("form1.TextBox1.focus()");
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;
    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname2))
    {
        cs.RegisterStartupScript(this.GetType(), "FocusTextBox", cstext2.ToString(), true);
    }
}
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>

Note: The above two methods are overloaded with a Boolean value indicating whether to add script tags. If it is true, the script tags will be added automatically.

In the next article we will see the remaining two methods.

Comments/Suggestions are invited. Happy coding......!

Comments Post a Comment