我有两个下拉列表,其中我从数据库中显示数据。 当选择DropDownList2中的College Name时,只有相关的分支必须在DropDownList1中显示在该学院中,因为我使用了存储过程,当我通过传递Parameteres手动运行它时,它工作正常。
但是在执行代码时我会显示所有分支。我需要回复吗? 请帮助我解决这个问题。
以下是我的代码:
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();
}
答案 0 :(得分:0)
是的,您需要回复一些事情,这应该有帮助
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">
和行动
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();
}