asp.net 后台代码如何遍历checkbox_[Asp.Net教程]
aspx设计页面
//这个是检测按钮,检测下面的checkbox是否被选中。选中时打印其值
第一种设计:(.net 自带的)
第二种设计:(html 自带的,但含有runnat="server")
第三种设计:(html 自带的)
aspx.cs文件 (将按照对应的上面的三种设计模式去写后台代码。其他的单选按钮也类似。)
protected void Button1_Click(object sender, EventArgs e)
{
//这个是上面第一种模式
foreach (Control ct in form1.Controls)
{
if (ct.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox"))
{
CheckBox cb = (CheckBox)ct;
if (cb.Checked == true)
{
Response.Write(cb.Text);
}
}
}
//这个是上面第二种模式。(直接用request取值,会报错的。不信试试)
foreach (Control ct in form1.Controls)
{
if (ct.GetType().ToString().Equals("System.Web.UI.HtmlControls.HtmlInputCheckBox"))
{
HtmlInputCheckBox cb = (HtmlInputCheckBox)ct;
if (cb.Checked == true)
{
Response.Write(cb.Value);
}
}
}
//这个是上面第三种模式。(没有runnat="server",用request取值 最简单)
Response.Write(Request["aa"].ToString());
}