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 2.0中預(yù)設(shè)的cookie
ASP.NET 2.0中預(yù)設(shè)的cookie...2006-09-09
.net 中的SqlConnection連接池機(jī)制詳解
.net 中通過 SqlConnection 連接 sql server,我們會發(fā)現(xiàn)第一次連接時總是很耗時,但后面連接就很快,這個其實和SqlConnection 的連接池機(jī)制有關(guān)2013-04-04
Entity?Framework?Core生成數(shù)據(jù)庫表
這篇文章介紹了Entity?Framework?Core生成數(shù)據(jù)庫表的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03

