当前位置:编程学习 > C#/ASP.NET >>

如何实现两个DropDownList两级联动连接数据库

using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class DropDownList : System.Web.UI.Page
     
{
   

    string cnnstr = "Data Source=IE361-PC;Initial Catalog=emplyee;Persist Security Info=True;User ID=sa;Password=123";
    SqlConnection conn;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection conn = new SqlConnection(cnnstr);
        string strSQL = "select distinct xb  from xbbj";
     
        SqlDataAdapter ada = new SqlDataAdapter(strSQL, conn);
        DataSet ds = new DataSet();
        ada.Fill(ds, "xbbj");
        DropDownList1.DataSource = ds;//.Tables["xbbj"].DefaultView;
        DropDownList1.DataValueField ="xb";// ds.Tables["xbbj"].Columns[0].ColumnName;
        DropDownList1.DataTextField = "xb";// ds.Tables["xbbj"].Columns[0].ColumnName;
        DropDownList1.DataBind();
    //  ds.Dispose();
   
        }  
    }

   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Write(DropDownList1.SelectedItem.Text.ToString());
      
  //SqlConnection conn = new SqlConnection(cnnstr);
        string strSQL1 = "select distinct bj from xbbj where xb='"+DropDownList1.SelectedValue+"'";
        SqlDataAdapter ada1 = new SqlDataAdapter(strSQL1, conn);
        DataSet ds1 = new DataSet();
        ada1.Fill(ds1, "xbbj1");
        DropDownList2.DataSource = ds1.Tables["xbbj1"].DefaultView;
        DropDownList2.DataValueField = ds1.Tables["xbbj1"].Columns[0].ColumnName;
        DropDownList2.DataTextField = ds1.Tables["xbbj1"].Columns[0].ColumnName;
        DropDownList2.DataBind();
    
        ds1.Dispose();
      
      
    }
  
}

/*

 数据库文件  表名:xbbj
  xb             bj
 计算机系        一班
 计算机系        二班 
 计算机系        三班
 计算机系        四班
 水利水电系     一班
 水利水电系     二班
 水利水电系     三班
 水利水电系     四班
--------------------编程问答-------------------- 第一个DropDownList设置属性AutoPostBack="true" --------------------编程问答-------------------- 出现怎么个状况?设置ls说的autopostback没? --------------------编程问答--------------------
引用 1 楼 ihandler 的回复:
第一个DropDownList设置属性AutoPostBack="true"

+1 --------------------编程问答-------------------- 如果是用一条语句查询出的结果,要用到defaultview的filter过滤filter=“计算机系” --------------------编程问答-------------------- 参考
http://www.cnblogs.com/zhangzs8896/articles/382632.html --------------------编程问答-------------------- jQueryJSON 无刷新三级联动 

http://www.cnblogs.com/zengxiangzhan/archive/2010/02/21/1670646.html --------------------编程问答-------------------- 把第一级的DropDownList设置属性AutoPostBack="true" --------------------编程问答--------------------
引用 3 楼 peter200694013 的回复:
引用 1 楼 ihandler 的回复:
第一个DropDownList设置属性AutoPostBack="true"

+1
+1 --------------------编程问答-------------------- 问题描述的不够清楚
真的是autopostback的问题? --------------------编程问答--------------------    <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
               <asp:DropDownList ID="ddlLB" runat="server" Width="15%" AutoPostBack="True" OnSelectedIndexChanged="ddlLB_SelectedIndexChanged">
               </asp:DropDownList>
                 <asp:DropDownList ID="ddlChild" runat="server" Width="20%">
               </asp:DropDownList>
                 </ContentTemplate>
        </asp:UpdatePanel>
        </div> 
protected void ddlLB_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(this.ddlLB.SelectedValue!=null)
        {
            BindChild(this.ddlLB.SelectedValue);
        }
    }什么问题
--------------------编程问答--------------------
引用 10 楼 wuyq11 的回复:
<asp:ScriptManager ID="ScriptManager1" runat="server">
  </asp:ScriptManager>
  <div>
  <asp:UpdatePanel ID="UpdatePanel1" runat="server">
  <ContentTemplate>
  <asp:DropDownList ID="ddlLB" runat……
顶啊 --------------------编程问答-------------------- 还是不行啊 --------------------编程问答-------------------- 现在可以了,不过不怎么好用
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,