下拉列表默认值

发布于 2024-11-02 13:10:06 字数 620 浏览 0 评论 0原文

我的 WebUserControl 上有一对 ddlist。在我的 Page_Load 代码中,我输入了:

ddlDay.Items.Insert(0, new ListItem("Day", "0"));
    ddlMonth.Items.Insert(0, new ListItem("Month", "0"));

    CatalogAccess ca = new CatalogAccess();
    ddlStudents.Items.Insert(0, new ListItem("Choose", "0"));
    ddlStudents.DataSource = ca.GetStudents();
    ddlStudents.DataTextField = "FullName";
    ddlStudents.DataValueField = "UserID";
    ddlStudents.DataBind();

它对于这些未数据绑定的 ddl 效果很好。这个>

ddlStudents.Items.Insert(0, new ListItem("Choose", "0"));

根本不起作用。每次我只得到学生的名字。如何解决这个问题。

There's a pair of ddlists on my WebUserControl. In My Page_Load code I have typed:

ddlDay.Items.Insert(0, new ListItem("Day", "0"));
    ddlMonth.Items.Insert(0, new ListItem("Month", "0"));

    CatalogAccess ca = new CatalogAccess();
    ddlStudents.Items.Insert(0, new ListItem("Choose", "0"));
    ddlStudents.DataSource = ca.GetStudents();
    ddlStudents.DataTextField = "FullName";
    ddlStudents.DataValueField = "UserID";
    ddlStudents.DataBind();

It works good to these ddl which are not databined. This one >

ddlStudents.Items.Insert(0, new ListItem("Choose", "0"));

doesnt work at all. Every time I am getting just the names of the students. How to fix this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

困倦 2024-11-09 13:10:06

您需要在 ddlStudents 下拉列表中设置 AppendDataBoundItems="true"

You need to set AppendDataBoundItems="true" on your ddlStudents dropdown.

长安忆 2024-11-09 13:10:06

我相信您还应该能够在数据绑定之后移动空白行的插入。

ddlDay.Items.Insert(0, new ListItem("Day", "0"));
ddlMonth.Items.Insert(0, new ListItem("Month", "0"));

CatalogAccess ca = new CatalogAccess();
ddlStudents.DataSource = ca.GetStudents();
ddlStudents.DataTextField = "FullName";
ddlStudents.DataValueField = "UserID";
ddlStudents.DataBind();
ddlStudents.Items.Insert(0, new ListItem("Choose", "0"));

希望这有帮助。

I believe that you should also be able to move the insert of the blank row after the databind.

ddlDay.Items.Insert(0, new ListItem("Day", "0"));
ddlMonth.Items.Insert(0, new ListItem("Month", "0"));

CatalogAccess ca = new CatalogAccess();
ddlStudents.DataSource = ca.GetStudents();
ddlStudents.DataTextField = "FullName";
ddlStudents.DataValueField = "UserID";
ddlStudents.DataBind();
ddlStudents.Items.Insert(0, new ListItem("Choose", "0"));

Hope this helps.

不…忘初心 2024-11-09 13:10:06

您可以通过 ca.GetStudents() 返回的 Students 集合。

You can through the collection of Students that ca.GetStudents() returns.

吐个泡泡 2024-11-09 13:10:06
<asp:DropDownList id="ddlStudents" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Selected="true" Text="Choose" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList id="ddlStudents" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Selected="true" Text="Choose" Value="0"></asp:ListItem>
</asp:DropDownList>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文