Using RegisterStartupScript() Methods of ClientScriptManager Class
Read my previous code snippet Using RegisterClientScriptBlock() Methods of ClientScriptManager Class
Moving forward, we will see the usage of ClientScriptManager.RegisterStartupScript() method in ClientScriptManager class.
What is the difference between RegisterStartupScript() method and RegisterClientScriptBlock() method?
Both, RegisterStartupScript() method and RegisterClientScriptBlock() method will inject Javascript 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 will show a part html output given by the asp.net page when executing these RegisterClientScriptBlock and RegisterStartupScript methods.
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, we can see the script embedded before the page controls, but after form open tag.
RegisterStartupScript Output
<script type="text/javascript">
<!--
alert(document.getElementById('txtName').value)// -->
</script>
</form>
</body>
In the above html, we can see the script injected after the page controls but before form close tag, thus making the script able to access the page controls as I said earlier.
Implementation
There are 2 overloads for this method.
ClientScriptManager.RegisterStartupScript(Type typeofscript, String key,
String script)
ClientScriptManager.RegisterStartupScript(Type typeofscript, String key,
String script, Boolean addScriptTags)
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.
1st overload
ClientScriptManager script = Page.ClientScript;
txtMsg.Text = "Welcome to CodeDigest.Com!!!";
if (!script.IsStartupScriptRegistered (this.GetType(), "Alert"))
{
script.RegisterStartupScript (this.GetType(), "Alert",
"<script type=text/javascript>alert(document.getElementById('txtMsg').value)</script>");
}
2nd overload
ClientScriptManager script = Page.ClientScript;
txtMsg.Text = "Welcome to CodeDigest.Com!!!";
if (!script.IsStartupScriptRegistered (this.GetType(), "Alert"))
{
script.RegisterStartupScript (this.GetType(), "Alert",
"alert(document.getElementById('txtMsg').value)",true);
}
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. Also, we can visually see the page controls in the page when the alert box is popped up as opposed to RegisterClientScriptBlock methods.
|