如何以编程方式将 ASCX 内的 ASP.NET 控件添加到外部RequiredFieldValidator?

发布于 2024-07-26 07:47:41 字数 108 浏览 7 评论 0原文

我在用户控件 (ASCX) 中有一个下拉列表,我想从放置 ASCX 的页面进行验证,但是当我将 ControlToValidate 设置为下拉列表时,页面抱怨说它不能找不到。 感谢您的任何帮助/建议。

I have a drop-down list inside a user control (ASCX) that I want to validate from the page on which I've placed the ASCX, but when I set the ControlToValidate to the drop-down list the page complains that it can't be found. Thanks for any help/suggestions.

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

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

发布评论

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

评论(3

遥远的她 2024-08-02 07:47:41

使用用户控件中的公共属性公开下拉列表:

public DropDownList DropDownToValidate
    {
        get
        {
            return ddlTest;
        }
    }

然后使用公开的下拉列表的 UniqueID 将控件设置为在您放置用户控件的页面的页面加载中进行验证:

protected void Page_Load(object sender, EventArgs e)
{

    RequiredFieldValidator1.ControlToValidate = WebUserControl1.DropDownToValidate.UniqueID;
}

Expose the dropdown list with a public property in your user control:

public DropDownList DropDownToValidate
    {
        get
        {
            return ddlTest;
        }
    }

Then use the UniqueID of the exposed Dropdown to set the control to validate in the page load of the page on which you dropped the user control:

protected void Page_Load(object sender, EventArgs e)
{

    RequiredFieldValidator1.ControlToValidate = WebUserControl1.DropDownToValidate.UniqueID;
}
无语# 2024-08-02 07:47:41

我知道执行此操作的唯一方法是在您的用户控件类中执行此操作:


[ValidationProperty("Foo")]
public class MyUserControl : UserControl
{
     public string Foo
     {
          get { return(yourDropDown.SelectedValue); }
     }
}

然后在您放置用户控件的页面中:


<asp:RequiredFieldValidator ControlToValidate="yourUserControlName" runat="server" ErrorMessage="You are required to make a selection" />

不完全一样,但这是我唯一的解决方法知道。

The only way I know to do this is to do this in your user control class:


[ValidationProperty("Foo")]
public class MyUserControl : UserControl
{
     public string Foo
     {
          get { return(yourDropDown.SelectedValue); }
     }
}

And then in the page you place the user control on:


<asp:RequiredFieldValidator ControlToValidate="yourUserControlName" runat="server" ErrorMessage="You are required to make a selection" />

Not quite the same thing, but that's the only workaround that I know.

久伴你 2024-08-02 07:47:41

我认为验证用户控件的最佳方法是在用户控件中使用公共方法:

public void Validate() {
  reqRecipientName.Validate();
  reqRecipientMail.Validate();
  valRecipientMail.Validate();
  reqRecipientPhone.Validate();
}

其中 reqRecipientNamereqRecipientMail... 是验证器的 ID(它们也是 insice ascx) )。
然后在Page里面的submit方法中调用controlId.Validate();
这对我有用。

I think the best way to validate user control is to have public method inside your user control:

public void Validate() {
  reqRecipientName.Validate();
  reqRecipientMail.Validate();
  valRecipientMail.Validate();
  reqRecipientPhone.Validate();
}

where reqRecipientName, reqRecipientMail... are ID of validators (they are also insice ascx).
And then on Page inside submit method call controlId.Validate();
This works for me.

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