简体   繁体   中英

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

I have drop down list in asp.net app with c#. 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". 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"; 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;

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" :

        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 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"));***

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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