【问题标题】:ASP.NET, Telerik RadListView, RadioButtonList and CustomValidator problemASP.NET、Telerik RadListView、RadioButtonList 和 CustomValidator 问题
【发布时间】:2011-07-03 17:48:50
【问题描述】:

我正在开发一个 ASP.NET,我有一个 RadListView (Telerik)。在每个 RadListView 的项目中都有一个带有两个单选按钮的 RadioButtonList。我需要做的是:

  • 在第一次加载页面时,必须默认选中两个单选按钮之一;
  • 在回发时,我必须检查用户是否选择了另一个(尝试使用 CustomValidator);
  • 在回发时,我必须保持 RadioButtonLists 的状态。

知道我该怎么做吗?

这是我的部分代码:

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

感谢任何帮助(甚至是教程链接)

提前致谢, 丹尼尔

【问题讨论】:

    标签: asp.net listview telerik radiobuttonlist customvalidator


    【解决方案1】:

    为了完成第一步,您可以遵循您在上面的代码中发布的想法(所选 RadioButton 的声明性设置),也可以通过执行以下几行的操作以编程方式设置它:

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

    如您所见,您必须通过控件的 Items 集合访问特定的 RadListView 项。获得您感兴趣的项目后,您可以使用 FindControl() 方法,该方法将控件的 ID 作为字符串。

    至于验证部分,这是一个可能的实现:

    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;
            }
        }
    

    这应该确保在回发时选择“我接受”单选按钮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-09
      • 2011-09-24
      • 1970-01-01
      • 2021-09-23
      • 2017-01-06
      • 1970-01-01
      • 2012-07-11
      相关资源
      最近更新 更多