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
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(
DropDownList.SelectedIndex
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>
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>
Try adding a
SelectedIndex = -1
in the markup of the list.
SelectedIndex = -1
in the markup of the list.
How about setting the text property to "" on page_load
UsersList.Text = ""
UsersList.Text = ""
Oops... you want to do this on the markup file?!?
How about using JavaScript to set the innerHTML to blank?
UsersList.innerHTML = '';
How about using JavaScript to set the innerHTML to blank?
UsersList.innerHTML = '';
ASKER
Sorry guys, the above hasn't worked!
rick_gwu: Should I place your JavaScript code inside script tags?
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>
ASKER
Sorry guys... non of the above appears to work.
Is the default listitem being added?
Are the database values being populated in the dropdownlist?
Are the database values being populated in the dropdownlist?
ASKER
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.
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.
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.
ASKER
If you mean this part: Selected="true" then yes I did....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wow...... thank!
I almost given up...
I almost given up...
ASKER
Thanks a lot
Open in new window