0

I have created one grid view with radiobuttonlist as shown in figure. radio-button list items are bind dynamically from SharePoint list.

When I click on submit button, unable to get selected radio option. its always give empty value except first option is selected.string value return empty and int I return -1.

enter image description here

    DataTable dt = new DataTable();
     SPList oList = Generalfunction.GetList(oWeb, "Question", false);
                SPQuery query = new SPQuery();
                query.Query = @"<Where><Eq><FieldRef Name='IsActive' /><Value Type='Boolean'>1</Value></Eq></Where>";
                dt = oList.GetItems(query).GetDataTable();

  if (dt != null)
            {
                GridQue.DataSource = dt;
                GridQue.DataBind();
                foreach (GridViewRow row in GridQue.Rows)
                {
                    RadioButtonList optionlist = (RadioButtonList)row.FindControl("optionlist");
                    int i = row.RowIndex;
                    string[] Value = dt.Rows[i]["Options"].ToString().Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);

                    foreach (string s in Value)
                    {
                        optionlist.Items.Add(s);
                    }
                }
            }


protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                    foreach (GridViewRow row in GridQue.Rows)
                    {
                        RadioButtonList optionlist = row.FindControl("optionlist") as RadioButtonList;
 string value = optionlist.SelectedValue;
                int i = optionlist.SelectedItem;
}
catch (Exception ex)
            {
                Label1.Visible = true;
                Label1.Text = ex.Message;
            }


        }

<asp:GridView ID="GridQue" runat="server" AutoGenerateColumns="false" AllowPaging="true">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <%--<asp:Label ID="lblQNo" runat="server" Text='<%# Eval("QuestionNo")%>'></asp:Label>--%>
                <asp:Label ID="lblQid" runat="server" Text='<%# Eval("Title")%>'></asp:Label>
                <asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question")%>'></asp:Label>

                <asp:Label ID="lblAnswer" runat="server" Text='<%# Eval("Answer")%>' Visible="false"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>

                <asp:RadioButtonList ID="optionlist" runat="server">
                </asp:RadioButtonList>

            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

</asp:GridView>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
0

Replace following

 foreach (string s in Value)
                    {
                        optionlist.Items.Add(s.Trim());
                    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.