Solved

DropDownList - First row must be empty

Posted on 2006-06-22
3
963 Views
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.


Peter
0
Comment
Question by:Contestoas
3 Comments
 
LVL 16

Accepted Solution

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

Regards,
NetSwap
0
 

Author Comment

by:Contestoas
Comment Utility
Hi
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 .

Peter
0
 
LVL 3

Expert Comment

by:rstrader
Comment Utility
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

        Try
           myConnection.Open()

           myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

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

    End Sub
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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