In depth analysis of the usage of DropDownList

  • 2020-05-12 03:07:31
  • OfStack

First, bind the data.
The three databind methods for collecting dropdownlist are as follows:
Basic data binding: directly enumerated with ListItem for a list of types that do not need to be modified.

<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Value=" Design home "> Design home </asp:ListItem>
    <asp:ListItem Value=" Web design "> Web design </asp:ListItem>
    <asp:ListItem Value=" Network programming "> Network programming </asp:ListItem>
    <asp:ListItem Value=" Cool site appreciate "> Cool site appreciate </asp:ListItem>
</asp:DropDownList>

Dynamic binding method 1: dynamically bind the fields in the database.

SqlConnection conn = system.Configuration.ConfigurationSettings.AppSettings["SqlConnection"].ToString();
string strSQL = "select * from CompanyType";
SqlDataAdapter ada = new SqlDataAdapter(strSQL, conn);
DataSet ds = new DataSet();
ada.Fill(ds, "CompanyType");
DropDownList1.DataSource = ds.Tables["CompanyType"].DefaultView;
DropDownList1.DataValueField = ds.Tables["CompanyType"].Columns[1].ColumnName;
DropDownList1.DataTextField = ds.Tables["CompanyType"].Columns[1].ColumnName;
DropDownList1.DataBind();
ds.Dispose();

// Among them datavaluefield Properties are for controls 1 The key attributes, cs Page through value Value to obtain; 
// while datatextfield Is the text displayed on the view page. 

Dynamic binding method 2: use the DropDownList.Items.Add method.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        SqlConnection conn = system.Configuration.ConfigurationSettings.AppSettings["SqlConnection"].ToString();
        try
        {
conn.Open();
this.DropDownList1.Items.Add("");
string strSQL = "select CompanyType from CompanyType";
SqlCommand com = new SqlCommand(strSQL, conn);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
        {
        this.DropDownList1.Items.Add(dr["CompanyType"].ToString());
        // or 
        //DropDownList_name.Items.Add(new ListItem(TEXT, Value));
    }
}
catch (Exception ex)
{
    Response.Write("<scirpt>alert('" + ex.Message.ToString() + "')</script>");
}
finally
{
    conn.Close();
}
}
}

After binding, we will realize the linkage function of dropdownlist.
To make an online change, you need to use the selectedindexchange event, and remember to set the AutoPostBack value to "true"
Here is one of the simplest effects.

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList2.Items.Clear();
        if (DropDownList1.Items[0].Selected)
        {
DropDownList2.Items.Add(" Luk siu fung ");
DropDownList2.Items.Add(" Chu liuxiang ");
        }
        else
        {
DropDownList2.Items.Add(" Yang2 guo4 ");
DropDownList2.Items.Add(" Little dragon female ");
        }
    }

If you want to achieve no refresh linkage, go to baidu. There are many well-documented cases on the web.

Similarly, if you want a subordinate to automatically get the right data field.
string id=dropdownlist1.SelectedValue;
You can then read the corresponding part of the data in the database according to this "id"

Finally, a nice example of reading data line by line through DataSet, the business system "plan center" drop-down list.

DataSet Ds = null;
string SqlStr = null;
SqlServer sqlserverDB = new SqlServer();
SqlStr = "select name,account from qdvc_usersimple";
Ds = sqlserverDB.DataSetRun(null, SqlStr, "qdvc_usersimple");
foreach (DataRow dataRow in Ds.Tables[0].Rows) 
{ 
    object[] itemArray = dataRow.ItemArray; // To obtain dataRow All the data in the cells Array 
    // itemArray[0].ToString() is "name",itemArray[1].ToString() is "account"
    DropDownList_name.Items.Add(new ListItem(itemArray[0].ToString(), itemArray[1].ToString()));
}


Related articles: