检索动态创建的 RadioButtonList 的回发值不起作用

发布于 2024-12-22 16:34:09 字数 4067 浏览 1 评论 0原文

情况如下: 我正在创建一项调查,其中有 X 个问题,每个问题可以用六个选项来回答。由于只允许一种选择,因此我使用 RadioButtonList,并且由于存在 X 个问题,因此我使用 ASP.NET Repeater 来生成 RadioButtionList。

ASPX代码:

<asp:Repeater runat="server" ID="ActualSurvey">
    <HeaderTemplate>
        <table>
            <tr>
                <th class="headLeftCol leftCol headerCol">
                    Bank
                </th>
                <th class="headerCol">
                    1
                </th>
                <th class="headerCol">
                   2
                </th>
                <th class="headerCol">
                    3
                </th>
                <th class="headerCol">
                   4
                </th>
                <th class="headerCol">
                    5
                </th>
                <th class="headerCol">
                    6
                </th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr class="even">
            <td class="leftCol">
                <%# ((ReliabilityMeter.DataAccessLayer.Bank)Container.DataItem).DisplayName %>
            </td>
            <td class="midCol" colspan="6">
                <asp:RadioButtonList ID="rbl1" runat="server" CssClass="radioButtonList" RepeatDirection="Horizontal">
                    <asp:ListItem data-rating="1" />
                    <asp:ListItem data-rating="2"/>
                    <asp:ListItem data-rating="3"/>
                    <asp:ListItem data-rating="4"/>
                    <asp:ListItem data-rating="5"/>
                    <asp:ListItem data-rating="6" Selected="True" />
                </asp:RadioButtonList>
            </td>
        </tr>
    </ItemTemplate>
    <AlternatingItemTemplate>
        <tr class="odd">
            <td class="leftCol">
                <%# ((ReliabilityMeter.DataAccessLayer.Bank)Container.DataItem).DisplayName %>
            </td>
            <td class="midCol" colspan="6">
                <asp:RadioButtonList ID="rbl2" runat="server" CssClass="radioButtonList" RepeatDirection="Horizontal">
                    <asp:ListItem data-rating="1"/>
                    <asp:ListItem data-rating="2"/>
                    <asp:ListItem data-rating="3"/>
                    <asp:ListItem data-rating="4"/>
                    <asp:ListItem data-rating="5"/>
                    <asp:ListItem data-rating="6" Selected="True" />
                </asp:RadioButtonList>
            </td>
        </tr>
    </AlternatingItemTemplate>
</asp:Repeater>
<tr class="footerRow">
    <td colspan="7">
        <asp:Button ID="SubmitButton" runat="server" Text="Insturen" OnClick="SubmitButton_Click" />
    </td>
</tr>
</table>

当我回发时,我得到以下代码: C# 代码隐藏

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ActualSurvey.DataSource = new BankManager().GetBanks();
            ActualSurvey.DataBind();
        }
    }

protected void SubmitButton_Click(object sender, EventArgs e)
    {
        //Check if the page is valid and the request is valid
        if (Page.IsValid && IsValidRequest)
        {
            //Iterate through each repeater item to find the RadioButtonList
            foreach (RepeaterItem repeaterItem in ActualSurvey.Items)
            {
                var bank = repeaterItem.DataItem as Bank;
                RadioButtonList radioButtons = repeaterItem.Controls.FindControl<RadioButtonList>();
                var rating = FindRating(radioButtons);
            }
        }
    }

在 SubmitButton_Click 处,ActualSurvey.Items 已填充,但内部是空的,我猜它与页面生命周期有关,但我无法弄清楚...而且 Page.Request.Forms 不包含值。

任何帮助将不胜感激。

亲切的问候

The situation is as following:
I am creating a survey where there are X questions and each question could be answered with six choices. Since there is only one choice allowed, I use a RadioButtonList and since there are X questions, I use a ASP.NET Repeater to generate the RadioButtionList.

ASPX code:

