In this section, we will see the usage
ClientScriptManager.RegisterStartupScript() method of ClientScriptManager
class. Both, RegisterStartupScript() method and RegisterClientScriptBlock()
method will inject Jscript code that will fire during start up of subsequent
postback. But the real difference is the former methods will inject the script
after the form open tag but before page controls and the
RegisterStartupScript() methods will inject the scripts after page controls but
before form close tag. This indicates that injecting script using RegisterClientScriptBlock()
method, it is not possible to access page controls in the script block.
However, using RegisterStartupScript() method we can.
The markups below (Listing 4 and 5) will show a part html
output given by the asp.net page when executing these RegisterClientScriptBlock
and RegisterStartupScript methods.
Listing 4 - RegisterClientScriptBlock Output
<form name="form1" method="post" action="Default.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwUJMjgzMDgzOTgzZGQfI8LfDKmcT0TXZj8jwrxqI6TOIA==" />
</div>
<script type=text/javascript>alert('hi')</script>
In the above HTML snippet, we can see the script embedded
before the page controls, but after form open tag.
Listing 5 - RegisterStartupScript Output
<script type="text/javascript">
<!--
alert(document.getElementById('txtName').value)// -->
</script>
</form>
</body>
In the above html snippet (Listing 5), we can see the script
embedded after the page controls, but before form close tag, thus making the
script able to access the page controls as I said earlier.
Overloads
There are 2 overloads for this method.
Listing 6 - RegisterClientScriptBlock Overloads
ClientScriptManager.RegisterClientScriptBlock (Type typeofscript, String key,
String script)
ClientScriptManager.RegisterClientScriptBlock (Type typeofscript, String key,
String script, Boolean addScriptTags)
Usage
Placing this code on page load or a button click makes the
script fire on the start up of subsequent postback. This method also has a
method called IsStartupScriptRegistered(), like RegisterClientScriptBlock()
methods, which will check for script duplications. Refer to the code below for
the implementation of the RegisterClientScriptBlock() method.
Listing 7 - 1st overload
ClientScriptManager script = Page.ClientScript;
txtName.Text = "Welcome to AspAlliance!!!";
if (!script.IsStartupScriptRegistered (this.GetType(), "Alert"))
{
script.RegisterStartupScript (this.GetType(), "Alert",
"<script type=text/javascript>alert(document.getElementById('txtName').value)</script>");
}
Listing 8 - 2nd overload
ClientScriptManager script = Page.ClientScript;
txtName.Text = "Welcome to AspAlliance!!!";
if (!script.IsStartupScriptRegistered (this.GetType(), "Alert"))
{
script.RegisterStartupScript (this.GetType(), "Alert",
"alert(document.getElementById('txtName').value)",true);
}
When the above code is executed we will get an output
similar to "Figure 2 - RegisterStartupScript Output."
Figure 2 - RegisterStartupScript Output
Here, the script block will get executed after the controls
in the page are rendered and the controls in the page will be visible to the
script as opposed to RegisterClientScriptBlock() method, refer to the above
figure. Thus, we can access the page controls from the script block when using
RegisterStartupScript() method.