.net 遍历checkbox 怎么遍历不到?

来源:百度知道 编辑:UC知道 时间:2024/09/21 09:13:07
<html>
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox ID="CheckBox1" Text="1" runat="server" />
<asp:CheckBox ID="CheckBox2" Text="2" runat="server" />
<asp:CheckBox ID="CheckBox3" Text="3" runat="server" />
<asp:CheckBox ID="CheckBox4" Text="4" runat="server" />
<asp:CheckBox ID="CheckBox5" Text="5" runat="server" />
<asp:CheckBox ID="CheckBox6" Text="6" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>

也可以用FindControl。

你这个找不到。是不是出错了呢?你在catch里面把错误输出来看看呢?

改成这个
foreach (System.Web.UI.Control control in this.Controls[1].Controls)
注意:this.Controls[1].Controls

foreach (ListItem PowerList in ((CheckBoxList)sender).Items)
{
if(PowerList.Selected)
{
PowerSql = PowerSql + PowerList.Value + @"','";
}
}

foreach (Control ctl in form1.Controls)
{
if (ctl is CheckBox)
Response.Write(ctl.ClientID.ToString() + "<br>");
}

Button1_Click写上以上代码就可以了,当前页要有form1,form1为服务器控件。
你要获取的是form1这里的控件不是整个页面的控件。

首先你要搞清楚控件的层次关系page-form-control

你搜出来的控件应该是Form,所以你找不到
应该为foreach (Control ctrl in this.form.Controls)
我也没写过,你试试吧。