Hi irshad1231,
Since ControlToValidate is assigned as TextBox, CustomValidator will only fire when the TextBox is not empty.
So the work around for this is to set CustomValidator ValidateEmptyText property to true.
Check this example. Now please take its reference and correct your code.
For demonstration i have used TextBox instead of Label.
HTML
<asp:TextBox runat="server" ID="lbl_iscitizen" Text="true" /><br />
<%--<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" ValidateEmptyText="true" OnServerValidate="cv_name_ServerValidate"
ValidationGroup="validate" ForeColor="Red"></asp:CustomValidator><br />
<asp:Button ID="submitButton" runat="server" OnClick="submitButton_Click" Text="Submit"
ValidationGroup="validate" />
C#
protected void cv_name_ServerValidate(object source, ServerValidateEventArgs args)
{
if (lbl_iscitizen.Text.ToLower() == "true")
{
args.IsValid = true;
}
else
{
args.IsValid = false;
}
}
protected void submitButton_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
submitButton.Text = "Valid";
}
else
{
submitButton.Text = "Invalid!";
}
}
VB.Net
Protected Sub cv_name_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
If lbl_iscitizen.Text.ToLower() = "true" Then
args.IsValid = True
Else
args.IsValid = False
End If
End Sub
Protected Sub submitButton_Click(ByVal sender As Object, ByVal e As EventArgs)
If Page.IsValid Then
submitButton.Text = "Valid"
Else
submitButton.Text = "Invalid!"
End If
End Sub
Screenshot