GridView RadioButtonList Values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • renuami
    New Member
    • Feb 2009
    • 15

    #1

    GridView RadioButtonList Values

    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!!!!!
    Code:
    <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" />
    Code:
    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.....
    Last edited by Frinavale; Feb 11 '09, 03:21 PM. Reason: Fixed [code] tags and added indentation for legibility
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Is your GridView in Edit Mode when the button is clicked?
    If so, your RadioButtonList wont exist.

    Are you, by any chance, binding the Data to the GridView during the Page Load event?
    If so then you'll lose what the user has selected.

    Comment

    Working...