Reputation: 2808
I have a method to get a random color:
private System.Drawing.Color GetRandColor()
{
Random r = new Random(DateTime.Now.Millisecond);
System.Drawing.Color[] colours =
{
System.Drawing.Color.Yellow,
System.Drawing.Color.LightGreen,
System.Drawing.Color.LightCyan,
System.Drawing.Color.LightSalmon,
System.Drawing.Color.LightSkyBlue
};
int i = r.Next(0, colours.Length - 1);
System.Drawing.Color c = colours[i];
return c;
}
So what I want to do, is to have this random color passed into my html when the page loads. So it will be put in Page_Load somehow:
protected void Page_Load(object sender, EventArgs e)
{
// code to set button color
}
<asp:Button ID="Button1" runat="server" Text="Button" BackColor=GetRandColor()/>
Upvotes: 1
Views: 1793
Reputation: 5818
You can have your method as common one
private void GetRandColor(object sender)
{
Random r = new Random(DateTime.Now.Millisecond);
System.Drawing.Color[] colours =
{
System.Drawing.Color.Yellow,
System.Drawing.Color.LightGreen,
System.Drawing.Color.LightCyan,
System.Drawing.Color.LightSalmon,
System.Drawing.Color.LightSkyBlue
};
int i = r.Next(0, colours.Length - 1);
System.Drawing.Color c = colours[i];
Button btn = (Button)sender;
btn.BackColor = c;
}
Upvotes: 0
Reputation: 18629
You can change the background color of button in C# code itself. Please check the method.
private void GetRandColor()
{
Random r = new Random(DateTime.Now.Millisecond);
System.Drawing.Color[] colours =
{
System.Drawing.Color.Yellow,
System.Drawing.Color.LightGreen,
System.Drawing.Color.LightCyan,
System.Drawing.Color.LightSalmon,
System.Drawing.Color.LightSkyBlue
};
int i = r.Next(0, colours.Length - 1);
System.Drawing.Color c = colours[i];
Button1.BackColor = c;
}
Upvotes: 3