Passenger
Passenger

Reputation: 107

about the use of user control inside the aspx page

I have one UserControl containing one dropdown list and one textbox.This usercontrol i'm using in my my web application inside aspx page. So i have problem that if I put some text inside the textbox inside the usercontrol then the requiredfieldvalidator for the dropdown list should be enabled at runtime otherwise it should be disabled.

Any help will be highly appreciable.....

Upvotes: 0

Views: 119

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460268

Since you want the RequiredFieldValidator to be active only if the user entered text into the TextBox i would recommend to use a CustomValidator instead.

void ServerValidation (object source, ServerValidateEventArgs args)
{     
   args.IsValid = TextBox1.Text.Length == 0 || DropDownList1.SelectedIndex != -1; 
}

It is possible to use a CustomValidator control without setting the ControlToValidate property.

A possible clientvalidation-function:

<script language="javascript">
   <!--
   function ClientValidate(source, arguments)
   {
      var txt = document.getElementById('<%= TextBox1.ClientID %>');
      var ddl = document.getElementById('<%= DropDownList1.ClientID %>');
      if (txt.length == 0)
         arguments.IsValid = true;
      else
         arguments.IsValid = ddl.selectedIndex >= 0;
   }
   // -->
</script>

You have to register it on the validator via ClientValidationFunction property:

<asp:CustomValidator Id="CustomValidator1" runat="server"
    ClientValidationFunction="ClientValidate" 
    OnServerValidate="ServerValidation">
</asp:CustomValidator>

Upvotes: 1

qamar
qamar

Reputation: 1447

set CausesValidation property to false, then validation won't fire by the dropdownlist.

Upvotes: 0

Related Questions