0

I am adding a list of radiobutton lists dynamically to the page and on button click I want to store the selected values .i am following this sample code below. How can I retrieve the selectedIndexes of these dynamically created RadioButtonLists? I've spent the better part of the day trying various fixes from other similar questions online, but have had no luck. If I use 'Inspect Element' in Chrome, I am able to see the RadioButtonLists in their desired locations ostensibly with the ID I have assigned (RBQuestion_1, RBQuestion_2, etc),

Sub BindForm()

Dim tblStars As New Table()
    Dim rb As New RadioButtonList()
    rb.ID = RBQuestion_" & row("Id")
    Dim tc As New TableCell()
    Dim tr As New TableRow()
    tc.Controls.Add(rb)
    tr.cells.Add(tc)

    tblStars.Rows.Add(tr)

    form1.Controls.Add(tblStars)
Next
end sub


 Protected Sub btnSave_click(ByVal sender As Object, ByVal e As EventArgs)

For Each ctrl As Control In Page.FindControl(RBQuestion_" & row("Id"))
            If TypeOf ctrl Is RadioButtonList Then
                Dim rbl As RadioButtonList = DirectCast(ctrl, RadioButtonList)
                For i As Integer = 0 To rbl.Items.Count - 1
                    If rbl.Items(i).Selected Then
                        'get the  value of the selected radio button'
                        Dim value As String = rbl.SelectedItem.Value
                    End If
                Next
            End If
        Next
end sub
4

1 回答 1

1

您正在查看页面的控制列表。

您的单选按钮将不存在。它们位于您将它们添加到的 TableCell 的控制列表中。

您需要找到您的表格,然后逐步浏览每一行,并为每一行找到单元格,然后在单元格内找到单选按钮。

或者,您可以尝试在创建按钮时将按钮添加到 RadioButton 类型的对象范围通用列表控件。然后直接从您的 btnSave_Click 事件处理程序中引用它们。优点:无需导航控制层次结构。缺点:将代码与实际的底层实现分开。当开发人员将来查看这些控件的真实位置时,这可能会产生误导......在您有时间忘记您做了什么以及为什么之后可能包括您自己的集合。对我来说,这通常是美好的一天大约 4 个小时。

因此,我更喜欢导航层次结构方法,因为它更符合实际布局的方式。但我很确定通用列表方法也会起作用。


编辑

我认为您缺少的一块拼图是覆盖 CreateChildControls 方法。

我承认你的问题是用 VB 表达的。但我对 VB 很不满意,所以我改用 C#。希望这对您有用。

在 ASP.NET 开发服务器中托管时,确认这在 VS 2010 中按预期工作。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            this.CreateRadioButtons();
        }

        private void CreateRadioButtons()
        {
            var tblStars = new Table();
            tblStars.ID = "tblStars";

            ListItem opt1 = new ListItem();
            opt1.Text = "I like red";
            opt1.Value = "Red";

            ListItem opt2 = new ListItem();
            opt2.Text = "I like green";
            opt2.Value = "Green";

            ListItem opt3 = new ListItem();
            opt3.Text = "I like blue";
            opt3.Value = "Blue";

            var rb = new RadioButtonList();
            rb.ID = "RBQuestion_1";

            rb.Items.Add(opt1);
            rb.Items.Add(opt2);
            rb.Items.Add(opt3);

            var tc = new TableCell();
            var tr = new TableRow();
            tc.Controls.Add(rb);
            tr.Cells.Add(tc);
            tblStars.Rows.Add(tr);

            form1.Controls.Add(tblStars);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var tblStars = this.form1.FindControl("tblStars") as Table;
            if (tblStars == null)
                return;

            foreach (TableRow row in tblStars.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    var rb = cell.FindControl("RBQuestion_1") as RadioButtonList;
                    if (rb == null)
                        continue;

                    var selectedValue = rb.SelectedValue;
                }
            }
        }
    }
}

我相应的页面标记:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
    </div>
    </form>
</body>
</html>
于 2013-07-25T05:17:52.700 回答