CheckBox的CheckedChanged事件获取所在GridView中的Cell值

在开发的一个网站的后台中,用到这么一个功能:

    1)在GridView中绑定一个CheckBox;

    2)当选中/取消选中CheckBox时,激发后台表中对应数据操作或者设置对应标志;

  在 protected void CheckBox1_CheckedChanged(object sender, EventArgs e){} 中,通过获取sender的parent来进行操作,代码为:

   

ExpandedBlockStart.gif 代码
 1              CheckBox chk  =  (CheckBox)sender;
 2              DataControlFieldCell dcf  =  (DataControlFieldCell)chk.Parent;
 3              GridViewRow gvr  =  (GridViewRow)dcf.Parent;
 4               try
 5              {
 6                   if  (chk.Checked)
 7                  {
 8                      model.NewsId  =  NewsID; // model为映射表的实例化对象
 9                      model.AddressId  =   int .Parse(gvr.Cells[ 1 ].Text); // 此处宏来获取GridView中的某一个Cell的值;
10                      model.Status  =   1 ;
11                      model.Memo  =   "" ;
12                      bll.Add(model);  // 执行一个添加操作.
13                  }
14                   else
15                  {
16                      strsql  =   " delete from biao1 where NewsId= "   +  NewsID  +   "  and AddressId= "   +   int .Parse(gvr.Cells[ 1 ].Text);
17                      DbHelper.ExecuteSql(strsql);
18                  }
19              }
20               catch  (Exception ex)
21              {
22                   // throw(this, ex.ToString());
23              }

 

   附带:

        如果要遍历GridView中的CheckBox是否选中,并做相应操作,则:

   

ExpandedBlockStart.gif 代码
 1               for  ( int  j  =   0 ; j  <  GridView1.Rows.Count; j ++ )
 2              {
 3                  CheckBox chbox  =  GridView1.Rows[j].FindControl( " CheckBox1 " as  CheckBox;
 4                   if  (chbox.Checked)
 5                  {
 6                      model.NewsId  =  NewsID; //
 7                      model.AddressId  =   int .Parse(GridView1.Rows[j].Cells[ 1 ].Text);
 8                      model.Status  =   1 ;
 9                      model.Memo  =   "" ;
10                      bll.Add(model);
11                  }
12              }

 

 

 

转载于:https://www.cnblogs.com/mushaobai/archive/2010/04/12/1710663.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值