i have:
<asp:TemplateField HeaderText="Choose Option">
<ItemTemplate>
<asp:RadioButtonList ID="rdbChoice" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
In the button click, I want to get the RadioButtonList.SelectedValue. I try:
protected void btnsubmit_Click(object sender, EventArgs e)
{
for (int i = 0; i <= gvQuestion.Rows.Count - 1; i++)
{
RadioButtonList rd1 = (RadioButtonList)gvQuestion.FindControl("rdbChoice");
string rd = rd1.SelectedValue.ToString();
}
}
But i get an error Object reference not set to an instance of an object. What is my mistake?
The NamingContainer of your RadioButtonList is the GridViewRow not the GridView, so this works:
Keep in mind that FindControl does not look recursively into child container, it only searches the current NamingContainer for the given ID.