Solved

DropDownList - First row must be empty

Posted on 2006-06-22
3
977 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Accepted Solution

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

Regards,
NetSwap
0
 

Author Comment

by:Contestoas
ID: 16958800
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
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

        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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 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