我的Web应用程序项目中有2个下拉列表和数据库。在DB中我有域表和子域表。在子域表中,我有域表中DomainId列的外键。 我用来自域的数据填充第一个droplist(我使用DomainId作为datavalue,DomainName作为datatext /这里是代码:
protected void Page_Load(object sender, EventArgs e)
{
string connString = System.Configuration.ConfigurationManager.ConnectionStrings["SampleConnectionString"].ToString();
conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("SELECT DomainId , DomainName FROM Domain",conn);
conn.Open();
DropDownList1.DataTextField = "DomainName";
DropDownList1.DataValueField = "DomainId";
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataBind();
}
现在当用户从下拉列表中选择项目时,我想根据所选项目填充第二个下拉列表,但它不起作用。 这是代码的第二部分:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string connString = System.Configuration.ConfigurationManager.ConnectionStrings["SampleConnectionString"].ToString();
conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("SELECT SubDomainName, SubDomainId FROM SubDomain where DomainId= @id", conn);
cmd.Parameters.AddWithValue("@id", DropDownList1.SelectedValue);
conn.Open();
DropDownList2.DataTextField = "SubDomainName";
DropDownList2.DataValueField = "SubDomainId";
DropDownList2.DataSource = cmd.ExecuteReader();
DropDownList2.DataBind();
conn.Close();
}
答案 0 :(得分:0)
将DropDownList1控件的AutoPostBack属性设置为true。如果您已经设置了但仍然有错误,也可以发布.aspx文件