To change color with a push-button

M

Mirco

I must make a aspx page that make these single steps:
1. when I open it is green
2. when I click a button it is blue
3. when I click the same button it is red

alternate 2 and 3

I have written this code but it does not work. Which thing I am mistaking?

Code:
<%@ Page Language="C#"%>

<script runat="server">

bool T=false;
int a=1;

protected void Page_Load(Object Src, EventArgs E)
{
Sfondo.Attributes.Add("bgcolor", "#00ff00");
}

void Cambia_Sfondo(Object Src, EventArgs E)
{
T=!T;
if (T)
{
Sfondo.Attributes.Add("bgcolor", "#0000ff");
}
else
{
Sfondo.Attributes.Add("bgcolor", "#ff0000");
}
a++;
}

</script>

<html>
<body id="Sfondo" runat="server">
<form runat="server">
<asp:Button OnClick="Cambia_Sfondo" Text="Cambia Sfondo" Height="20"
Width="80" runat="server"/>
<br><%Response.Write("A= " + a);%>
</form>
</body>
</html>
 
G

garethdjames

Firstly the boolean logic won't work, secondly you need to store the
value of i in either viewstate or the session each time, try this code

<script runat="server">

protected void Page_Load(Object Src, EventArgs E)
{
if(!Page.isPostback)
{
Sfondo.Attributes.Add("bgcolor", "#00ff00");
int i=0;
ViewState["num"]=i;
}
}

void Cambia_Sfondo(Object Src, EventArgs E)
{
int i = (int)ViewState["num"];
if (i==0)
{
Sfondo.Attributes.Add("bgcolor", "#0000ff");
i++;
}
else
{
Sfondo.Attributes.Add("bgcolor", "#ff0000");
i = 0;
}
ViewState["num] = i;
}

</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top