Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have been trying to add a list item to the top of the items in my drop down list. The drop down list has a data source binded to it. VS ignores every piece of code that I have written. Here what i have tried so far:
C#
ddlRepUserCreated.Items.Insert(0, new ListItem("Please choose a user", String.Empty));

My code in the designer:
XML
<asp:DropDownList ID="ddlRepUserCreated" runat="server"
                                    DataSourceID="dsRepUserCreated" DataTextField="Expr1" DataValueField="Expr1">
                                    <asp:ListItem>Please choose a user</asp:ListItem>
                                </asp:DropDownList>
                    <asp:SqlDataSource ID="dsRepUserCreated" runat="server"
                        ConnectionString="<%$ ConnectionStrings:RMA_DBConnectionStringNew %>"
                        SelectCommand="SELECT userFirstName + SPACE(1) + userSurname AS Expr1 FROM RMAUsers">
                    </asp:SqlDataSource>


Even I have a ListItem in the designer, i can not get to have an item in my drop down saying "Please choose a user". I know i can accomplish this by adding this to the database but I do not want to do that. Can you guys please advise how i can add this item? Thanks in advance.
Posted

Hi,

your code does exactly what it should like ;).
you are inserting a new ListItem with an empty string at INDEX 0 of your dropdownlist.

This should work:
ddlRepUserCreated.Items.Add("Please choose a user");

Anyway I would remove your

C#
<asp:ListItem>Please choose a user</asp:ListItem>
                                </asp:DropDownList>


and add it through code (see obove items.Add("")) after retrieving Data from SQL.

EDIT: AppendDataBoundItems="true" is also missing in your Dropdownlist Properties.

Regards
 
Share this answer
 
v3
Comments
Tech Code Freak 9-Mar-12 1:22am    
5up!
wonder-FOOL 9-Mar-12 17:43pm    
Thanks. AppendDataBoundItems="true" solved it for me.
Add AppendDataBoundItems="true" on your dropdownlist tag
 
Share this answer
 
Comments
El_Codero 8-Mar-12 21:30pm    
sorry for this, I had the improve solution page opened for a while, didn't see your answer. my 5. Best Regards
Tech Code Freak 9-Mar-12 1:28am    
5up!
wonder-FOOL 9-Mar-12 17:43pm    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900