I'm working on an ASP.NET where I have a RadListView (Telerik). Inside every RadListView's item there's a RadioButtonList with two radio buttons. What I need to do is:
- on the first开发者_JAVA百科 time the page gets loaded one of the two radio buttons must be selected by default;
- on postback I have to check that the user has selected the other one (trying to do it with a CustomValidator);
- on post back I have to keep the status of the RadioButtonLists.
Any idea on how can I do that?
Here's part of my code:
<telerik:RadListView ID="rlvContracts" runat="server">
<ItemTemplate>
<fieldset style="margin-bottom: 30px;">
<table cellpadding="0" cellspacing="0">
[...]
<asp:RadioButtonList runat="server" EnableViewState="true" ID="rblContract" RepeatDirection="Horizontal">
<asp:ListItem Value="1" Text="Accept"></asp:ListItem>
<asp:ListItem Value="0" Text="I do not accept" Selected="True"></asp:ListItem>
</asp:RadioButtonList>
[...]
<!-- Custom Validator Here -->
[...]
</table>
</fieldset>
</ItemTemplate>
</telerik:RadListView>
Any help (even links to tutorials) is apreciated
Thanks in advance, Daniele
In order to do the first step you can either follow the idea that you posted in your code above (declarative setting of the selected RadioButton) or you could programmatically set it by doing something along the following lines:
//MyRadListView is the name of the RadListView on the page
RadListView myListView = MyRadListView;
RadioButtonList myRadioButtonList = myListView.Items[0].FindControl("MyRadioButtonList") as RadioButtonList;
myRadioButtonList.SelectedIndex = 0;
As you can see you have to access the particular RadListView Item via the Items collection of the control. Once you have the item you're interested in you can just use the FindControl() method which takes the ID of your control as a string.
As for the validation part this is a possible implementation:
ASPX:
<asp:CustomValidator ID="RadioButtonListValidator" runat="server" ControlToValidate="MyRadioButtonList"
OnServerValidate="RadioButtonListValidator_ServerValidate"
ErrorMessage="Please select I Accept">
</asp:CustomValidator>
C#:
protected void RadioButtonListValidator_ServerValidate(object sender, ServerValidateEventArgs e)
{
RadListView myListView = MyRadListView;
RadioButtonList myRadioButtonList = myListView.Items[0].FindControl("MyRadioButtonList") as RadioButtonList;
myRadioButtonList.SelectedIndex = 0;
if (myRadioButtonList.SelectedValue != "1")
{
e.IsValid = false;
}
}
That should take care of all ensuring that the "I Accept" RadioButton is selected upon PostBack.
精彩评论