C # code
Copy codeThe Code is as follows:
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 + "')"); // pass the RowIndex of the selected row to the server.
}
}
Javascript code
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function onClientClick (selectedId, rowIndex)
{
// Use a hidden control to record the selected row number
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 code:
Copy codeThe Code is as follows:
<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 = "file name">
<ItemTemplate>
<Asp: LinkButton runat = "server" ID = "lbtDirName" CommandName = "Change" CommandArgument = '<% # Container. DataItemIndex %>'>
<% # Eval ("AA") %>
</Asp: LinkButton>
</ItemTemplate>
</Asp: TemplateField>
<Asp: BoundField DataField = "BB" HeaderText = "Field 1"/>
<Asp: BoundField DataField = "CC" HeaderText = "Field 2"/>
<Asp: BoundField DataField = "DD" HeaderText = "Field 3"/>
<Asp: BoundField DataField = "EE" HeaderText = "field 4"/>
</Columns>
</Asp: GridView>
<Input id = "Hidden1" type = "hidden" runat = "server"/>