Validate empty TextBox on ASP.Net CustomValidator ServerValidate event using C# and VB.Net

Last Reply 11 months ago By dharmendr

Posted 11 months ago

Dear All,

I have created custom validator but it is not working on the condition 

if Iscitizen == true

then the textbox should be empty with no custom validator 

if Iscitizen == false

then the textbox custom validator is Required with text Required.

Thanks

<asp:Label ID="lbl_iscitizen" runat="server" Text="true"></asp:Label>
<asp:TextBox ID="lbl_iqamaexpiry" runat="server" Text=""  CssClass="textboxAslabel"></asp:TextBox>
<asp:CustomValidator ID="cv_name" runat="server" ControlToValidate="lbl_iqamaexpiry" Text="Required" OnServerValidate="cv_name_ServerValidate"
    ValidationGroup="validate" ForeColor="Red"></asp:CustomValidator>
<asp:Button ID="submitButton" runat="server" OnClick="submitButton_Click" Text="Submit" />

 

protected void cv_name_ServerValidate(object source, ServerValidateEventArgs args)
{
    if (lbl_iscitizen.Text == "true")
    {
        args.IsValid = false;
    }
    else
    {
        args.IsValid = true;
    }
}
protected void submitButton_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        submitButton.Text = "Valid";
    }
    else
    {
        submitButton.Text = "Invalid!";
    }
}

 

This question does not have replies that have been liked.