开发者

how to apply custom validation on different controls in c#?

开发者 https://www.devze.com 2023-04-03 12:33 出处:网络
I am using Dropd开发者_开发百科ownBox and TextBox in my c# application. I need to validate that either DropdownBox item should be selected or TextBox should have the value

I am using Dropd开发者_开发百科ownBox and TextBox in my c# application.

I need to validate that either DropdownBox item should be selected or TextBox should have the value

But not both have value.

How can I apply validation on both together?


Something like this (with a CustomValidator control):

protected void cvName_ServerValidate(object source, ServerValidateEventArgs args)
{
  if (myDDL.SelectedItem == null && myTB.Text == "") //If nothing is selected in the drop down AND the text box is blank...
  {
    args.IsValid = false; // Set the validator to false
  }
}

The conditions on the if statement will depend on your validation requirements, but this gives a general idea of what to do. Hope it helps!

See this this post for some good details about validation.

NOTE: You don't need to assign a ControlToValidate to a CustomValidator. Just set the above function as the ServerValidate event for the CustomValidator and it will fire when the page submits.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号