How can I add an ASP.NET control inside ASCX to an external RequiredFieldValidator programmatically?

I have a drop-down list inside a user control (ASCX) that I want to check on the page where I posted ASCX, but when I set ControlToValidate to the drop-down list, the page complains that it cannot be found. Thanks for any help / suggestions.

+3
source share
3 answers

Open the drop-down list with the public property in the user control:

public DropDownList DropDownToValidate
    {
        get
        {
            return ddlTest;
        }
    }

UniqueID , , :

protected void Page_Load(object sender, EventArgs e)
{

    RequiredFieldValidator1.ControlToValidate = WebUserControl1.DropDownToValidate.UniqueID;
}
+5

, , - :


[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" />

, , .

+3

, - :

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

reqRecipientName, reqRecipientMail... - ( insix ascx). controlId.Validate(); .

+1

Source: https://habr.com/ru/post/1712197/


All Articles