我有一种获得随机颜色的方法:
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;
}
所以我想要做的是在页面加载时将这种随机颜色传递到我的 html 中。所以它会以某种方式放在 Page_Load 中:
protected void Page_Load(object sender, EventArgs e)
{
// code to set button color
}
<asp:Button ID="Button1" runat="server" Text="Button" BackColor=GetRandColor()/>