asp.net通过DropDownList绑定数据数据库实现级联怎么无效呢?代码如下:
前台:<asp:DropDownListID="school"runat="server"AutoPostBack="true"OnSelectedIndexChange...
前台:
<asp:DropDownList ID="school" runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="school_SelectedIndexChanged"
Height="20px" Width="109px">
</asp:DropDownList>
<asp:DropDownList ID="pro" runat="server" Height="20px"
Width="107px" >
</asp:DropDownList>
后台:
protected void Page_Load(object sender, EventArgs e)
{
this.results.Visible = false;
this.add2.Visible = false;
if(!IsPostBack)
{
string strConn = @"Data Source=. ;Initial Catalog=Stuadmin;Integrated
Security=True";
SqlConnection con = new SqlConnection(strConn);
string sql = "SELECT distinct [sname] FROM [pro]";
SqlDataAdapter school1 = new SqlDataAdapter(sql, con);
DataSet schools = new DataSet();
con.Open();
school1.Fill(schools);
this.school.DataSource = schools;
this.school.DataValueField = "sname";
this.school.DataTextField = "sname";
this.school.DataBind();
con.Close();
}
}
protected void school_SelectedIndexChanged(object sender, EventArgs e)
{
string strConn = @"Data Source=.;Initial Catalog=Stuadmin;Integrated
Security=True";
SqlConnection con = new SqlConnection(strConn);
string sql = "SELECT [pname] FROM [pro] WHERE sname='" +
this.school.SelectedValue.Trim() + "'";
SqlDataAdapter pro1 = new SqlDataAdapter(sql,con);
DataSet pros = new DataSet();
con.Open();
pro1.Fill(pros);
this.pro.DataSource = pros;
this.pro.DataValueField = "pname";
this.pro.DataTextField = "pname";
this.pro.DataBind();
con.Close();
} 展开
<asp:DropDownList ID="school" runat="server"
AutoPostBack="true"
OnSelectedIndexChanged="school_SelectedIndexChanged"
Height="20px" Width="109px">
</asp:DropDownList>
<asp:DropDownList ID="pro" runat="server" Height="20px"
Width="107px" >
</asp:DropDownList>
后台:
protected void Page_Load(object sender, EventArgs e)
{
this.results.Visible = false;
this.add2.Visible = false;
if(!IsPostBack)
{
string strConn = @"Data Source=. ;Initial Catalog=Stuadmin;Integrated
Security=True";
SqlConnection con = new SqlConnection(strConn);
string sql = "SELECT distinct [sname] FROM [pro]";
SqlDataAdapter school1 = new SqlDataAdapter(sql, con);
DataSet schools = new DataSet();
con.Open();
school1.Fill(schools);
this.school.DataSource = schools;
this.school.DataValueField = "sname";
this.school.DataTextField = "sname";
this.school.DataBind();
con.Close();
}
}
protected void school_SelectedIndexChanged(object sender, EventArgs e)
{
string strConn = @"Data Source=.;Initial Catalog=Stuadmin;Integrated
Security=True";
SqlConnection con = new SqlConnection(strConn);
string sql = "SELECT [pname] FROM [pro] WHERE sname='" +
this.school.SelectedValue.Trim() + "'";
SqlDataAdapter pro1 = new SqlDataAdapter(sql,con);
DataSet pros = new DataSet();
con.Open();
pro1.Fill(pros);
this.pro.DataSource = pros;
this.pro.DataValueField = "pname";
this.pro.DataTextField = "pname";
this.pro.DataBind();
con.Close();
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询