DropDownList - First row must be empty

Posted on 2006-06-22
Last Modified: 2012-06-27
Hi !
I'm using ASP.NET 2.0 and VS 2005

In my form I have a DropDownList that is populated with a SQLDataSource SELECT DISTINCT ......etc.

I need the DropDownList to have one empty row first so the user does not have to make a selection.

Question by:Contestoas
LVL 16

Accepted Solution

Swapnil Piparia earned 250 total points
ID: 16958612
Hi Contestoas,
you can add dropdown item after pupulating it as following
dropdownlist1.items.insert(0," ")


Author Comment

ID: 16958800
I Tried putting the code in Page_Load Event but the empty row did not show !
On what event should I put the code??

The DropDownList is bound directly to a SQLDataSource object on the page .....
.... so I guess that I should add the code after the SQLDataSource has finished poplulating the DropDownList .


Expert Comment

ID: 16958911
page_load is the right spot, and your right it would need to happen after the SQLDataSource databind - I'd also check postback and only populate the list once on initial page load, here is a sample

 Sub Page_Load(sender As Object, e As EventArgs)
      If Not Page.IsPostBack Then
          Dim myConnection As SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings("pubs"))

        Dim myCommand As SqlCommand = new SqlCommand("Select pub_id, pub_name From publishers", myConnection)
        Dim myDataReader As SqlDataReader


           myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

           DropDownList1.DataSource = myDataReader
        Catch myException As Exception
           Response.Write("An error has occurred: " & myException.ToString())
           If Not myDataReader Is Nothing Then
           End If
              DropDownList1.Items.Insert(0, " ")
           DropDownList1.SelectedIndex = 0
        End Try
      End If

    End Sub

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Execute Stored Procedure for a set of values 5 44
LINQ - C# to VB convertion 12 46
Host pages 5 25
Error on Add method 1 38
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now