locked
Radio Button in GridView - want to save selection after postback, help RRS feed

  • Question

  • User-1995945195 posted

    I did some extensive reading on putting a Radio Button column in a GridView control.  I know that if you want to uniquely select a Radio button, the RadioButton server control isn't going to work, and instead I have to use the HTML "<input>" tag, and just reference the value in Request.Form[].

    My question is- after I post back and do some operation(s), when the page comes up again, I want to keep track of which radio button was selected, and make that 'checked'.  What is the proper way of doing this?

    Many thanks in advance.

     

    Thursday, August 5, 2010 5:50 PM

Answers

  • User3866881 posted

    If you want to select a single choice and save it into the DB, you can also use RadioButtonList, here's the codes:

    <asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" 
                  AutoGenerateColumns="False" 
                  DataSourceID="SqlDataSource1" 
                  OnRowDataBound="GridView1_RowDataBound" 
                  OnRowUpdated="GridView1_RowUpdated" 
                  OnRowUpdating="GridView1_RowUpdating" 
                  OnRowEditing="GridView1_RowEditing">
     <Columns>
     <asp:TemplateField HeaderText="ID">
     <ItemTemplate>
     <asp:Label ID="lblID" runat="server" Text='<%#Eval("ID") %>'>
     </asp:Label>
     </ItemTemplate>
     </asp:TemplateField>
     
     <asp:BoundField DataField="Name" HeaderText="Name" 
                     SortExpression="Name" />
     <asp:TemplateField HeaderText="Gender">
     <ItemTemplate>
     <asp:Label ID="lblGender" runat="server" 
                Text='<%#Eval("Sex") %>'>
     </asp:Label>
     </ItemTemplate>
     <EditItemTemplate>
     <asp:RadioButtonList ID="rbGenderEdit" runat="server">
     <asp:ListItem>Male</asp:ListItem>
     <asp:ListItem>Female</asp:ListItem>
     </asp:RadioButtonList>
     </EditItemTemplate>
     </asp:TemplateField>
     
     <asp:TemplateField HeaderText="Marital Status">
     <ItemTemplate>
     <asp:Label ID="lblStatus" runat="server" 
                Text='<%#Eval("MaritalStatus") %>'>
     </asp:Label>
     </ItemTemplate>
     <EditItemTemplate>
     <asp:DropDownList ID="ddlStatusEdit" runat="server">
     <asp:ListItem>Single</asp:ListItem>
     <asp:ListItem>Married</asp:ListItem>
     </asp:DropDownList>
     </EditItemTemplate>
     </asp:TemplateField>
     <asp:CommandField ShowEditButton="True" />
    </Columns>
    </asp:GridView>
    
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT [ID], [Name], [Sex], [MaritalStatus] 
                   FROM [Details]" 
    UpdateCommand="Update Details Set [Name]=@Name, [Sex]=@Sex, 
                  [MaritalStatus]=@MaritalStauts Where [ID]=@ID">
       <UpdateParameters>
           <asp:Parameter Name="Name" />
           <asp:Parameter Name="Sex" />
           <asp:Parameter Name="ID" />
           <asp:Parameter Name="MaritalStauts" />
       </UpdateParameters>
    </asp:SqlDataSource>
     
    And your cs codes should be :
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    02.{
    03. DataRowView dRowView = (DataRowView)e.Row.DataItem;
    04. if (e.Row.RowType == DataControlRowType.DataRow)
    05. {
    06.   if ((e.Row.RowState & DataControlRowState.Edit) > 0)
    07.   {
    08.     RadioButtonList rblGender = (RadioButtonList)e.Row.FindControl("rbGenderEdit");
    09.     DropDownList ddlStatus = (DropDownList)e.Row.FindControl("ddlStatusEdit");
    10.     rblGender.SelectedValue = dRowView[2].ToString();
    11.     ddlStatus.SelectedValue = dRowView[3].ToString();
    12.   }
    13. }
    14.         
    15.}
    16.     
    17.protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    18.{
    19. RadioButtonList rblGender = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("rbGenderEdit");
    20. DropDownList ddlStatus = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlStatusEdit");
    21. SqlDataSource1.UpdateParameters["Sex"].DefaultValue = rblGender.SelectedValue;
    22. SqlDataSource1.UpdateParameters["MaritalStauts"].DefaultValue = ddlStatus.SelectedValue;
    23.}
     
     
    Please refer to this sample download to see more:
    Download the full codes:
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 8, 2010 10:18 PM

