用户名:
密 码: 记住
您当前的位置:首页 > 网络编程 > Net教程

gridview中实现radiobutton的单选示例

时间:2015-01-29  来源:互联网  作者:佚名
c# 代码
复制代码 代码如下:
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"/>
来顶一下
返回首页
返回首页
推荐资讯
.net mvc页面UI之Jquery博客日历控件实现代码 .net mvc页面UI之Jquery博客日历控一、效果图二、页面文件页面上需要添加<div id="cal"></di
Asp.net 自带报表的使用详解 Asp.net 自带报表的使用详解1:新建報表所需的數據源DataSet.cs复制代码 代码如下:using
相关文章
栏目更新
栏目热门