c# 代碼
復(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也傳過(guò)去,提交后在服務(wù)器端取值時(shí)用
}
}
javascript代碼
復(fù)制代碼 代碼如下:
script type="text/javascript">
function onClientClick(selectedId, rowIndex)
{
//用隱藏控件記錄下選中的行號(hào)
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"/>
您可能感興趣的文章:- Android利用GridView實(shí)現(xiàn)單選功能
- asp.net GridView中使用RadioButton單選按鈕的方法
- ASP.NET GridView中加入RadioButton不能單選的解決方案
- DataGridView中CheckBox實(shí)現(xiàn)某一列單選
- Gridview使用CheckBox全選與單選采用js實(shí)現(xiàn)同時(shí)高亮顯示選擇行
- js實(shí)現(xiàn)GridView單選效果自動(dòng)設(shè)置交替行、選中行、鼠標(biāo)移動(dòng)行背景色
- asp.net 擴(kuò)展GridView 增加單選按鈕列的代碼
- Android利用GridView實(shí)現(xiàn)單選效果