We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Hiding first row in DropDownList

Medium Priority
572 Views
Last Modified: 2012-06-21
Hi,

I am using mark ups to create a DropDownList on an Asp.Net form.

Every thing is working fine except for one nagging problem. The first row shows up in the text field of the DropDownList.

 When using regular VB code I usually insert these two lines of code to make the first row blank:

DropDownList.Items.Insert(0, New ListItem(String.Empty, String.Empty))
        DropDownList.SelectedIndex = 0

But in the mark ups on the aspx page I don't know how to do some thing similar.

Below is the code for the DropDownList and its SqlDataSource:

Thanks
<asp:DropDownList ID="UsersList"  AutoPostBack="True" 
        DataSourceID="SqlDataSource1" DataTextField="UserName" DataValueField="NoteID" runat="server" 
        style="z-index: 1; left: 800px; top: 194px; position: absolute; width: 123px;" > </asp:DropDownList>

      <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ProviderName="System.Data.SqlClient"
        ConnectionString="<%$ ConnectionStrings:HelpDeskConnectionString %>" 
        SelectCommand="Select Users.UserName,Notes.NoteID from Users join Notes on Users.userID= Notes.userID where Notes.dDate=@dDate">
        <SelectParameters>
            <asp:ControlParameter ControlID="txtDate" DbType="Date" Name="dDate" 
                PropertyName="Text" />
        </SelectParameters>
    </asp:SqlDataSource>

Open in new window

Comment
Watch Question

Paul JacksonSoftware Engineer
CERTIFIED EXPERT
Top Expert 2011

Commented:
Add a blank listitem and set the AppendDataBoundItems attribute to true

<asp:DropDownList ID="UsersList"  AutoPostBack="True"  
        DataSourceID="SqlDataSource1" DataTextField="UserName" DataValueField="NoteID" runat="server"  
        style="z-index: 1; left: 800px; top: 194px; position: absolute; width: 123px;" AppendDataBoundItems="true">
<asp:ListItem Text="" Value =0/>
 </asp:DropDownList> 

Open in new window

Commented:
There may be other ways, but I set the AppendDataBoundItems attribute to True for the dropdown, then add a blank option like so:
<asp:DropDownList AppendDataBoundItems="true">
<asp:ListItem Text="Select an option" Value=""></asp:ListItem>
</asp:DropDownList>

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Try adding a

SelectedIndex = -1

in the markup of the list.

Commented:
How about setting the text property to "" on page_load

UsersList.Text = ""

Commented:
Oops... you want to do this on the markup file?!?

How about using JavaScript to set the innerHTML to blank?

UsersList.innerHTML = '';

Author

Commented:
Sorry guys, the above hasn't worked!

rick_gwu: Should I place your JavaScript code inside script tags?
Paul JacksonSoftware Engineer
CERTIFIED EXPERT
Top Expert 2011

Commented:
Add the Selected attribute to the default listitem :
<asp:DropDownList ID="UsersList"  AutoPostBack="True"   
        DataSourceID="SqlDataSource1" DataTextField="UserName" DataValueField="NoteID" runat="server"   
        style="z-index: 1; left: 800px; top: 194px; position: absolute; width: 123px;" AppendDataBoundItems="true"> 
<asp:ListItem Text="Select an item" Value ="0" Selected="true"/> 
 </asp:DropDownList>  

Open in new window

Author

Commented:
Sorry guys... non of the above appears to work.
Paul JacksonSoftware Engineer
CERTIFIED EXPERT
Top Expert 2011

Commented:
Is the default listitem being added?
Are the database values being populated in the dropdownlist?

Author

Commented:
jacko72:  

I am not sure what you mean by the "default listitem", but the answer to the second question is yes.
The dropDownList is being populated by the values in the database. There is no problem in this area.
It all works well.
The only thing missing is to have the first row as blank as I said in the beginning. Thanks.
Paul JacksonSoftware Engineer
CERTIFIED EXPERT
Top Expert 2011

Commented:
By the default listitem i mean the one setup in the markup.
So it would seem this is still being overriden by the values being loaded from the database.
Are you sure you have added the AppendDataBoundItems = "true" to the dropdownlistbox markup.
Are you possibly binding the dropdownlist box in the code-behind anywhere that might be overwriting it?
It would be useful if you posted your code  as you have it now so we can try to see what is wrong.

Author

Commented:
If you mean this part:    Selected="true" then yes I did....
Software Engineer
CERTIFIED EXPERT
Top Expert 2011
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Wow...... thank!
I almost given up...

Author

Commented:
Thanks a lot
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.