Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code as follows

Student ID Dropdownlist1

Course Dropdownlist2

Course timings Dropdownlist3


i want to validate if any of the above 3 Dropdownlist is not selected want to show the message "Please select"


for that how can i validate drodpownlist in asp.net using c#.


Regards,
Narasiman P.
Posted
Comments
♥…ЯҠ…♥ 12-Nov-13 22:59pm    
Show the message where? and you want common function to validate all the drop down list? where you wanna validate in button click event or where?

Hi Narasiman,

To validate the dropdownlist selection property you can use this function
C#
protected void ValidateDDL(DropDownList ddl)
        {
            if (ddl.SelectedIndex == -1) //Customize index value 
            {
                //Throw your error here
            }
        }
Function Call:

ValidateDDL(Dropdownlist1);
ValidateDDL(Dropdownlist2);
ValidateDDL(Dropdownlist3);

Same can also validate using Javascript, it depends on you...
C#
function validate()
{
   if(document.getElementById("ddlList").value == "")
   {
      alert("Please select value"); // prompt user
      document.getElementById("ddlList").focus(); //set focus back to control
      return false;
   }
}

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
Check out this..
This will help you.
Link 1 [^]
Link 2 [^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900