<asp:Repeater runat="server" ID="ActualSurvey">
    <HeaderTemplate>
        <table>
            <tr>
                <th class="headLeftCol leftCol headerCol">
                    Bank
                </th>
                <th class="headerCol">
                    1
                </th>
                <th class="headerCol">
                   2
                </th>
                <th class="headerCol">
                    3
                </th>
                <th class="headerCol">
                   4
                </th>
                <th class="headerCol">
                    5
                </th>
                <th class="headerCol">
                    6
                </th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr class="even">
            <td class="leftCol">
                <%# ((ReliabilityMeter.DataAccessLayer.Bank)Container.DataItem).DisplayName %>
            </td>
            <td class="midCol" colspan="6">
                <asp:RadioButtonList ID="rbl1" runat="server" CssClass="radioButtonList" RepeatDirection="Horizontal">
                    <asp:ListItem data-rating="1" />
                    <asp:ListItem data-rating="2"/>
                    <asp:ListItem data-rating="3"/>
                    <asp:ListItem data-rating="4"/>
                    <asp:ListItem data-rating="5"/>
                    <asp:ListItem data-rating="6" Selected="True" />
                </asp:RadioButtonList>
            </td>
        </tr>
    </ItemTemplate>
    <AlternatingItemTemplate>
        <tr class="odd">
            <td class="leftCol">
                <%# ((ReliabilityMeter.DataAccessLayer.Bank)Container.DataItem).DisplayName %>
            </td>
            <td class="midCol" colspan="6">
                <asp:RadioButtonList ID="rbl2" runat="server" CssClass="radioButtonList" RepeatDirection="Horizontal">
                    <asp:ListItem data-rating="1"/>
                    <asp:ListItem data-rating="2"/>
                    <asp:ListItem data-rating="3"/>
                    <asp:ListItem data-rating="4"/>
                    <asp:ListItem data-rating="5"/>
                    <asp:ListItem data-rating="6" Selected="True" />
                </asp:RadioButtonList>
            </td>
        </tr>
    </AlternatingItemTemplate>
</asp:Repeater>
<tr class="footerRow">
    <td colspan="7">
        <asp:Button ID="SubmitButton" runat="server" Text="Insturen" OnClick="SubmitButton_Click" />
    </td>
</tr>
</table>

When I postback, I got the following code:
C# Code behind

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ActualSurvey.DataSource = new BankManager().GetBanks();
            ActualSurvey.DataBind();
        }
    }

protected void SubmitButton_Click(object sender, EventArgs e)
    {
        //Check if the page is valid and the request is valid
        if (Page.IsValid && IsValidRequest)
        {
            //Iterate through each repeater item to find the RadioButtonList
            foreach (RepeaterItem repeaterItem in ActualSurvey.Items)
            {
                var bank = repeaterItem.DataItem as Bank;
                RadioButtonList radioButtons = repeaterItem.Controls.FindControl<RadioButtonList>();
                var rating = FindRating(radioButtons);
            }
        }
    }

At SubmitButton_Click the ActualSurvey.Items is filled but empty inside, I guess that it has got something to do with the Page Lifecylebut I can't figure it out... Also Page.Request.Forms does not contain the values.

Any help would be appreciated.

Kind regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷清清 2024-12-29 16:34:09

你的 ListItem 定义在我看来很奇怪。

<asp:ListItem data-rating="1"/>
<asp:ListItem data-rating="2"/>

你是否尝试像下面这样改变它们

<asp:ListItem Value="1"/>
<asp:ListItem Value="2"/>

Your ListItem definitions seems to me weird.

<asp:ListItem data-rating="1"/>
<asp:ListItem data-rating="2"/>

Did you try to change them like below

<asp:ListItem Value="1"/>
<asp:ListItem Value="2"/>
暮色兮凉城 2024-12-29 16:34:09

DataItem 仅在呈现项目之前有效,例如在 DataBinding 事件中。
回发后,中继器会从 ViewState 重建其内容。

中执行数据绑定:

保持该属性有效的一种方法是在 Repeater.Init 事件aspx:

<asp:Repeater runat="server" ID="ActualSurvey" OnInit="ActualSurvey_Init"> 
 ...
</asp:Repeater>

....

cs:

void ActualSurvey_Init(object sender, EventArgs e)
{
      object banks = Cache["ActualSurvey_Banks"];
      if (banks == null)
      {
         banks = new BankManager().GetBanks();
         Cache.Insert("ActualSurvey_Banks", banks);
      }

      ActualSurvey.DataSource = banks;
      ActualSurvey.DataBind(); 
}

DataItem is only valid before items are rendered, e.g in DataBinding event.
After the post back the repeater rebuilds it's content from ViewState.

One way to keep that property valid is to perform data-binding in Repeater.Init event

aspx:

<asp:Repeater runat="server" ID="ActualSurvey" OnInit="ActualSurvey_Init"> 
 ...
</asp:Repeater>

....

cs:

void ActualSurvey_Init(object sender, EventArgs e)
{
      object banks = Cache["ActualSurvey_Banks"];
      if (banks == null)
      {
         banks = new BankManager().GetBanks();
         Cache.Insert("ActualSurvey_Banks", banks);
      }

      ActualSurvey.DataSource = banks;
      ActualSurvey.DataBind(); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文