您好,登录后才能下订单哦!
本篇文章为大家展示了如何在gridview中利用radiobutton实现一个单选功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
 代码如下:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
RadioButton rb = (RadioButton)e.Row.FindControl("rbtSelect"); 
if (rb != null) 
rb.Attributes.Add("onclick", "onClientClick('" + rb.ClientID + "','" + e.Row.RowIndex + "')"); //把选中行的RowIndex也传过去,提交后在服务器端取值时用 
} 
} 
javascript代码 
复制代码 代码如下:
<script type="text/javascript"> 
function onClientClick(selectedId, rowIndex) 
{ 
//用隐藏控件记录下选中的行号 
var hidden = document.getElementById("Hidden1").value=rowIndex; 
var inputs = document.getElementById("<%=GridView1.ClientID%>").getElementsByTagName("input"); 
for(var i=0; i <inputs.length; i++) 
{ 
if(inputs[i].type=="radio") 
{ 
if(inputs[i].id==selectedId) 
inputs[i].checked = true; 
else 
inputs[i].checked = false; 
} 
} 
} 
</script> 
hmtl代码: 
复制代码 代码如下:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
Width="648px" Font-Size="9pt" onrowcommand="GridView1_RowCommand" 
DataKeyNames="id" onrowdatabound="GridView1_RowDataBound"> 
<Columns> 
<asp:TemplateField> 
<ItemTemplate> 
<asp:RadioButton ID="rbtSelect" runat="server" /> 
</ItemTemplate> 
</asp:TemplateField> 
<asp:TemplateField HeaderText="文件名"> 
<ItemTemplate> 
<asp:LinkButton runat="server" ID="lbtDirName" CommandName="Change" CommandArgument='<%#Container.DataItemIndex %>'> 
<%#Eval("AA") %> 
</asp:LinkButton> 
</ItemTemplate> 
</asp:TemplateField> 
<asp:BoundField DataField="BB" HeaderText="字段1" /> 
<asp:BoundField DataField="CC" HeaderText="字段2" /> 
<asp:BoundField DataField="DD" HeaderText="字段3" /> 
<asp:BoundField DataField="EE" HeaderText="字段4" /> 
</Columns> 
</asp:GridView> 
<input id="Hidden1" type="hidden" runat="server"/> 
上述内容就是如何在gridview中利用radiobutton实现一个单选功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。