ASP.NET Validate a label
What is the easiest way t开发者_高级运维o validate a label (automatically shows total of shopping cart) using the asp.net validation controls??
Thank you
Im not 100% sure why a label needs to be checked, but using a Custom Validator will be the way you need to go. You can write custom code to do almost anything you want. A Good tutiorial on a Basic Custom Validator can be found here.
Ok , I don't have comment permissions yet, but I assume you want to have the total of shopping cart to display > 0.
in the code behind, using a <asp:CustomValidator ID="Validator1" runat="server">
control,
if (!(String.IsNullOrEmpty(labelCart.Text)))
{
try
{
int total = int.Parse(labelCart.Text);
if (!(total <= 0))
{
Validator1.ErrorMessage = "You cannot enter less than 0 products to your shopping cart";
Validator1.IsValid = false;
}
}
catch {}
}
精彩评论