Anthony Graglia
Anthony Graglia

Reputation: 5435

How to get ControlToValidate control inside validation function when a validation function is used for multiple controls

Ok,

This seems straight forward but I am having trouble finding the solution.

I have 10 PeopleEditor controls. Each one of the PeopleEditor controls has a CustomValidator and the ControlToValidate property is set to that specific PeopleEditor control. I assign a function to the control based on criteria.

It is possible that the same validation function is assigned to multiple CustomValidators which in turn means the function needs to know which ControlToValidate control it is validating.

Is this clear?

The question is: How do I reference the control from the ControlToValidate property in the validation function server side c# code?

Here are similar issues but they reference client side or inline validation: How to get the 'controlToValidate' property on ClientValidationFunction? and Extract value from control in ControlToValidate property in a CustomValidator control?

UPDATE: I have 10 of these in the .aspx page:

<asp:Label ID="lblPeople0" runat="server" />
<SharePoint:PeopleEditor ID="edtPeople0" SelectionSet="User,SecGroup" AutoPostBack="false" CausesValidation="false" PlaceButtonsUnderEntityEditor="false" Rows="3" AllowEmpty="true" ValidatorVisible="true" runat="server" MultiSelect="true" MaximumEntities="100" ShowCreateButtonInActiveDirectoryAccountCreationMode="true" />
<asp:CustomValidator id="vldPeople0" display="Dynamic" runat="server" ErrorMessage="Error Message." ControlToValidate="edtPeople0" />

In the .aspx.cs page, I assign the validating function like this:

vldPeople0.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(validate_ThisAndThat);

Then, I have this for the function and need to get the ControlToValidate in order get the ResolvedEntities from it.

/// <summary>
/// Validation function.
/// </summary>
private void validate_ThisAndThat(Object source, ServerValidateEventArgs args)
{
  foreach (PickerEntity entity in (ControlToValidate).ResolvedEntities)
  {
    String tmpPrincipalType = (entity.EntityData["PrincipalType"]).ToString();

    if (tmpPrincipalType == "User")
    {
      if ((entity.EntityData["DisplayName"]).ToString().Contains("aString"))
      {
        args.IsValid = false;
      }
    }
  }
}

Upvotes: 1

Views: 4708

Answers (2)

Anthony Graglia
Anthony Graglia

Reputation: 5435

Ok, so the key was to assign an attribute to the CustomValidator...

<asp:Label ID="lblPeople0" runat="server" />
<SharePoint:PeopleEditor ID="edtPeople0" SelectionSet="User,SecGroup" AutoPostBack="false" CausesValidation="false" PlaceButtonsUnderEntityEditor="false" Rows="3" AllowEmpty="true" ValidatorVisible="true" runat="server" MultiSelect="true" MaximumEntities="100" ShowCreateButtonInActiveDirectoryAccountCreationMode="true" />
<asp:CustomValidator id="vldPeople0" display="Dynamic" runat="server" ErrorMessage="Error Message." ControlToValidate="edtPeople0" />

In the .aspx.cs page, I assign the validating function like this:

vldPeople0.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(validate_ThisAndThat);
vldPeople0.Attributes.Add("peopleEditor", "edtPeople0");

Then, I have this for the function.

private void validate_ThisAndThat(Object source, ServerValidateEventArgs args)
{
  CustomValidator thisValidator = (CustomValidator) source;
  string strPeopleEditor = (string)thisValidator.Attributes["peopleEditor"];
  PeopleEditor peopleEditor = (PeopleEditor)panel1.FindControl(strPeopleEditor);

  foreach (PickerEntity entity in peopleEditor.ResolvedEntities)
  {
    String tmpPrincipalType = (entity.EntityData["PrincipalType"]).ToString();

    if (tmpPrincipalType == "User")
    {
      if ((entity.EntityData["DisplayName"]).ToString().Contains("aString"))
      {
        args.IsValid = false;
      }
    }
  }
}

Upvotes: 3

Brian Mains
Brian Mains

Reputation: 50728

Yes, it's possible to do. The OnServerValidate definition can point to the same method (handler). You can cast the sender of the ServerValidateEventArgs to find the object type and ID. Alternatively, give each validator its own callback, and define a central set of validation methods called from each of the callbacks. This way, the logic is in common methods is called for the appropriate handler.

HTH.

Upvotes: 0

Related Questions