All replies

  • User626880745 posted

    If its across the same page that you would need it across postbacks, you could use a ViewState variable

    Thursday, August 5, 2010 6:56 PM
  • User-1995945195 posted

    Thanks for the reply - correct me if I am wrong, but I am not using a RadioButton server control, rather an <input type="radio"> html (client-side) control, and therefore cannot use ViewState.

    What do you all think?

    Thursday, August 5, 2010 8:06 PM
  • User626880745 posted

    :) I said, a variable:

    use a ViewState variable

    it would store a value (not a control)

    like: ViewState["radio"] = your value 

    Thursday, August 5, 2010 8:11 PM
  • User-1995945195 posted

    ahh, okay.  So assuming my radio button is called "ID", if I do ViewState["ID"], the correct radio button will still show as being selected ('checked')?  Or do I have to do anything else in addition to that?

    Thanks.


    Thursday, August 5, 2010 8:17 PM
  • Thursday, August 5, 2010 9:25 PM
  • User1495804863 posted

    Hi,

    You can try in this way also

    To memorize checked row after postback, the only way i've found to do that is a javascript on the body onload event, it works even within a master page:

    - add an ID attribute on the radio button
    <input name="rdChoixConso" type="radio" ID="<%# Eval("n_client_cdr")%>" value='<%# Eval("n_client_cdr") %>'/>

    - re-check button on page load
    <body onload="rd=document.all('<%=Request.Form["rdChoixConso"]%>');if (rd) rd.checked=true;"> 

    Thursday, August 5, 2010 11:21 PM
  • User-1995945195 posted

    Peter,

    I looked at that post, and tried that method, but it only works for radiobutton's that have incremental values (hence the 'index' iterator value he uses).  My radio button values are all unique ID's (arbitrary numbers).

    Any ideas? 

    Friday, August 6, 2010 9:40 AM
  • User626880745 posted

    yeah, and that works quite well (not with paging though, but that might be another issue to look at with ViewState)


    Are you going to use postback on the radios? (you were using html controls anyway)

    IF the IDs for the row (record) are going to be used there are other ways to get at. but at this point it would be good to know.

    Friday, August 6, 2010 9:56 AM
  • User3866881 posted

    If you want to select a single choice and save it into the DB, you can also use RadioButtonList, here's the codes:

    <asp:GridView ID="GridView1" runat="server" DataKeyNames="ID" 
                  AutoGenerateColumns="False" 
                  DataSourceID="SqlDataSource1" 
                  OnRowDataBound="GridView1_RowDataBound" 
                  OnRowUpdated="GridView1_RowUpdated" 
                  OnRowUpdating="GridView1_RowUpdating" 
                  OnRowEditing="GridView1_RowEditing">
     <Columns>
     <asp:TemplateField HeaderText="ID">
     <ItemTemplate>
     <asp:Label ID="lblID" runat="server" Text='<%#Eval("ID") %>'>
     </asp:Label>
     </ItemTemplate>
     </asp:TemplateField>
     
     <asp:BoundField DataField="Name" HeaderText="Name" 
                     SortExpression="Name" />
     <asp:TemplateField HeaderText="Gender">
     <ItemTemplate>
     <asp:Label ID="lblGender" runat="server" 
                Text='<%#Eval("Sex") %>'>
     </asp:Label>
     </ItemTemplate>
     <EditItemTemplate>
     <asp:RadioButtonList ID="rbGenderEdit" runat="server">
     <asp:ListItem>Male</asp:ListItem>
     <asp:ListItem>Female</asp:ListItem>
     </asp:RadioButtonList>
     </EditItemTemplate>
     </asp:TemplateField>
     
     <asp:TemplateField HeaderText="Marital Status">
     <ItemTemplate>
     <asp:Label ID="lblStatus" runat="server" 
                Text='<%#Eval("MaritalStatus") %>'>
     </asp:Label>
     </ItemTemplate>
     <EditItemTemplate>
     <asp:DropDownList ID="ddlStatusEdit" runat="server">
     <asp:ListItem>Single</asp:ListItem>
     <asp:ListItem>Married</asp:ListItem>
     </asp:DropDownList>
     </EditItemTemplate>
     </asp:TemplateField>
     <asp:CommandField ShowEditButton="True" />
    </Columns>
    </asp:GridView>
    
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
    SelectCommand="SELECT [ID], [Name], [Sex], [MaritalStatus] 
                   FROM [Details]" 
    UpdateCommand="Update Details Set [Name]=@Name, [Sex]=@Sex, 
                  [MaritalStatus]=@MaritalStauts Where [ID]=@ID">
       <UpdateParameters>
           <asp:Parameter Name="Name" />
           <asp:Parameter Name="Sex" />
           <asp:Parameter Name="ID" />
           <asp:Parameter Name="MaritalStauts" />
       </UpdateParameters>
    </asp:SqlDataSource>
     
    And your cs codes should be :
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    02.{
    03. DataRowView dRowView = (DataRowView)e.Row.DataItem;
    04. if (e.Row.RowType == DataControlRowType.DataRow)
    05. {
    06.   if ((e.Row.RowState & DataControlRowState.Edit) > 0)
    07.   {
    08.     RadioButtonList rblGender = (RadioButtonList)e.Row.FindControl("rbGenderEdit");
    09.     DropDownList ddlStatus = (DropDownList)e.Row.FindControl("ddlStatusEdit");
    10.     rblGender.SelectedValue = dRowView[2].ToString();
    11.     ddlStatus.SelectedValue = dRowView[3].ToString();
    12.   }
    13. }
    14.         
    15.}
    16.     
    17.protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    18.{
    19. RadioButtonList rblGender = (RadioButtonList)GridView1.Rows[e.RowIndex].FindControl("rbGenderEdit");
    20. DropDownList ddlStatus = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlStatusEdit");
    21. SqlDataSource1.UpdateParameters["Sex"].DefaultValue = rblGender.SelectedValue;
    22. SqlDataSource1.UpdateParameters["MaritalStauts"].DefaultValue = ddlStatus.SelectedValue;
    23.}
     
     
    Please refer to this sample download to see more:
    Download the full codes:
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 8, 2010 10:18 PM