Asp.net中使用GridView的单选按钮

1、为GridView添加新列,选择字段类型为TemplateField,页眉文本为“选择”
2、编辑模版,为其添加RadioButton,并将其属性设为自动回发。
3、获取GridView中的某个单选按钮
     RadioButton rb = (RadioButton)ImageView.Rows[0].Cells[0].FindControl("RadioButton1" );
     rb.Checked = true;
     ImageView.SelectedIndex = 0;
4、为GridView添加RowDataBound事件
     添加该事件的处理方法:
     if (e.Row.RowType == DataControlRowType .DataRow)
        {
            RadioButton rdb = (RadioButton)e.Row.Cells[0].FindControl("RadioButton1" );
            rdb.Attributes.Add( "onclick", "setRadio(this)" );//为每一个单选按钮的单击事件
        }
     在前端界面添加setRadio的定义,把当前选择的按钮的checked设为true,其他设为false
     //处理单选按钮
        function setRadio(nowRadio) {
            var myForm, objRadio;
            nowRadio.checked = true;
            myForm = document.forms[0];
            for ( var i = 0; i < myForm.length; i++) {
                if (myForm.elements[i].type == "radio") {
                    objRadio = myForm.elements[i];
                    if (objRadio != nowRadio) {
                        if (objRadio.checked)
                            objRadio.checked = false;
                    }
                }
            }
        }
4、为单选按钮添加change事件
        RadioButton rdb = sender as RadioButton ;
        int index = (rdb.NamingContainer as GridViewRow).RowIndex;
        this.ImageView.SelectedIndex = index;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值