DropDownList Validation problem in ASP.NET

Suppose in your application you are using a DropDownList in a Form and depending upon the item selection you are showing/hiding some views inside a Multiview and you want that the user must select one of the item from the DropDownList before submitting a Form.

So you can write code like the following to validate the DropDown :

 
    
 
    
 
    
 
    
 

 
 

This code will work fine but it has an issue. The issue is – If page validation returns false and after that you are trying to change the DropDown’s index then it will not work for first time.
Because when you submit the form it will do Form validation. If Validation returns false [indicates not to submit the Form], then you can can’t go to server side code. The problem lies here. Since you have used “SelectedstateChanged” event for the Dropdown, the code inside the event handler function will not execute after form validation[when validation will return false].

So How to handle this problem. The solution is very simple, you have to do the following things –

Make CausesValidation=”false” for the DropDown.

Remove any validation group for that DropDown, i.e ValidationGroup=”none”

Handle client side change event to avoid Blocking of Submit on DropDown item change, i.e

onchange="Page_BlockSubmit = false;"

So  the final code would look like :

 
    
 
    
 
    
 
    
 

 

150 150 Burnignorance | Where Minds Meet And Sparks Fly!