下拉列表问题

发布于 2024-09-06 20:16:16 字数 473 浏览 2 评论 0原文

我尝试向下拉列表中添加一个新项目。我希望将添加的项目放置在下拉列表的第一个位置。我该怎么做

   I want this to be a first option
   ListItem itemspecial = new ListItem();
   itemspecial.Value = "-1";
   itemspecial.Text = "Please select specialization";
   ddlSpecialization.Items.Add(itemspecial);

    ddlSpecialization.DataTextField = "Description";
    ddlSpecialization.DataValueField = "Id";
    ddlSpecialization.DataSource = objspecial.GetAll();
    ddlSpecialization.DataBind();

I try to add a new item to the drodownlist.I want the added item to be place at first position of a dropdownlist.How can I do that

   I want this to be a first option
   ListItem itemspecial = new ListItem();
   itemspecial.Value = "-1";
   itemspecial.Text = "Please select specialization";
   ddlSpecialization.Items.Add(itemspecial);

    ddlSpecialization.DataTextField = "Description";
    ddlSpecialization.DataValueField = "Id";
    ddlSpecialization.DataSource = objspecial.GetAll();
    ddlSpecialization.DataBind();

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

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

发布评论

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

评论(3

怪异←思 2024-09-13 20:16:16

在插入之前先进行数据绑定...

ddlSpecialization.DataTextField = "Description";
ddlSpecialization.DataValueField = "Id";
ddlSpecialization.DataSource = objspecial.GetAll();
ddlSpecialization.DataBind();

ddlSpecialization.Items.Insert(0, new ListItem("Please select specialization", "-1"));

Databind first before you insert...

ddlSpecialization.DataTextField = "Description";
ddlSpecialization.DataValueField = "Id";
ddlSpecialization.DataSource = objspecial.GetAll();
ddlSpecialization.DataBind();

ddlSpecialization.Items.Insert(0, new ListItem("Please select specialization", "-1"));
无所谓啦 2024-09-13 20:16:16

您可以使用 DropDownListInsert 函数来完成此操作:

ListItem itemspecial = new ListItem();
itemspecial.Value = "-1";
itemspecial.Text = "Please select specialization";
ddlSpecialization.Items.Insert(0, itemspecial);

ddlSpecialization.DataTextField = "Description";
ddlSpecialization.DataValueField = "Id";
ddlSpecialization.DataSource = objspecial.GetAll();
ddlSpecialization.DataBind();

You can do this using the Insert function of the DropDownList:

ListItem itemspecial = new ListItem();
itemspecial.Value = "-1";
itemspecial.Text = "Please select specialization";
ddlSpecialization.Items.Insert(0, itemspecial);

ddlSpecialization.DataTextField = "Description";
ddlSpecialization.DataValueField = "Id";
ddlSpecialization.DataSource = objspecial.GetAll();
ddlSpecialization.DataBind();
殊姿 2024-09-13 20:16:16

使用
-- ddlSpecialization.Items.insertAt(itemspecial,0);<-指定第一个索引,

use
-- ddlSpecialization.Items.insertAt(itemspecial,0);<-Specify the first index,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文