Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a dropdownlist that is bound to data in the code behind.
i want the default to be -- None -- at the top of the list.
if the user select the top of list i.e --None-- it should prompt as pl z select from the list ,
if user select the other than --None-- then it should continue the process

i have added the top list as
drop down.Items.Insert(0, new ListItem("-- None--", "")) 


how to validate

thanks in advance
Posted
Updated 6-Mar-12 5:07am
v5
Comments
[no name] 6-Mar-12 10:32am    
Is it a Web Project or a windows Project?

Please tag the question with proper areas.
pradeep manne 6-Mar-12 10:34am    
hi,
its a web project

use this one.
C#
drop down.Items.Insert(0, new ListItem("-- None--", "-- None--"));
 
Share this answer
 
v2
C#
dropdownlist1.Items.Insert(0, new ListItem("None", "0"));


for validating you can use javascript for example


C#
try
{
  if(dropdownlist1.SelectedValue == "0")
  {
     throw new Exception("<script language="javascript">" + "alert('Please select something');" + "");
  }
}

catch (Exception ex)
{
   label1.Text = ex.Message;
}</script>
 
Share this answer
 
C#
function validateDropDown(){
       if(document.getElementById(<%=ddl.ClientID%>).value=='0')
       {
           alert('please select valid item.');
           return false;
       }
   }
 
Share this answer
 
Set the AppendDataBoundItems property in the markup and add an ListItem. Add a RangeValidator and have it validate the DropDownList. If the value for the ListItem added in the markup is -1, for instance, then you would set the MinValue for the RangeValidator to 0.
 
Share this answer
 

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