我在ASP.NET页面中有12个按钮。我想给按钮上色;但我的代码不起作用。我将检查断点但是button.BackColor = Color.DarkGreen;代码未执行。
ASP设计编码 -
<form id="form1" runat="server" style="background-color:Silver; height:100%; min-height:300px">
<div style="height:100%;">
<div style="height:20%;">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
<div id="content" runat="server" class="content" style="height:70%; min-height:180px">
<div >
<asp:Button ID="Button1" runat="server" Text="Button" style="width:32%"
Enabled="False" />
<asp:Button ID="Button2" runat="server" Text="Button" style="width:32%"
Enabled="False"/>
<asp:Button ID="btnUp" runat="server" Text="UP" onclick="btnUp_Click" style="width:32%" /> <br /><br />
<asp:Button ID="Button3" runat="server" Text="Button" style="width:32%"
Enabled="False" />
<asp:Button ID="Button4" runat="server" Text="Button" style="width:32%"
Enabled="False" />
<asp:Button ID="Button5" runat="server" Text="Button" style="width:32%"
Enabled="False" /> <br /><br />
<asp:Button ID="Button6" runat="server" Text="Button" style="width:32%"
Enabled="False" />
<asp:Button ID="Button7" runat="server" Text="Button" style="width:32%"
Enabled="False" />
<asp:Button ID="Button8" runat="server" Text="Button" style="width:32%"
Enabled="False" /> <br /><br />
<asp:Button ID="Button9" runat="server" Text="Button" style="width:32%"
Enabled="False" />
<asp:Button ID="Button10" runat="server" Text="Button" style="width:32%"
Enabled="False" />
<asp:Button ID="btnDown" runat="server" Text="DOWN" onclick="btnDown_Click" style="width:32%" /> <br /><br />
</div><!-- /grid-b -->
</div>
<div id="footer" class="footer" style="height:10%;">
<a href="OutLet.aspx" >Home</a>
<a href="Selection.aspx">Selection</a>
</div>
</div>
</form>
* C#编码 - *
var buttons = this.form1.Controls.OfType<Button>();
foreach (var button in buttons)
{
button.BackColor = Color.DarkGreen;
}
答案 0 :(得分:3)
用ASP.NET面板替换你的一个div(它被渲染为div但可以从代码隐藏中引用):
<asp:Panel id="ButtonPanel" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" style="width:32%"
Enabled="False" />
<asp:Button ID="Button2" runat="server" Text="Button" style="width:32%"
Enabled="False"/>
<asp:Button ID="btnUp" runat="server" Text="UP" onclick="btnUp_Click" style="width:32%" /> <br /><br />
<asp:Button ID="Button3" runat="server" Text="Button" style="width:32%"
Enabled="False" />
<asp:Button ID="Button4" runat="server" Text="Button" style="width:32%"
Enabled="False" />
<asp:Button ID="Button5" runat="server" Text="Button" style="width:32%"
Enabled="False" /> <br /><br />
<asp:Button ID="Button6" runat="server" Text="Button" style="width:32%"
Enabled="False" />
<asp:Button ID="Button7" runat="server" Text="Button" style="width:32%"
Enabled="False" />
<asp:Button ID="Button8" runat="server" Text="Button" style="width:32%"
Enabled="False" /> <br /><br />
<asp:Button ID="Button9" runat="server" Text="Button" style="width:32%"
Enabled="False" />
<asp:Button ID="Button10" runat="server" Text="Button" style="width:32%"
Enabled="False" />
<asp:Button ID="btnDown" runat="server" Text="DOWN" onclick="btnDown_Click" style="width:32%" /> <br /><br />
</asp:Panel>
然后,您可以循环此面板中的所有控件,按下每个按钮并执行您需要执行的操作:
foreach(Control c in ButtonPanel.Controls){
if(c.GetType()==typeof(Button)){
Button btn = (Button)c;
btn.BackColor = Color.DarkGreen;
}
}
@Abbas提到的编辑,如果至少使用.NET framework 3.5,您可以使用Enumerable.OfType method:
foreach(Button btn in ButtonPanel.Controls.OfType<Button>()) {
btn.BackColor = System.Drawing.Color.DarkGreen;
}
答案 1 :(得分:1)
要回答你的问题,这里的问题是Controls属性只会给你form1的直接子控件。要获取按钮,请使用:
var buttons = this.content.Controls.OfType<Button>();
但是我也同意Jayanga,CSS会做得更好。
答案 2 :(得分:0)
控件集合仅包含直接子项。我的猜测是var buttons
中没有任何项目。
foreach( Control c in this.[immediate parent ID].Controls )
{
Button b = c as Button;
if( b != null )
{
b.BackColor = Color.DarkGreen;
}
}
正如@ Jayanga的评论指出的那样,您可以(并且应该)使用页面中的CSS设置按钮的样式。
答案 3 :(得分:0)
除了需要专门使用代码隐藏来设置按钮的背景颜色之外,我建议您使用javascript来实现这一点,因为它非常快。 Eample here