I am using a radiobuttonlist in my ASPX page inside a Gridview :
<asp:GridView ID="grdRampPrepQues" runat="server" class="table table-dark table-striped" AlternatingRowStyle-BackColor="#eaeaea" ShowHeader="true"
EmptyDataRowStyle-HorizontalAlign="Center" EmptyDataRowStyle-ForeColor="Red" OnRowCreated="grdRampPrepQues_RowCreated"
OnRowDataBound="grdRampPrepQues_RowDataBound" ShowHeaderWhenEmpty="true" EmptyDataText="No Data Found !!"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Sl. No" ItemStyle-Width="4%" ItemStyle-HorizontalAlign="Center"
HeaderStyle-CssClass="gridHeader" HeaderStyle-HorizontalAlign="Center" ItemStyle-CssClass="gridRow">
<ItemTemplate>
<%--<asp:Label ID="lblSno" runat="server" Enabled="false" Text="<%#Container.DataItemIndex+1%>"></asp:Label>--%>
<asp:Label ID="lblSno" runat="server" CssClass="WrapText" Text='<%#Eval("QUES_NUM_SEQ") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" ItemStyle-Width="60%"
ItemStyle-CssClass="gridRow" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="gridHeader"
ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblRampQues" runat="server" CssClass="WrapText" Text='<%#Eval("RAMP_PREP_QUES") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Response" ItemStyle-Width="20%"
ItemStyle-CssClass="gridRow" HeaderStyle-HorizontalAlign="Center" HeaderStyle-CssClass="gridHeader"
ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblRampQuesType" runat="server" Visible="false" CssClass="WrapText" Text='<%#Eval("QUES_TYPE") %>'></asp:Label>
<asp:RadioButtonList ID="rblOptions" runat="server" RepeatDirection="Horizontal" Visible="false" CssClass="rbl">
<asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
<asp:ListItem Text="No" Value="No"></asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am getting the same radiobuttonlist control tagged to every question and I can select individual options particular to question:
Now the problem I am facing is at the backend while trying to store the data in database. I have a table in my database where I will be adding a new row every time somebody fills this form. In my C# code I have variables like these in which I need to store the values coming from the front when somebody clicks on the submit button and a event is triggered :
ques1 = radiobutton.selectedvalue;
ques2 = radiobutton.selectedvalue;
ques3 = radiobutton.selectedvalue;
Here I am not able to understand how to differentiate and get the values for each question’s radio button uniquely from the front.
Sorry for my English and thanks for helping me out.
2
Answers
Sure, just keep in mind, that controls inside of the GridView repeat over and over. So, you don’t have "one" control say called rblOptions, but you have a "new" control with an ID generated for each row of the GridView.
So, of course you can’t just use the control ID = rblOptions, since which row would it apply to?
So, you have to use “FindControl" for each row in question.
I don’t have your data, but this sample will show a working concept as to how you can save the selected RadioButtonList back into the database.
So, say this GridView of some hotels, and we have an RadioButtonList to set if the room been inspected.
So, this markup:
So, code behind to load is this:
However, turns out some "extra" work is required, since our column in the database could be null (empty). Normally, we could just bind the RadioButtonList to an expression in the markup, but since we have null values, then that will not work.
Hence, on row data bound, we test for null, and ONLY if a value exists, do we set the RB.
Hence this code:
So, now the GV will display, and the user is free to choose/set each RB for each row.
Our save button then has to send the choices back to the database table. Hence this code:
So, above transfers the choices from each row of the GridView back to the data table, and then on "one" operation, we send all changes back to the database.
The result is thus this:
And note how if I go back to the web page with the GridView, you can see my choices persist and are correctly saved back to the database.
Hence this:
In summary, to get use of a control in ONE row of a GridView, then you have to get an instance of that one GridView row, and then use FindControl method of that GridView row, since that control exists "many" times in the page, and you have to resolve to the one row of controls you wish to obtain values from.
To retrieve the selected value from the radio button from grid view controller you could loop through each row and backend code ad get the selected value, below is the code:
Gridview:
Backend code:
Result:
Data from Databse: