gridview中實現(xiàn)radiobutton的單選示例
更新時間:2013年08月27日 16:09:43 作者:
radiobutton可以單選,于是想讓gridview也可以實現(xiàn),具體的思路及代碼如下,感興趣的朋友可以參考下
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也傳過去,提交后在服務(wù)器端取值時用
}
}
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"/>
復(fù)制代碼 代碼如下:
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也傳過去,提交后在服務(wù)器端取值時用
}
}
javascript代碼
復(fù)制代碼 代碼如下:
<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代碼:
復(fù)制代碼 代碼如下:
<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"/>
相關(guān)文章
asp.net實現(xiàn)DataList與Repeater嵌套綁定的方法
這篇文章主要介紹了asp.net實現(xiàn)DataList與Repeater嵌套綁定的方法,結(jié)合實例形式分析了DataList與Repeater的步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2016-04-04ajaxControlToolkit中CascadingDropDown的用法說明
今天頭叫寫一個類似三級聯(lián)動的控件,最好實現(xiàn)無刷新,是石油軟件中的一個數(shù)據(jù),需要表現(xiàn)出類似 X1-22 這樣的格式,上下標的數(shù)據(jù)是固定的 想了很多辦法來表現(xiàn)這個數(shù)字,最后決定用3個DropDownList控件2008-11-11ASP.NET MVC3網(wǎng)站創(chuàng)建與發(fā)布(1)
這篇文章主要介紹了ASP.NET MVC3網(wǎng)站創(chuàng)建與發(fā)布,根據(jù)文章內(nèi)容大家可以實現(xiàn)發(fā)布網(wǎng)站,感興趣的小伙伴們可以參考一下2015-08-08Visual Studio 2017 (VS 2017)離線安裝包制作方法
這篇文章主要為大家詳細介紹了Visual Studio 2017離線安裝包的制作方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03asp.net實現(xiàn)調(diào)用存儲過程并帶返回值的方法
這篇文章主要介紹了asp.net實現(xiàn)調(diào)用存儲過程并帶返回值的方法,結(jié)合實例形式較為詳細的分析了asp.net存儲過程調(diào)用的相關(guān)技巧,需要的朋友可以參考下2016-03-03aspxgridview CustomButtonCallback 不支持彈出消息提示解決方法
aspxgridveiw是devexpress的一個grid控件,使用起來還不錯,不能再 CustomButtonCallback 事件中使用response.write,具體的解決方法如下,感興趣的朋友可以參考下哈2013-06-06詳解ASP.NET-----Repeater數(shù)據(jù)控件的用法總結(jié)
本篇文章主要介紹了ASP.NET--Repeater數(shù)據(jù)控件的用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。2016-11-11