3

I am using a user control (for phone number), and I have some validations inside it and this user-control is used at multiple places at the system, and I need to pass the ValidationGroup to it from the place where it is used, but this is NOT working (the validation not fired) and the user can submit the form without entering the phone number

Note: The submit button already has the same validation group

Here is the code of the user control

<div runat="server" ID="DivContainer" class="control-group">
<label class="control-label">Phone Number<span class="required">*</span></label>
<div class="row-fluid">
    <div class="span6">
        <asp:DropDownList runat="server" ID="DdlCountryPhoneCodes" CssClass="span12" />
    </div>
    <div class="span6">
        <input type="text" maxlength="50" cssclass="span12" runat="server" id="TxtPhoneNumber" />
    </div>
</div>
<div class="validation-message-wrapper">
    <asp:RequiredFieldValidator runat="server" CssClass="validation-message" ErrorMessage="Enter your phone number." ValidationGroup="<%= ValidationGroup %>" Display="Dynamic" ControlToValidate="TxtPhoneNumber" />
    <asp:CustomValidator runat="server" ID="CustomValidatorTxtPhoneNumber" ControlToValidate="TxtPhoneNumber" Display="Dynamic" CssClass="validation-message" ValidationGroup="<%= ValidationGroup %>" ClientValidationFunction="validatePhoneNumber" />
</div>

Here is an example of the usage of the user control

<uc1:RecoveryPhoneNumber runat="server" ID="ucRecoveryPhoneNumber" ValidationGroup="ManageAccountGroup" />

Q: How to make the validation work at any place the user-control used?

7
  • Have you tried setting up the ValidationGroup property of validators in Page_Load event in user control code behind? Commented Apr 1, 2019 at 10:45
  • No, I am passing the value of the ValidationGroup property from the place that uses it (like the example I wrote above). Commented Apr 1, 2019 at 10:47
  • Yes so your user control has a property ValidationGroup which you set on the page where user control is placed. Inside the usercontrol you have validator controls e.g. RequiredField and CustomValidator. On user control Page_Load event you can set the ValidationGroup property of each validator control to the user control ValidationGroup property e.g. CustomValidatorTxtPhoneNumber.ValidationGroup = this.ValidationGroup Commented Apr 1, 2019 at 10:51
  • I don't need to do that; as I am setting the value by this code (ValidationGroup="<%= ValidationGroup %>") inside the user control code Commented Apr 1, 2019 at 11:02
  • 1
    Ok. Kindly update your question mentioning that ValidationGroup is already set on button. Also, have you checked browser console for any errors. Commented Apr 1, 2019 at 11:14

1 Answer 1

1

You need a DataBinding expression. You are now simply adding the string <%= ValidationGroup %> as the ValidationGroup name, not the variable ValidationGroup. This is the correct way

ValidationGroup='<%# ValidationGroup %>'

And you need to call DataBind in the Page_Load of the UserControl.

protected void Page_Load(object sender, EventArgs e)
{
    DataBind();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.