0

I have this code that wants to change the background color of the webpage after clicking a button. I have no CSS for this webpage is just an empty asp.net page

<asp:Button ID="btn_Change" runat="server" Text="Change"  OnClientClick="changeBackground()" />
        <script language="JavaScript">
            function changeBackground()
            {

                document.body.style.backgroundColor = "#eeeeee";
                document.getElementById('<%=txtBox3.ClientID %>').style.backgroundColor = "#00FF00";

            }
        </script>

now the problem is the change only occurs on the click and not staying after the click. How do I make the change stay permanently?

2
  • Do you mean that after you refresh the page the change does not stay? Commented Apr 21, 2014 at 19:44
  • Assign the color you want to a variable, and have it change whenever the page is painted out, rather than just a button click. Commented Apr 21, 2014 at 19:45

3 Answers 3

1

Store the fact that the change has been made somewhere persistent (such as a cookie, local storage or (having made an Ajaxy HTTP request) the server).

On page load, test the place you are storing the data to see if the fact has been stored there. If it has, call the function that makes the change.

Sign up to request clarification or add additional context in comments.

Comments

1

The easy way would be to return false, I think its posting back after the click, causing refresh...

OnClientClick="changeBackground();return false;"

or

OnClientClick="return changeBackground()"

and modify JS adding return false; after last line

document.body.style.backgroundColor = "#eeeeee";
document.getElementById('<%=txtBox3.ClientID %>').style.backgroundColor = "#00FF00";
return false;

2 Comments

yes it worked thanks. but does return false stops the postback? because I want to minimize the server workload
In this situation, yes, it stops further action from the server control.In jQuery it be like function(e) { e.preventDefault(); }
0

In this scenario you don't really have to use asp:Button if you are not doing anything on the server side. You should use a normal html button so that you can submit your form later:

<input type="button" ID="btn_Change" value="Change"  onclick="changeBackground()" />
            <script language="JavaScript">
                function changeBackground()
                {

                    document.body.style.backgroundColor = "#eeeeee";
                    document.getElementById('btn_Change').style.backgroundColor = "#00FF00";

                }
            </script>

Or you can try returning false, this will avoid the post_back. But that is unnecessary:

 <asp:Button ID="btn_Change" runat="server" Text="Change"  OnClientClick="changeBackground()" />
            <script language="JavaScript">
                function changeBackground()
                {

                    document.body.style.backgroundColor = "#eeeeee";
                    document.getElementById('<%=txtBox3.ClientID %>').style.backgroundColor = "#00FF00";
return false;

                }
            </script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.