C# 检索动态创建的RadioButtonList的回发值无效
情况如下: 我正在创建一个调查,其中有X个问题,每个问题可以用六个选项来回答。由于只允许一个选择,我使用RadioButtonList,由于有X个问题,我使用ASP.NET Repeater生成RadioButtonList ASPX代码:C# 检索动态创建的RadioButtonList的回发值无效,c#,asp.net,postback,repeater,radiobuttonlist,C#,Asp.net,Postback,Repeater,Radiobuttonlist,情况如下: 我正在创建一个调查,其中有X个问题,每个问题可以用六个选项来回答。由于只允许一个选择,我使用RadioButtonList,由于有X个问题,我使用ASP.NET Repeater生成RadioButtonList ASPX代码: <asp:Repeater runat="server" ID="ActualSurvey"> <HeaderTemplate> <table> <tr>
<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>
银行
1.
2.
3.
4.
5.
6.
当我回发邮件时,我得到了以下代码:
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);
}
}
}
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
{
ActualSurvey.DataSource=new BankManager().GetBanks();
ActualSurvey.DataBind();
}
}
受保护的无效提交按钮单击(对象发送者,事件参数e)
{
//检查页面是否有效,请求是否有效
if(Page.IsValid&&IsValidRequest)
{
//遍历每个repeater项以查找RadioButtonList
foreach(实际调查中的RepeaterItem RepeaterItem.项目)
{
var bank=repeaterItem.DataItem作为银行;
RadioButtonList radioButtons=repeaterItem.Controls.FindControl();
var额定值=FindRating(单选按钮);
}
}
}
在SubmitButton(提交)按钮,点击实际调查。项目已填充但内部为空,我猜这与页面生命周期有关,但我无法理解。。。另外,Page.Request.Forms不包含这些值
任何帮助都将不胜感激
类问候数据项仅在呈现项之前有效,例如在数据绑定事件中。 回发后,中继器将从ViewState重建其内容 保持该属性有效的一种方法是在Repeater.Init事件中执行数据绑定 aspx:
我觉得你的列表项定义很奇怪
<asp:ListItem data-rating="1"/>
<asp:ListItem data-rating="2"/>
你试着像下面这样改变他们吗
<asp:ListItem Value="1"/>
<asp:ListItem Value="2"/>
我不想显示值,但我想添加一个标识评级的HTML属性。data-*是自定义属性的HTML5标准,我很惭愧。看起来这个自定义HTML属性确实搞乱了它。。。我将不得不以另一种方式添加HTML属性。您也可以使用自定义属性,如data rating=“1”,但我认为需要值才能正常工作。我使用自定义属性时没有工作,所以现在我将坚持使用值。值的问题是除了单选按钮之外还显示了,我不想要…顺便说一下,这就是答案。ASP控件不喜欢这样添加HTML5属性。当然,在我的页面的Ctor中不提供ActualSurvey。我想Page_Init也可以做得很好:)它不起作用。我不必使用DateItem。我想使用表单中发布的值。啊,我只是想发布事件从未触发过
<asp:ListItem data-rating="1"/>
<asp:ListItem data-rating="2"/>
<asp:ListItem Value="1"/>
<asp:ListItem Value="2"/>