简体   繁体   English

在ASP.NET C#中动态填充数据库中的下拉列表

[英]fill drop down list from database dynamically in asp.net c#

I have drop down list in asp.net app with c#. 我用c#在asp.net应用程序中下拉列表。 The element of drop down list coming from database (names of user). 下拉列表的元素来自数据库(用户名)。 My drop down list showing me selected name as first name appear in my database table by default. 默认情况下,我的下拉列表显示我选择的名字作为名字。 ie when i open page i saw that name is already selected. 即当我打开页面时,我看到该名称已被选择。 I want that, selected name should be like "nworks User". 我希望选择的名称应类似于“ nworks用户”。 I tried it by using code : 我尝试通过使用代码:

 <asp:DropDownList ID="DropDownListSelectEmployee" runat="server" AutoPostBack="true"
                                OnSelectedIndexChanged="DropDownListSelectEmployee_SelectedIndexChanged"
                                OnTextChanged="DropDownListSelectEmployee_TextChanged" Height="30px" Width="250px">
                                <asp:ListItem Selected="True">nWorks Employee</asp:ListItem>
                            </asp:DropDownList>

but no use. 但没有用。 and next option i tried is : DropDownListSelectEmployee.Selecteditem.Text="nWorks User"; 我尝试的下一个选项是: DropDownListSelectEmployee.Selecteditem.Text="nWorks User"; again not worked. 再次没有用。 I am getting items from database using code : 我正在使用代码从数据库中获取项目:

public void 

    DropDownListSelectEmployee_Fill()
            {
                if (!Page.IsPostBack)
                {

                    DropDownListSelectEmployee.Items.Clear();

                    string q = "select username from nworksuser where _type='Employee';";
                    MySqlCommand cmd = new MySqlCommand(q, conn);
                    conn.Open();
                    string user = "";
                    MySqlDataReader rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        user = rdr.GetString("username");
                        DropDownListSelectEmployee.Items.Add(user);
                    }
                    conn.Close();
                }
            }

how it is possible? 怎么可能?

Assuming you want to have no names selected, use: 假设您不想选择任何名称,请使用:

use DropDownListSelectEmployee.SelectedIndex = -1; 使用DropDownListSelectEmployee.SelectedIndex = -1;

However, if you want a named box at the top, just add it as the first item of the list and have that as the selected index. 但是,如果要在顶部放置一个命名框,只需将其添加为列表的第一项,并将其作为所选索引即可。

Like so: 像这样:

DropDownListSelectEmployee.Items.Add("nWorks User");
while (rdr.Read())
                {
                    user = rdr.GetString("username");
                    DropDownListSelectEmployee.Items.Add(user);
                }

You need to add the first item outside of the loop, then by default you will have that first item selected. 您需要将第一个项目添加到循环之外,然后默认情况下将选择该第一个项目。

Use the code Behind the bold letter code after binding your data ,to select by default "nworks User" : 绑定数据后,使用粗体字母代码后面的代码来默认选择“ nworks User”:

        DropDownListSelectEmployee.DataSource = table;
        DropDownListSelectEmployee.DataTextField = "Name";
        DropDownListSelectEmployee.DataValueField = "Id";
        DropDownListSelectEmployee.DataBind();
        DropDownListSelectEmployee.Items.Insert(0, new ListItem("--nworks User--", "0"));


<asp:DropDownList runat="server" ID="DropDownListSelectEmployee"OnSelectedIndexChanged="DropDownListSelectEmployee_SelectedIndexChanged" AutoPostBack="true"
        OnTextChanged="DropDownListSelectEmployee_TextChanged" Height="30px" Width="250px">

    </asp:DropDownList>

Code Behind: protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindDropdwon(); 后面的代码:protected void Page_Load(object sender,EventArgs e){if(!Page.IsPostBack){BindDropdwon(); } } }}

    protected void DropDownListSelectEmployee_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void DropDownListSelectEmployee_TextChanged(object sender, EventArgs e)
    {

    }


    private void BindDropdwon()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Id", typeof(int));
        table.Columns.Add("Name", typeof(string));

        // Here we add three DataRows.
        table.Rows.Add(25, "Indocin");
        table.Rows.Add(50, "Enebrel");
        table.Rows.Add(10, "Hydralazine");



        DropDownListSelectEmployee.DataSource = table;
        DropDownListSelectEmployee.DataTextField = "Name";
        DropDownListSelectEmployee.DataValueField = "Id";
        DropDownListSelectEmployee.DataBind();
        ***DropDownListSelectEmployee.Items.Insert(0, new ListItem("--nworks User--", "0"));***

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM