Implement radiobutton radio in the gridview

Source: Internet
Author: User

C # code [csharp]
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.
}
}

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
[Javascript]
<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>

<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:
[Html]
<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"/>

<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"/>


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.