将列表项添加到下拉列表 [英] Add listitem to a dropdownlist
问题描述
大家好,
我一直试图在我的下拉列表中的项目顶部添加一个列表项.下拉列表绑定了一个数据源. VS忽略了我编写的每段代码.这是我到目前为止尝试过的内容:
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:
ddlRepUserCreated.Items.Insert(0, new ListItem("Please choose a user", String.Empty));
我在设计器中的代码:
My code in the designer:
<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>
即使我在设计器中有一个ListItem,我也无法在下拉菜单中说请选择用户".我知道我可以通过将其添加到数据库中来完成此操作,但我不想这样做.你们能告诉我如何添加这个项目吗?预先感谢.
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.
推荐答案
ConnectionStrings:RMA_DBConnectionStringNew %> " SelectCommand =" > < /asp:SqlDataSource >
ConnectionStrings:RMA_DBConnectionStringNew %>" SelectCommand="SELECT userFirstName + SPACE(1) + userSurname AS Expr1 FROM RMAUsers"> </asp:SqlDataSource>
即使我在设计器中有一个ListItem,我也无法在下拉菜单中说请选择用户".我知道我可以通过将其添加到数据库中来完成此操作,但我不想这样做.你们能告诉我如何添加这个项目吗?预先感谢.
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.
您好,
您的代码完全符合其要求;).
您将在下拉列表的INDEX 0处插入带有空字符串的新ListItem.
这应该可以工作:
ddlRepUserCreated.Items.Add(请选择一个用户");
无论如何,我会删除您的
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
<asp:ListItem>Please choose a user</asp:ListItem>
</asp:DropDownList>
并从SQL检索数据后,通过代码将其添加(请参见obove items.Add(")).
您的Dropdownlist属性中也缺少AppendDataBoundItems ="true".
问候
and add it through code (see obove items.Add("")) after retrieving Data from SQL.
AppendDataBoundItems="true" is also missing in your Dropdownlist Properties.
Regards
在您的下拉列表标签上添加AppendDataBoundItems="true"
Add AppendDataBoundItems="true"
on your dropdownlist tag
这篇关于将列表项添加到下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!