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
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"/>