【问题标题】:C#: How would I change the contents of a drop down list upon the change of another?C#:如何在更改另一个下拉列表时更改下拉列表的内容?
【发布时间】:2020-09-17 15:14:51
【问题描述】:

我是 C# 新手,我正在尝试了解如何在更改另一个下拉列表时更改另一个下拉列表的内容?如下所示,我目前的尝试不成功,因此我们将不胜感激任何建议或帮助。

protected void drpDwnSchool_TextChanged(object sender, EventArgs e)
{
    drpDwnTeacher.Items.Clear();
    string selectedSchool = drpDwnSchool.SelectedValue.ToString();
    String sqlQueryTeacher = "SELECT * FROM Teacher WHERE SchoolName = '" + selectedSchool + "'";
    SqlConnection sqlConnect = new SqlConnection(WebConfigurationManager.ConnectionStrings["Lab1"].ToString());
    SqlCommand sqlCommand1 = new SqlCommand();
    sqlCommand1.Connection = sqlConnect;
    sqlCommand1.CommandType = CommandType.Text;
    sqlCommand1.CommandText = sqlQueryTeacher;
    sqlConnect.Open();
    SqlDataReader queryResultsTeacher = sqlCommand1.ExecuteReader();
    while (queryResultsTeacher.Read())
    {
        string LastName = queryResultsTeacher["LastName"].ToString();
        drpDwnTeacher.Items.Add(queryResultsTeacher["LastName"].ToString());
    }
   sqlConnect.Close();
}

【问题讨论】:

  • 你通常使用AJAX,如果你使用你知道的语言你会怎么做
  • 您没有将任何数据绑定到drpDwnTeacher
  • 删除行drpDwnTeacher.Items.Clear();来自您的 textcahnged 事件

标签: c# sql asp.net


【解决方案1】:

它没有填充第二个列表吗?如果是这样,请尝试“SelectedIndexChanged”事件而不是更改文本。

【讨论】:

    【解决方案2】:

    好的,有几个问题。

    因此,在页面加载时,我们假设您加载了第一个下拉菜单。

    if (IsPostBack == false)
    {
        // first time page load schools
        LoadSchools();
    }
    

    以及我们加载组合框的代码:

    public void LoadSchools()
    {
    string strSQL = "SELECT SchoolName FROM tblSchools ORDER BY SchoolName";
    
    this.DropDownSchools.DataSource = Myrst(strSQL);
    this.DropDownSchools.DataBind();
    }
    

    因此,在页面加载时,上述内容会使用学校列表填写下拉列表(组合)。

    现在,当您选择一所学校时?

    首先,您需要为学校组合框设置自动过去。 现在,当学校被选中时,您现在可以展示教师。

    我会使用已更改的选定索引,而不是像您那样更改文本。

    因此,该代码如下所示:

    protected void DropDownSchools_SelectedIndexChanged(object sender, EventArgs e)
    {
    string strSQL;
    strSQL = "SELECT Teacher from Teachers WHERE School = '" + 
    DropDownSchools.SelectedValue + "' ORDER BY Teacher";
    DropDownTeachers.DataSource = MyRst(strSQL);
    DropDownTeachers.databind();
    
    }
    

    因为对于每个 sql 查询,你必须在接下来的 10 年中编写,并创建 cmd 对象、connect 对象和 datatable 对象?好吧,让我们节省一些手指,您可以使用放置在 app_code 中或放置全局范围代码的公共例程。该例程看起来像:

    public DataTable Myrst(string strSQL, string strCon = "")
    {
    
    // this also allows one to pass custom connection string 
    // - if not passed, then default
    
    if (strCon == "") {
      strCon =
      ConfigurationManager.ConnectionStrings("WebEasy.My.MySettings.Test3").ConnectionString;
    }
    
    SqlConnection mycon = new SqlConnection(strCon);
    
    SqlDataAdapter oReader = new SqlDataAdapter();
    DataTable rstData = new DataTable();
    
    oReader.SelectCommand = new SqlCommand(strSQL, mycon);
    
    try
    {
        oReader.Fill(rstData);
        return rstData;
    }
    catch
    {
        return null;
    }
    }
    

    我的 c# 有点弱,但上面应该可以工作。

    但是,我们应该为此使用参数。虽然下拉菜单并不是真正直接的用户输入,但作为未来的习惯,上面的代码应该使用参数而不是 sql 的字符串连接。

    修改上面的 MyRst 以使用 sql 命令对象代替字符串,但上面是一个很好的开始,可以让这种类型的代码工作。

    【讨论】:

      【解决方案3】:

      我认为最简单的方法是从 HTML 源中设置 Dropdownlist DataTextField 如下

      <asp:DropDownList ID="DropDownList1" DataTextField="YourFieldNameHere" runat="server" />
      

      还要确保在您打算用于填充第二个 Dropdownlist 的 Dropdownlist 上将 AutoPostBack 设置为 true。 在代码隐藏时,将下拉列表绑定到数据库,如下所示

      protected void drpDwnSchool_TextChanged(object sender, EventArgs e)
      {
          string selectedSchool = drpDwnSchool.SelectedValue.ToString();
          String sqlQueryTeacher = "SELECT * FROM Teacher WHERE SchoolName = '" + selectedSchool + "'";
          SqlConnection sqlConnect = new SqlConnection(WebConfigurationManager.ConnectionStrings["Lab1"].ToString());
          SqlCommand sqlCommand1 = new SqlCommand();
          sqlCommand1.Connection = sqlConnect;
          sqlCommand1.CommandType = CommandType.Text;
          sqlCommand1.CommandText = sqlQueryTeacher;
          sqlConnect.Open();
          SqlDataReader queryResultsTeacher = sqlCommand1.ExecuteReader();
          if(queryResultsTeacher.HasRows){
      DropDownList1.DataSource = queryResultsTeacher;
      DropDownList1.DataBind();
      }
         sqlConnect.Close();
      }
      

      【讨论】:

        猜你喜欢
        • 2019-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多