gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:13:13 25 4
gpt4 key购买 nike

我有一个用户控件,它根据其他配置选择显示 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>

隐藏代码:

<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

另一个页面使用该用户控件,我希望有一个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>

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

enter image description here

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

最佳答案

这应该可以解决问题:

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

我的 Controller 很简单:

<asp:TextBox ID="TextBox2" runat="server" MaxLength="20"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>

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

关于asp.net - 用户控件中的控件是否可以由用户控件外部的验证器进行验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32318346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com