Search code examples
asp.netvalidationrequiredfieldvalidator

asp.net requiredfieldvalidator to depend from label text


I have two controls on a page, label and dropdownlist. I have a requiredfieldvalidator on the dropdownlist, but what I want to achieve is that if the label has some text than there is no need for selection from the dropdownlist. If the label doesn't have any content than the dropdownlist requiredfieldvalidator should work. Any idea how I can solve this? Thanks in advance Laziale


Solution

  • If the text is changing with your C Sharp code, then you can do like this.

    if (lblText.Text == "") // your condition
      RequiredFieldValidator1.Enabled =true;
    else
      RequiredFieldValidator1.Enabled =false;
    

    If it is changing in client side, better to write some custom javascript code and do the validation after checking the condition.

    Hope it helps.