Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
I want to display "Please select....." as the first item in the dropdownlist while populating them from server side.
 
I have this code behind
ddldept.DataSource = emp.GetDepartments();
               ddldept.DataTextField = "deptid";
               ddldept.DataValueField = "deptname";
               ddldept.DataBind();
 
i am getting bit confusion please help
 
regards
sreekanth
Posted 27-Jun-12 1:26am
sree561.1K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Add below code after you bind your dropdown:
 
ddldept.DataSource = emp.GetDepartments();
               ddldept.DataTextField = "deptid";
               ddldept.DataValueField = "deptname";
               ddldept.DataBind();
ddldept.Items.Insert(0, new ListItem("--Please Select--", "0"));
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

here is the code
 
ddldept.DataSource = emp.GetDepartments();
ddldept.DataTextField = "deptid";
ddldept.DataValueField = "deptname";
ddldept.DataBind();
ddldept.Items.Insert(0, new ListItem("Please select.....", "0"));
 
P.S. just make sure you handle this "Please select....." while validations
  Permalink  
v2
Comments
Vani Kulkarni at 27-Jun-12 6:35am
   
Ditto! 5!
Rahul Rajat Singh at 27-Jun-12 6:41am
   
i was actually planning to write "Ditto" under your answer. i guess that would have become double ditto :)
 
+5 to you too.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try this
DropDownList1.Items.Insert(0,new ListItem("Please select"));
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 27 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100