火炏炎焱燚

火炏炎焱燚

统计

留言簿(1)

使用ASP.NET AJAX 1.0框架PreviewGlitz进行网页动画编程

阅读排行榜

评论排行榜

for each访问checkbox

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)  ; 
            }  
//    //表示为容器控件   
  }   
  }

posted on 2007-04-11 14:11 火炏炎焱燚 阅读(820) 评论(2)  编辑 收藏 引用

评论

# re: for each访问checkbox 2007-04-12 16:31 啊发

如果你的数据绑定控件用的是repeater话,你必须在repeater中加上一例隐藏的数据列(例如asp:lable设置其text=<%#DataBinder.Eval(Container.DataItem,"bookid")%> visable="false">
然后在你的删除按钮中,用这段代码就可以了
foreach(RepeaterItem Item in 你的repeaterID.Items)
{
IsChecked = ((CheckBox)Item.FindControl("你的CheckBoxID")).Checked;
if(IsChecked == true)
{
string xh = ((Label)Item.Controls[1]).Text.Trim();
string sqlstr = "delete from shoppingCart where use_id =" +Session["use_id"] + " and book_id = '" + xh +"'";
}
}

如果用datagrid或者datalist就不用增加隐藏列,它们有datakeyfield书信=你的book_id

下边的一样
bool IsChecked = false;

foreach(DataGridItem item in dgLostNumber.Items)
{
IsChecked = ((CheckBox)(item).FindControl("cbDelete")).Checked;
if(IsChecked == true)
{
string id = dgLostNumber.DataKeys[item.ItemIndex].ToString();
number1.ID = id;
number1.DeleteData();
}
}
//解决了,网络就是好啊  回复  更多评论   

# re: for each访问checkbox 2007-04-12 16:37 啊发

在findcontrol时,你必须先判断行的类型.itemtype
如果是行或是交替行的时候,才用findcontrol,因为你的checkbox控件,肯定是放在<itemtemplate>中的,而在footertemplate中是没有checkbox的.
只有满足是普通行和交替行时,才用findcontrol.
string str = ((Label)Item.Controls[1]).Text.Trim();

异常详细信息: System.InvalidCastException: 指定的转换无效。
这是怎么回事?怎么解决?急。。。线上等!!!!!!!!!!!!!
那是因为你的label放的位置不是第一个位置,在reapter中的第一列是control[1],第二列是control[2],以此向后类推,你自己看看你的label放的是第几列吧?  回复  更多评论   

只有注册用户登录后才能发表评论。