Hello friends please advise.......
I am building an application for Survey. For this i have GridView with two Template fields. The first Template has Label (To display question text) and RadioButtonList (for options). The 2nd Template field is the primary key value. And i have a button on the form.
In the button click event i wrote code to grab RadioButtonList values.But the selected value is coming as null....PLEASE ADVISE....
Let me know if my question is unclear!!!!!
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None">
<Columns>
<asp:TemplateField SortExpression="Question_Text">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Question_Text") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("Question_Text") %>'></asp:Label>
<asp:RadioButtonList ID="RadioButtonList1" Visible='<%# GetRadioSetting(Eval("Answer_Type")) %>' runat="server" RepeatColumns="5" RepeatDirection="Horizontal"
RepeatLayout="Flow" Width="400px">
<asp:ListItem>N/A</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="Question_Number" Visible="False">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Question_Number") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Question_Number") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
protected void Button1_Click(object sender, EventArgs e)
{
IDictionary<int, string> values = new Dictionary<int, string>();
foreach (GridViewRow row in GridView1.Rows)
{
// Assuming Question_Number is placed into DataKeyNames property
int questionNumber = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
RadioButtonList lst = row.FindControl("RadioButtonList1") as RadioButtonList;
string answer = (lst != null) ? lst.SelectedValue.ToString() : String.Empty;
values.Add(questionNumber, answer);
}
}
questionNumber value is coming correct.but answer="" for all the questions.
PLEASE ADVISE.....
I am building an application for Survey. For this i have GridView with two Template fields. The first Template has Label (To display question text) and RadioButtonList (for options). The 2nd Template field is the primary key value. And i have a button on the form.
In the button click event i wrote code to grab RadioButtonList values.But the selected value is coming as null....PLEASE ADVISE....
Let me know if my question is unclear!!!!!
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None">
<Columns>
<asp:TemplateField SortExpression="Question_Text">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Question_Text") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("Question_Text") %>'></asp:Label>
<asp:RadioButtonList ID="RadioButtonList1" Visible='<%# GetRadioSetting(Eval("Answer_Type")) %>' runat="server" RepeatColumns="5" RepeatDirection="Horizontal"
RepeatLayout="Flow" Width="400px">
<asp:ListItem>N/A</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField SortExpression="Question_Number" Visible="False">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Question_Number") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Question_Number") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
protected void Button1_Click(object sender, EventArgs e)
{
IDictionary<int, string> values = new Dictionary<int, string>();
foreach (GridViewRow row in GridView1.Rows)
{
// Assuming Question_Number is placed into DataKeyNames property
int questionNumber = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
RadioButtonList lst = row.FindControl("RadioButtonList1") as RadioButtonList;
string answer = (lst != null) ? lst.SelectedValue.ToString() : String.Empty;
values.Add(questionNumber, answer);
}
}
questionNumber value is coming correct.but answer="" for all the questions.
PLEASE ADVISE.....