Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 560
  • Last Modified:

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

0
adamtrask
Asked:
adamtrask
  • 6
  • 5
  • 2
  • +2
1 Solution
 
Paul JacksonCommented:
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

0
 
cb1393Commented:
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

0
 
CodeCruiserCommented:
Try adding a

SelectedIndex = -1

in the markup of the list.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

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

How about using JavaScript to set the innerHTML to blank?

UsersList.innerHTML = '';
0
 
adamtraskAuthor Commented:
Sorry guys, the above hasn't worked!

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

0
 
adamtraskAuthor Commented:
Sorry guys... non of the above appears to work.
0
 
Paul JacksonCommented:
Is the default listitem being added?
Are the database values being populated in the dropdownlist?
0
 
adamtraskAuthor 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.
0
 
Paul JacksonCommented:
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.
0
 
adamtraskAuthor Commented:
If you mean this part:    Selected="true" then yes I did....
0
 
Paul JacksonCommented:
No I mean this : AppendDataBoundItems="true"

you should have something like this :
<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>


0
 
adamtraskAuthor Commented:
Wow...... thank!
I almost given up...
0
 
adamtraskAuthor Commented:
Thanks a lot
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now