Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于另一个下拉列表在下拉列表中显示数据_C# - Fatal编程技术网

C# 基于另一个下拉列表在下拉列表中显示数据

C# 基于另一个下拉列表在下拉列表中显示数据,c#,C#,我有两个下拉列表,其中我从数据库中获取显示的数据。 当选择DropDownList2中的学院名称时,只有相关分支必须显示在DropDownList2中该学院的中。为此,我使用了一个存储过程,当我通过传递参数手动运行它时,它工作正常 但在执行代码时,我会显示所有的分支。我需要回发帖子吗? 请在这个场景中帮助我 下面是我的代码: string queryString = "select College_Name from Colleges"; string constring

我有两个下拉列表,其中我从数据库中获取显示的数据。 当选择DropDownList2中的学院名称时,只有相关分支必须显示在DropDownList2中该学院的中。为此,我使用了一个存储过程,当我通过传递参数手动运行它时,它工作正常

但在执行代码时,我会显示所有的分支。我需要回发帖子吗? 请在这个场景中帮助我

下面是我的代码:

string queryString = "select College_Name from Colleges";
            string constring = System.Configuration.ConfigurationManager.ConnectionStrings["ConnDBForum"].ConnectionString;
            SqlConnection connection = new SqlConnection(constring);
            SqlCommand command = new SqlCommand(queryString, connection);
            connection.Open();
            DataTable dt = new DataTable();
            SqlDataAdapter ad = new SqlDataAdapter(command);
            ad.Fill(dt);

            if (dt.Rows.Count > 0)
            {
                DropDownList2.Items.Insert(0, new ListItem(String.Empty, String.Empty));
                DropDownList2.DataSource = dt;
                DropDownList2.DataTextField = "College_Name";
                DropDownList2.DataValueField = "College_Name";
                DropDownList2.DataBind();
                DropDownList2.Items.Insert(0, new ListItem(String.Empty, String.Empty));
            }


            SqlCommand Cmd = new SqlCommand("Branch_display", connection);
            Cmd.CommandType = CommandType.StoredProcedure;
            Cmd.Parameters.Add(new SqlParameter("@College_Name", DropDownList2.SelectedValue));
            DataTable dt1 = new DataTable();
            SqlDataAdapter ad1 = new SqlDataAdapter(Cmd);
            ad1.Fill(dt1);

            if (dt1.Rows.Count > 0)
            {

                DropDownList1.DataSource = dt1;
                DropDownList1.DataTextField = "Name";
                DropDownList1.DataValueField = "Name";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem(String.Empty, String.Empty));
            }
            connection.Close();
        }

是的,你需要回复一个帖子,像这样的帖子应该会有帮助

      protected void Page_Load(object sender, EventArgs e)
        {

                if(!IsPostBack)
                {
          string queryString = "select College_Name from Colleges";
    string   constring=System.Configuration.ConfigurationManager.ConnectionStrings["ConnDBForum"].ConnectionString;

                SqlConnection connection = new SqlConnection(constring);
                SqlCommand command = new SqlCommand(queryString, connection);
                connection.Open();
                DataTable dt = new DataTable();
                SqlDataAdapter ad = new SqlDataAdapter(command);
                ad.Fill(dt);

                if (dt.Rows.Count > 0)
                {
                    DropDownList2.Items.Insert(0, new ListItem(String.Empty, String.Empty));
                    DropDownList2.DataSource = dt;
                    DropDownList2.DataTextField = "College_Name";
                    DropDownList2.DataValueField = "College_Name";
                    DropDownList2.DataBind();
                    DropDownList2.Items.Insert(0, new ListItem(String.Empty, String.Empty));

                }
 connection.Close();
            }
        }
asp.net渲染

  <asp:dropdownlist ID=" DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged=" DropDownList2_SelectedIndexChanged">

DropDownList2
autopostback
属性设置为
true
。在
DropDownList2.选择的索引已更改
事件。。。将所需数据填充到
DropDownList1
private void  DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{ 

string   constring=System.Configuration.ConfigurationManager.ConnectionStrings["ConnDBForum"].ConnectionString;


     SqlCommand Cmd = new SqlCommand("Branch_display", connection);
            Cmd.CommandType = CommandType.StoredProcedure;
            Cmd.Parameters.Add(new SqlParameter("@College_Name", DropDownList2.SelectedValue));
            DataTable dt1 = new DataTable();
            SqlDataAdapter ad1 = new SqlDataAdapter(Cmd);
            ad1.Fill(dt1);

            if (dt1.Rows.Count > 0)
            {

                DropDownList1.DataSource = dt1;
                DropDownList1.DataTextField = "Name";
                DropDownList1.DataValueField = "Name";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, new ListItem(String.Empty, String.Empty));
            }
            connection.Close();
}