using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ZXH_QZL_Data;
using System.Configuration ;
namespace ZXH_QZL
{
/// <summary>
/// customize_server 的摘要说明。
/// </summary>
public class customize_server : System.Web.UI.Page
{
protected System.Web.UI.WebControls.CheckBox CheckBox1;
protected System.Web.UI.WebControls.CheckBox CheckBox2;
protected System.Web.UI.WebControls.CheckBox CheckBox3;
protected System.Web.UI.WebControls.CheckBox CheckBox4;
protected System.Web.UI.WebControls.CheckBox CheckBox5;
protected System.Web.UI.WebControls.CheckBox CheckBox6;
protected System.Web.UI.WebControls.CheckBox CheckBox7;
protected System.Web.UI.WebControls.CheckBox CheckBox8;
protected System.Web.UI.WebControls.CheckBox CheckBox9;
protected System.Web.UI.WebControls.CheckBox CheckBox10;
protected System.Web.UI.WebControls.CheckBox CheckBox11;
protected System.Web.UI.WebControls.CheckBox CheckBox12;
protected System.Web.UI.WebControls.CheckBox CheckBox13;
protected System.Web.UI.WebControls.CheckBox CheckBox14;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlTable Table1;
protected System.Web.UI.WebControls.Label Label1;
protected Interface_sendMess sendmess = new ZXH_QZL_Data.sendMessage();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
int count = 0;
foreach(Control c in this.Controls)
{
if(c.GetType()==Type.GetType("CheckBox"))
{
CheckBox cb = (CheckBox)c;
string id = cb.ID;
if(cb.Checked)
count++;
}
}
}
}
}
方法1 :
ArrayList alTemp=new ArrayList();
foreach(DataGridItem dgi in dg.Items)
{
foreach(Control ctl in dgi.Cells[0].Controls)
{
CheckBox cb=ctl as CheckBox;
if(cb!=null)
{
if(cb.Checked)
alTemp.Add(int.Parse(dgi.Cells[1].Text));
}
}
}
return alTemp;
}
存在问题是表单控件中存在容器控件,在容器控件中的checkbox需要使用递归来进行查找
修改后:
void findControl(ref Control cc){
foreach(Control c in this.Controls)
{
if(c is CheckBox)
{
CheckBox cb = (CheckBox)c;
}
//表示为容器控件
if(c.HasControls()){
findControl(ref c)
}
}
}
//调用findControl
Control form =this.Controls[1];
findControl(ref form);
//此方法还是有问题,findControl(ref C)不能用
方法2:
foreach(Object obj in Page.Controls[1].Controls)
{
if(obj.GetType().ToString ()=="System.Web.UI.WebControls.CheckBox")
{
if((CheckBox)obj).Checked)
{
count++;
}
}
}
//以下可以用来获取Id,和text,但我不知道怎么绑定asp:checkbox的ID,text是可以的,
//有人路过,知道怎么绑定,请指点,万分感谢,小弟刚学asp.net,凑个热闹,
Control form =this.Controls[1];
this.findControl( form);
}
public void findControl( Control cc)
{
foreach(Control c in cc.Controls)
{
if(c is CheckBox)
{
CheckBox cb= (CheckBox)c;
cb.Checked=true;
Response.Write(c.GetType().ToString());
Response.Write(cb.ID.ToString());
}
if(c.HasControls()){
findControl( c) ;
}
// //表示为容器控件
}
}