drop down list value in asp.net

前端 未结 9 1925
刺人心
刺人心 2021-01-18 08:29

I want to add a value to the drop down list that cannot be selected, like a title. Ex: I have a month drop down list. The very first item should be \"select month\" this sho

9条回答
  •  不知归路
    2021-01-18 08:44

    To add items to drop down list with value in asp.net using c# just add below codes for example:

    try
    {
        SqlConnection conn = new SqlConnection(conStr);
        SqlCommand comm = conn.CreateCommand();
        comm = conn.CreateCommand();
        comm.CommandText = "SELECT title,gid from Groups";
        comm.CommandType = CommandType.Text;
        conn.Open();
        SqlDataReader dr = comm.ExecuteReader();
        while (dr.Read())
        {
           dropDownList.Items.Add(new 
           ListItem(dr[0].ToString(),dr[1].ToString()));
        }
    }
    catch (Exception e)
    {
        lblText.Text = e.Message;
    }
    finally
    {
      conn.Close();
    }
    

提交回复
热议问题