Link to home
Start Free TrialLog in
Avatar of adamtrask
adamtrask

asked on

Hiding first row in DropDownList

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

Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

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

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

Try adding a

SelectedIndex = -1

in the markup of the list.
Avatar of Rick
Rick

How about setting the text property to "" on page_load

UsersList.Text = ""
Oops... you want to do this on the markup file?!?

How about using JavaScript to set the innerHTML to blank?

UsersList.innerHTML = '';
Avatar of adamtrask

ASKER

Sorry guys, the above hasn't worked!

rick_gwu: Should I place your JavaScript code inside script tags?
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

Sorry guys... non of the above appears to work.
Is the default listitem being added?
Are the database values being populated in the dropdownlist?
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.
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.
If you mean this part:    Selected="true" then yes I did....
ASKER CERTIFIED SOLUTION
Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wow...... thank!
I almost given up...
Thanks a lot