Select one Gridview row with radio button ASP.NET
Asked Answered
A

3

5

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;
Aquilar answered 18/8, 2015 at 15:45 Comment(4)
Is something like this (asp.net/web-forms/overview/data-access/enhancing-the-gridview/…) what you are trying to do?Jonell
@MartínMisol what I'm trying to do is to select just a single radio button. My gridview selects multipleAquilar
In the article I linked, figure 11 displays a problem like the one you are having, and then a solution for it is explained in detail.Jonell
@MartínMisol Thank you, it works now :)Aquilar
P
6

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();
    }
}
Paulson answered 19/8, 2015 at 7:22 Comment(0)
T
1

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;
       }
   }
}
Telangiectasis answered 21/12, 2020 at 15:19 Comment(0)
B
0
<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>
Bow answered 29/12, 2020 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.