C# 如何在cs文件的下拉列表中添加项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11734683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:00:49  来源:igfitidea点击:

How to add Item in Dropdown list in cs file

c#asp.netsql-server-2008

提问by Venkatesh Chittepu

This is my CS file for dropdown:

这是我用于下拉菜单的 CS 文件:

protected void BindDropDownList()     

    {

        DataTable dt = new DataTable();
        string connString = System.Configuration.ConfigurationManager.AppSettings["EyeProject"];
        SqlConnection conn = new SqlConnection(connString);


        try
        {
            conn.Open();
            string sqlStatement = "SELECT FirstName FROM tbl_UserDetails";
            SqlCommand sqlCmd = new SqlCommand(sqlStatement, conn);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

            sqlDa.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                DropDownList1.DataSource =dt;


                DropDownList1.DataTextField = "FirstName"; // the items to be displayed in the list items

                DropDownList1.DataBind();
            }
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "No Data Found To display in the DropDown List";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            conn.Close();
        }


    }



By using this one iam getting values of table Firstname values now i want to add one more item Called ALLrecords. 

How can i add it.

this is my Aspx file 

 <div class="label">
                                    Select Name:
                                    <asp:DropDownList ID="DropDownList1" runat="server">

                                    </asp:DropDownList>
                                </div>

回答by Manish Parakhiya

Try this

尝试这个

 DropDownList1.Items.Add(new ListItem("All Record"));

and if you want to add item with value then

如果你想添加具有价值的项目,那么

 DropDownList1.Items.Add(new ListItem("All Record","0"));

 //or if you want to add at particular index then

 DropDownList1.Items.Insert(0,new ListItem("All Record"));// 0 is index of item

hope this helps.

希望这可以帮助。

回答by fengd

insert an item at specified index

在指定的索引处插入一个项目

DropDownListID.Items.Insert(0, new ListItem("Default text", "Default value")

回答by Vikash Singh

DropDownList1.Items.Insert(0,new ListItem("AllRecords","itsValue_on_dropdownlist")); // use 0 to show "ALLRecords" text on top in dropdownlist

i would suggest that you should bind values to dropdownlist also. like this -

我建议您也应该将值绑定到下拉列表。像这样 -

DropDownList1.DataValueField = "FirstName";

回答by y.k kumar

First write onLoad with "addItems" function in dropdownlist declaring tag .aspx file like this:

首先在下拉列表中使用“addItems”函数编写 onLoad 声明标记 .aspx 文件,如下所示:

then create "addItems" function in cs file.

然后在 cs 文件中创建“addItems”函数。

<asp:DropDownList ID="DropDownList1" runat="server" OnLoad="addDeleteItems" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">

protected void addItems(object sender, System.EventArgs e)
{
     DropDownList ddl = (DropDownList)sender;           
     ListItem newItem = new ListItem("rose", "i");
     ddl.Items.Add(newItem);
}