Why is the label not changing color using RegisterClientScriptBlock?

Posted by Xaisoft on Stack Overflow See other posts from Stack Overflow or by Xaisoft
Published on 2009-03-20T16:28:20Z Indexed on 2010/04/20 5:43 UTC
Read the original article Hit count: 306

Filed under:
|
|

I have an aspx page defined as follows:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

   <script type="text/javascript" language='javascript'>
       function changecolor() {
           var lbl = document.getElementById('lblDisplayDate');
           lbl.style.color = 'red';
       };
  </script>

</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:Label ID="lblDisplayDate" runat="server" 
               Text="Label"></asp:Label><br />

    <asp:Button ID="btnPostBack2" runat="server" 
                Text="Register Client Block Script" 
                onclick="btnPostBack2_Click" />

</div>
</form>

Here is the btnPostBack2 Click event:

protected void btnPostBack2_Click(object sender, EventArgs e)
{
    if (!ClientScript.IsClientScriptBlockRegistered("JSScriptBlock"))
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), 
        "JSScriptBlock", 
        "changecolor();",
        true);
    } 
}

Even though, I put the script in a function to change the color, it is still not doing it and why do I need to add the script tags to true if the function is already enclosed in script tags?

lblDisplayDate is in the Page Load where it is set to the current time on every page reload.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about ASP.NET