ASP.NET、Telerik RadListView、RadioButtonList 和 CustomValidator 问题

发布于 2024-10-18 23:52:39 字数 1291 浏览 3 评论 0原文

我正在开发一个 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>

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

提前致谢, 丹尼尔

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感受沵的脚步 2024-10-25 23:52:39

为了执行第一步,您可以遵循您在上面的代码中发布的想法(所选 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 Item。一旦获得了您感兴趣的项目,您就可以使用 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;
        }
    }

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文