1
Reply

Custom Validator

scampercat

scampercat

Oct 28 2011 11:11 AM
1.4k
I have setup a custom validator on a checkbox list 2010 C# asp.net control. The custom control does 'work', but I still have a question about it. When an item is not selected from the checkboxlist control, the OnServerValidate code is executed once the 'submit' button is clicked.
Is there a way to display the error message once the foucs has moved from the checkboxlist control to the next item on the web page? If so, can you explain how this would occur and point me to a reference that would explain this process?
I have one additional question that is once the OnServerValidate event is fired, the error message does not display. The display occurs once I have code in the 'submiton event' that says if (!Page.IsPostBack) return; I would think once the OnServerValidate event finishes executing, I error message should be displayed. The error message should not wait until the logic in the submit button fires. Thus can you tell me if this is ok and why? if this is not ok, can you tell me why not and possibly point me to a reference to solve this issue?
The following is the code I am referring to:
<asp:CustomValidator ID="CustomValidatorContractNumber" runat="server" CssClass="errorStyle"
OnServerValidate="ValidateNumber" ValidateEmptyText="True"
EnableClientScript="False"
ErrorMessage="You must select at least one item.">
</asp:CustomValidator>

public void ValidateNumber(Object source, ServerValidateEventArgs args)
{
args.IsValid = false;
for (int i = 0; i < ChkBoxLstPlan.Items.Count; i++)
{
if (ChkBoxLstPlan.Items[i].Selected)
{
args.IsValid = true;
}
}

}

protected void submitbutton(object sender, EventArgs e)
{


if (!Page.IsPostBack)
return;
}







Answers (1)