views:

569

answers:

3

I have an aspx page defined as follows:

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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.

A: 

Change this line:

var lbl = document.getElementById('lblDisplayDate');

to:

var lbl = document.getElementById('<%= lblDisplayDate.ClientID %>');

Also, this probably isn't doing what you want it to do anyway. Try adding:

OnClientClick="changecolor()"

to your <asp:Button /> tag.

Edit At this point I am pretty confused as to what exactly you are trying to accomplish...

If you just want to change the color of the label without posting back, change your <asp:Button /> to a normal <input type="button" /> with an onclick handler and call your script, like this:

<input type="button" ... onclick="changeColor();" />

If you want to change the color of the label while posting back, then just change the label's background in the code-behind like this:

protected void btnPostBack2_Click(object sender, EventArgs e)
{
    /* ... other stuff goes here ... */
    lblDisplayDate.BackColor = Color.Red;
    /* ... other stuff goes here ... */
}
Sean Bright
I changed it, but it is not changing the color still.
Xaisoft
See my edit above.
Sean Bright
I added the OnClientClick and it changes the color, but then the page reloads right after that and it goes back to being black.
Xaisoft
Is there a way to persit the color or is it better to just use RegisterStartUpScript if doing things in Page_Load
Xaisoft
I am expirementing with RegisterStartupScript and RegisterClientScriptBlock. If I use RegisterStartupScript, the page posts back and the color changes, but it did not with RegisterClientScriptBlock, but I was told that if I use it in a function it would work, so I am trying that out.
Xaisoft
If you want to stop the post back occuring then at the end of your changecolor() function add return false;
OneSHOT
A: 

like what Sean already stated: it is best to use OnClientClick in this case. OnClientClick would prevent a postback occuring - it is done dynamically using javascript. All u need to do is preference it to the javascript function.

I do not understand y u want to do a "RegisterStartupScript and RegisterClientScriptBlock" on colour change. These should be used when u want to add javascript from the code behind, but seeing that u have it written out already in the aspx page, its use is pointless.

waqasahmed
A: 

Check this out. I have modified your code and its working fine now without postback.

<html xmlns="http://www.w3.org/1999/xhtml"&gt;  
<head runat="server">  
    <title>Untitled Page</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" 
            OnClientClick="changecolor(); return false" />

    </div>
    </form>
  </body>
</html>

hope this helps

A Bright Worker