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?
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();
}