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.
Change this line:
to:
Also, this probably isn’t doing what you want it to do anyway. Try adding:
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: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: