基础环境:
GridView内部RadioButton使用模板列实现
Code
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:RadioButton ID="rbSelect" runat="server" CssClass="p_j" />
</ItemTemplate>
<HeaderTemplate>
<asp:Literal ID="liSelectText" runat="server" Text="选择" />
</HeaderTemplate>
</asp:TemplateField>
一, 实现RadioButton 在 GridView内部的单选:
1. JS函数
Code
function setRadio(nowRadio)
{
var myForm,objRadio;
myForm=document.forms[0];
///alert(myForm);
for(var i=0;i<myForm.length;i++)
{
if(myForm.elements[i].type=="radio")
{
objRadio=myForm.elements[i];
///alert(objRadio.name);
if(objRadio!=nowRadio && objRadio.name.indexOf("gvOrganizationInfo")>-1 && objRadio.name.indexOf("rbSelect")>-1)
{
//alert(objRadio.name);
if(objRadio.checked)
{
objRadio.checked=false;
}
}
else
{
//alert("current radio button name: " + objRadio.name + " and checked status:"+objRadio.checked);
}
}
}
}
2. GridView RowDataBound中加入:
为每个RadioButton绑定客户端事件
Code
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
try
{
((RadioButton)e.Row.FindControl("rbSelect")).Attributes.Add("onclick", "setRadio(this)");
}
catch { }
}
}