用户控件中的控件是否可以由用户控件外部的验证器进行验证?

Gqq*_*big 2 vb.net asp.net

我有一个用户控件,它根据其他配置选择显示 TextBox 或 DropdownList。

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="CustomRelationshipDropDown.ascx.vb" Inherits="CustomRelationshipDropDown" %>

<asp:TextBox ID="TextBox1" runat="server" MaxLength="20"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)

隐藏代码:

<ValidationProperty("Value")>
<SupportsEventValidation()>
Partial Class CustomRelationshipDropDown
    Inherits UserControl

    Public Property Value() As String
        Get
            If GetList("RelationshipList") Is Nothing Then
                Return TextBox1.Text
            Else
                Return DropDownList1.SelectedValue
            End If
        End Get
        Set(ByVal Value As String)
            If GetList("RelationshipList") Is Nothing Then
                TextBox1.Text = Value
            Else
                DropDownList1.SelectedValue = Value
            End If
        End Set
    End Property

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim list As List(Of String) = GetList("RelationshipList")

        If list IsNot Nothing Then
            For Each item In list
                DropDownList1.Items.Add(item)
            Next
            DropDownList1.Visible = True
            TextBox1.Visible = False
        Else
            DropDownList1.Visible = False
            TextBox1.Visible = True
        End If
    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)

另一个页面使用该用户控件,我希望有一个RequiredValidator 来验证文本框。

<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
    </div>
    <div>
        <uc1:CustomRelationshipDropDown ID="CustomRelationshipDropDown1" runat="server" />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="CustomRelationshipDropDown1"></asp:RequiredFieldValidator>
    </div>
    <asp:Button ID="Button1" runat="server" Text="Button" />
</form>
Run Code Online (Sandbox Code Playgroud)

当我单击 Button1 时,第二个验证器没有发现第二个文本框为空。

在此输入图像描述

我应该如何修改 CustomRelationshipDropDown 以便外部验证器可以验证其中的文本框?

Ziv*_*man 5

这应该可以解决问题:

 <div>
        <uc1:WebUserControl1 runat="server" ID="WebUserControl1" />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="RequiredFieldValidator2" ControlToValidate="WebUserControl1$TextBox2"></asp:RequiredFieldValidator>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="RequiredFieldValidator3" ControlToValidate="WebUserControl1$DropDownList1"></asp:RequiredFieldValidator>
 </div>
Run Code Online (Sandbox Code Playgroud)

我的控制器很简单:

<asp:TextBox ID="TextBox2" runat="server" MaxLength="20"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)

还尝试随机每个 Visible = false,并且它有效。