RadioButton和CheckBox激发单选和多选事件

Posted on 2009-08-25 17:00  sl2008  阅读(740)  评论(0编辑  收藏  举报
当RadioButton和CheckBox存在于Gridview的模板列中时,由于无CommanArgument,故无法取得当前行的索引值!
如何取得呢?
方法如下: 利用控件的CheckedChanged事件的sender获取事件传入参数。
1. 先要设置控件的AutoPostBack=true
2.编写CheckedChanged事件:
              //获得当前行索引
            RadioButton rb = (RadioButton)sender;
            GridViewRow curRow = (GridViewRow)rb.NamingContainer;

         if(rb.checked)
         {。。。。。。。
         //作出相应处理
         }        

另外: Gridview中的RadioButton无法实现单选,实现方法如下:
// 注意先要设置 RadioButton1.AutoPostBack="True"
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
         for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                ((RadioButton)this.GridView1.Rows[i].FindControl("RadioButton1")).Checked = false;
            }
            ((RadioButton)sender).Checked = true;//经典
       
    }

Copyright © 2024 sl2008
Powered by .NET 8.0 on Kubernetes