GridView RadioButtonList Values

Discussion in '.NET' started by renuami, Feb 10, 2009.

  1. renuami

    renuami

    Joined:
    Feb 10, 2009
    Messages:
    2
    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.....
    renuami, Feb 10, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James
    Replies:
    3
    Views:
    2,704
    James
    Dec 15, 2007
  2. Keith G Hicks
    Replies:
    3
    Views:
    749
    Keith G Hicks
    Feb 18, 2008
  3. Ben
    Replies:
    1
    Views:
    1,491
  4. Andy in South Jersey

    RadioButtonList(HTML) Control in a GridView

    Andy in South Jersey, Jul 7, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    630
    Andy in South Jersey
    Jul 7, 2008
  5. Ben
    Replies:
    5
    Views:
    271
Loading...

Share This Page