Click here to Skip to main content
11,720,785 members (71,545 online)
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 0:26am
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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 2,179
1 OriginalGriff 924
2 F-ES Sitecore 640
3 Maciej Los 609
4 Richard MacCutchan 540


Advertise | Privacy | Mobile
Web03 | 2.8.150901.1 | Last Updated 27 Jun 2012
Copyright © CodeProject, 1999-2015
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