如何将项目添加到一个下拉在asp.net列表 [英] How to add an item to a drop down list in asp.net

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

问题描述

我有以下代码

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();
        }
    }



我要添加添加新的在具体指数

I want to add the "Add new" at a specific index

但我不知道

语法

推荐答案

试试这个,它会在索引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天全站免登陆