您的位置:首页 > 编程语言 > ASP

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

2013-05-22 15:08 375 查看
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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: