So here is the issue. I have a DropDownList in a .ascx control with a CustomValidator on it. This control has a ValidationGroup set by a property in the PageLoad. The button that triggers the validation is in a another file that uses this control. The problem is that the server side validation is never getting fired. I add breakpoints in it and they are never hit. Anyone have any thoughts about what is going on? Here is the code:
Dropdown.ascx:
<asp:DropDownList ID="ddlQuestions" runat="server">
</asp:DropDownList>
<asp:CustomValidator runat="server" ID="cvddlQuestions" OnServerValidate="cvddlQuestions_ServerValidate"
ErrorMessage="* Parent question is required." ValidateEmptyText="true" Display="Dynamic" />
</p>
</div>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
cvddlQuestions.ValidationGroup = ValidationGroup;
//ValidationGroup is a property in the parent file that is being set on PageLoad
}
protected void cvddlQuestions_ServerValidate(object sender, ServerValidateEventArgs args)
{
args.IsValid = false;// (ddlQuestions.SelectedValue != "-1");
}
In the parent file that uses Dropdown.ascx I have this button:
<asp:LinkButton ID="btnQuestionAdd" runat="server" OnClick="btnQuestionAdd_Click"
ValidationGroup="editQuestion" CommandName="add" />
Parent files code behind:
protected void btnQuestionAdd_Click(object sender, EventArgs e)
{
Page.Validate("editQuestion");
if (Page.IsValid) //ALWAYS SEEMS TO BE TRUE
{
//Do something
}
}
When I add a client validation function to the customvalidator it fires no problem. But the page is ALWAYS returning as valid. I have tried adding a ControlToValidate and setting ValidateEmptyText=”true” and I still get the same result. What am I missing?
It turns out that the answer was something incredibly stupid. When I instantiated the control that all this code was within I had to set the ValidationGroup property on the front end rather than the back end.
Lets say the code I posted was within a .ascx file called QuestionControl.ascx. After registering the control on my page I had to write the following:
No idea why saying:
didn’t work since the property was only a string.