Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to display "Please select....." as the first item in the dropdownlist while populating them from server side.

I have this code behind
C#
ddldept.DataSource = emp.GetDepartments();
               ddldept.DataTextField = "deptid";
               ddldept.DataValueField = "deptname";
               ddldept.DataBind();


i am getting bit confusion please help

regards
sreekanth
Posted

Add below code after you bind your dropdown:

C#
ddldept.DataSource = emp.GetDepartments();
               ddldept.DataTextField = "deptid";
               ddldept.DataValueField = "deptname";
               ddldept.DataBind();
ddldept.Items.Insert(0, new ListItem("--Please Select--", "0"));
 
Share this answer
 
v2
here is the code

C#
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
 
Share this answer
 
v2
Comments
Vani Kulkarni 27-Jun-12 6:35am    
Ditto! 5!
Rahul Rajat Singh 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.
Try this
C#
DropDownList1.Items.Insert(0,new ListItem("Please select"));
 
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