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

时间:2011-12-22 21:11:49

标签: c# asp.net postback repeater radiobuttonlist

情况如下: 我正在创建一个调查,其中有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不包含值。

任何帮助都将不胜感激。

亲切的问候

2 个答案:

答案 0 :(得分:1)

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

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

您是否尝试按以下方式更改

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

答案 1 :(得分:0)

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(); 
}
相关问题