如何在ASP.NET的下拉列表中添加项目? [英] How to add an item to a drop down list in ASP.NET?

查看:84
本文介绍了如何在ASP.NET的下拉列表中添加项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加添加新在特定的索引,但我不确定语法。我有以下代码:

I want to add the "Add new" at a specific index, but I am not sure of the syntax. I have the following code:

protected void Page_Load(object sender, EventArgs e)
{
        DRPFill();
        if (!IsPostBack)
        {
            DropDownList1.Items.Add("Add New");
        }
}
public void DRPFill()
{
    if (!IsPostBack)
    {
        //Object
        AddMajor objMajor = new AddMajor();

        //Data Table
        DataTable dtMajor = objMajor.find();

        //Data Source
        DropDownList1.DataSource = dtMajor;
        DropDownList1.DataValueField = "MajorID";
        DropDownList1.DataTextField = "MajorName";

        //Data Bind
        DropDownList1.DataBind();
    }
}


推荐答案

尝试这样,它将在索引0处插入列表项;

Try this, it will insert the list item at index 0;

DropDownList1.Items.Insert(0, new ListItem("Add New", ""));

这篇关于如何在ASP.NET的下拉列表中添加项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