检索动态创建的 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 不包含值。
任何帮助将不胜感激。
亲切的问候
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的 ListItem 定义在我看来很奇怪。
你是否尝试像下面这样改变它们
Your ListItem definitions seems to me weird.
Did you try to change them like below
DataItem 仅在呈现项目之前有效,例如在 DataBinding 事件中。
回发后,中继器会从 ViewState 重建其内容。
中执行数据绑定:
保持该属性有效的一种方法是在 Repeater.Init 事件aspx:
cs:
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:
cs: