简体   繁体   English

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

[英]How do I add an ASP.NET control inside an ASCX to an external RequiredFieldValidator programmatically?

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. 我在用户控件(ASCX)中有一个下拉列表,我想从我放置ASCX的页面进行验证,但是当我将ControlToValidate设置到下拉列表时,页面会抱怨它可以'找到了。 Thanks for any help/suggestions. 感谢您的任何帮助/建议。

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: 然后使用公开的Dropdown的UniqueID将控件设置为在删除用户控件的页面的页面加载中进行验证:

protected void Page_Load(object sender, EventArgs e)
{

    RequiredFieldValidator1.ControlToValidate = WebUserControl1.DropDownToValidate.UniqueID;
}

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. 不太一样,但这是我所知道的唯一解决方法。

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). 其中reqRecipientNamereqRecipientMail ...是验证器的ID(它们也是insice ascx)。 And then on Page inside submit method call controlId.Validate(); 然后在Page里面提交方法调用controlId.Validate(); This works for me. 这适合我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ascx控件的asp.net中添加表单和类标记 - How to add form and class tag in asp.net in ascx control ASP.NET-如何将Web用户控件(.ascx)打包到DLL中以供其他应用程序使用? - ASP.NET - How do I package a Web User Control (.ascx) in a DLL for use in other applications? 带有自定义控件的asp.net RequiredFieldValidator - asp.net RequiredFieldValidator with custom control 我应该创建多个ASP.Net ASCX控件还是创建多个控件的单个控件? - Should I create multiple ASP.Net ASCX Controls or a single Control to do multiple things asp.net RequiredFieldValidator - asp.net RequiredFieldValidator 如何生成PDF并加载ASCX asp.net用户控件的内容? - How can I generate a PDF and load the contents of an ASCX asp.net user control? 如何从客户端.ascx使用控件ID并在服务器端ascx.cs(ASP.NET)中使用它 - How to use control ID from clientside .ascx and use it in serverside ascx.cs (ASP.NET) 如何使用包含验证文本框值的RequiredFieldValidator控件的asp.net创建Web服务器控件 - how to create web server control using asp.net that contain RequiredFieldValidator control that validate textbox value 在gridview的ASCX控件内,使用控件上的Javascript显示隐藏。 (ASP.NET + Javascript) - Show Hide using Javascript on a control, inside a ASCX control in a gridview. (ASP.NET + Javascript) 在ASP.NET C#中使用RequiredFieldValidator控件的问题 - Problem in using RequiredFieldValidator control in asp.net c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM