148
Is the only difference between the RegisterStartupScript and the RegisterClientScriptBlock is that RegisterStartupScript puts the javascript before the closing </form> tag of the page and RegisterClientScriptBlock puts it right after the starting <form> tag of the page?
Also, when would you choose one over the other? I wrote up a quick sample page where I had an issue and I am not sure the exact reason of why it is happening.
Here is the aspx markup:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblDisplayDate" runat="server"
Text="Label" /><br />
<asp:Button ID="btnPostback" runat="server"
Text="Register Startup Script"
onclick="btnPostback_Click" /><br />
<asp:Button ID="btnPostBack2" runat="server"
Text="Register"
onclick="btnPostBack2_Click" />
</div>
</form>
</body>
</html>
Here is the Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
lblDisplayDate.Text = DateTime.Now.ToString("T");
}
protected void btnPostback_Click(object sender, EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(@"<script language='javascript'>");
sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");
sb.Append(@"lbl.style.color='red';");
sb.Append(@"</script>");
if(!ClientScript.IsStartupScriptRegistered("JSScript"))
{
ClientScript.Register