Reputation: 317
I have a Gridview binded to a database table with a Radio Button Column, I want to select a row with single radio button. It is necessary a radio button group? I have that code above with checkbox but i want to change it to radio button. I think radio button code is similiar. Thank you. I'm using ASP.NET C#
<asp:GridView ID="DateGrid" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="RowSelector" runat="server" GroupName="SelectGroup" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
foreach (GridViewRow row in DateGrid.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRow = (row.Cells[0].FindControl("DateChBox") as CheckBox);
if (chkRow.Checked)
{
startdate = row.Cells[2].Text;
enddate = row.Cells[3].Text;
Upvotes: 5
Views: 25076
Reputation: 81
<asp:GridView ID="DateGrid" runat="server" DataSourceID="sdsData">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="chk" ClientIDMode="Static" runat="server" onclick="$('[id=chk]').prop('checked' , '');$(this).prop('checked' , 'checked');"></asp:RadioButton>
<asp:HiddenField ID="hdn" runat="server" Value='<%# Eval("Fserial") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="20px" />
</asp:TemplateField>
<asp:BoundField DataField="Fname" HeaderText="Name" SortExpression="Fname" />
</Columns></asp:GridView>
Upvotes: 0
Reputation: 1106
This can be done from the back-end by enabling AutoPostBack on the Radio Button:
<asp:RadioButton ID="RowSelector" runat="server" GroupName="SelectGroup" Checked="false" AutoPostBack="true" OnCheckedChanged="rd_CheckedChanged" />
and using the following code in the OnCheckedChanged Event:
static int rdChecked = -1; //Use this variable to store the last checked row
protected void rd_CheckedChanged(object sender, EventArgs e)
{
if (rdChecked != -1)
{
RadioButton rdSelection = DateGrid.Rows[rdChecked].FindControl("RowSelector") as RadioButton;
rdSelection.Checked = false;
}
for (int i = 0; i < DateGrid.Rows.Count; i++)
{
RadioButton rdSelection = DateGrid.Rows[i].FindControl("RowSelector") as RadioButton;
if (rdSelection.Checked)
{
rdChecked = i;
}
}
}
Upvotes: 1
Reputation: 1024
This might help you
Code In Front
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" Width="100%">
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate><asp:RadioButton ID="RowSelector" runat="server" onclick="checkRadioBtn(this);" /></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Start Date">
<ItemTemplate><asp:Label ID="lblStartDate" runat="server" Text='<%# DateTime.Now.ToString("MM/dd/yyyy") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="End Date">
<ItemTemplate><asp:Label ID="lblEndDate" runat="server" Text='<%# DateTime.Now.ToString("MM/dd/yyyy") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Javascript will uncheck other while you check other radio button
<script type="text/javascript">
function checkRadioBtn(id) {
var gv = document.getElementById('<%=gv.ClientID %>');
for (var i = 1; i < gv.rows.length; i++) {
var radioBtn = gv.rows[i].cells[0].getElementsByTagName("input");
// Check if the id not same
if (radioBtn[0].id != id.id) {
radioBtn[0].checked = false;
}
}
}
</script>
Code Behind for binding purpose...
protected void Page_Load(object sender, EventArgs e)
{
// Check
if (!IsPostBack)
{
// Varaible
DataTable dt = new DataTable();
dt.Columns.Add("A");
dt.Columns.Add("B");
for (int i = 0; i < 3; i++) dt.Rows.Add(i.ToString(), i.ToString());
// Bind
gv.DataSource = dt;
gv.DataBind();
}
}
Upvotes: 6