最近项目收官阶段, 发现联动有刷新, 
  我想用JS 来实现二级联动的无刷新效果,哪位高手给我写一段JS代码,
     我JS 不太懂, AJAX还没正式学, 以下是我后台代码, 大家帮帮忙  Sub bindProvince()
        Dim conn As New SqlConnection("server=.; database=fuhao; uid=sa;pwd=;")
        Dim adp As New SqlDataAdapter("select * from shengfen", conn)
        Dim ds As New DataSet
        adp.Fill(ds, "provinces")
        droppro.DataSource = ds
        droppro.DataTextField = "shengfenName"
        droppro.DataValueField = "shengfen_id"
        droppro.DataBind()
    End Sub
    Sub BindCity(ByVal shengfen_id As String)
        Dim conn As New SqlConnection("server=.; database=fuhao; uid=sa;pwd=;")
        Dim adp_chengshi As New SqlDataAdapter("select * from chengshi where shengfen_id =" & Me.droppro.SelectedValue, conn)
        Dim ds_chengshi As New DataSet
        adp_chengshi.Fill(ds_chengshi, "provinces")
        dropcity.DataSource = ds_chengshi
        dropcity.DataTextField = "chengshi_Name"
        dropcity.DataValueField = "shengfen_id"
        dropcity.DataBind()    End Sub
        If Not Page.IsPostBack Then
            Call bindProvince()
            Call BindCity(droppro.SelectedItem.Value)        End If Private Sub droppro_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles droppro.SelectedIndexChanged
        Call BindCity(droppro.SelectedItem.Value)    End Sub

解决方案 »

  1.   

    这个需要将下面的代码放到另一个页面 或者生成 xml 或者生成json或者用纯文本 然后前台用js根据参数调用
    思路大概就是这样  你可以找一下类似代码Sub BindCity(ByVal shengfen_id As String)
            Dim conn As New SqlConnection("server=.; database=fuhao; uid=sa;pwd=;")
            Dim adp_chengshi As New SqlDataAdapter("select * from chengshi where shengfen_id =" & Me.droppro.SelectedValue, conn)
            Dim ds_chengshi As New DataSet
            adp_chengshi.Fill(ds_chengshi, "provinces")
            dropcity.DataSource = ds_chengshi
            dropcity.DataTextField = "chengshi_Name"
            dropcity.DataValueField = "shengfen_id"
            dropcity.DataBind()    End Sub 
      

  2.   

    用ajax.net 无刷新效果更新不需要js。
      

  3.   

    恩 也是 现在微软的东西使得初中生都能做出好系统来ajax.net 拖个控件就能无刷新联动了
      

  4.   


     给你个例子: <script>
       function load(state)
       {
          var drp2 = document.getElementById("DropDownList2");
             for (i = drp2.length; i >= 0; i--)
             { 
               drp2.options.remove(i); 
             } 
            var oHttpReq = new ActiveXObject("MSXML2.XMLHTTP");
        var oDoc = new ActiveXObject("MSXML2.DOMDocument");
        oHttpReq.open("POST", "Default2.aspx?state="+escape(state), false);
    //    alert(state);
        oHttpReq.send(null);
        result = oHttpReq.responseText;
    //    alert(result);
        oDoc.loadXML(result);
        items = oDoc.selectNodes("//ZHENGBIAO/Table");
        for (var item = items.nextNode(); item; item = items.nextNode())
        {
         var city = item.nodeTypedValue; 
          var newOption = document.createElement("OPTION");
          newOption.text = city;
          newOption.value = city;
          drp2.options.add(newOption);
        }
       }
     </script>   <asp:DropDownList ID="ddl11111" runat="server" Style="left: 72px; position: relative;
                top: 18px" Width="146px">
                <asp:ListItem Selected="True">1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
                <asp:ListItem>3</asp:ListItem>
                
                <asp:ListItem></asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList ID="DropDownList2" runat="server" Style="left: 101px; position: relative;
                top: 20px" Width="137px">
            </asp:DropDownList>  后台:
        protected void Page_Load(object sender, EventArgs e)
        {
             //也可以在这里绑定数据到第一个下拉框中。
            ddl11111.Attributes.Add("onchange","load(this.options[this.selectedIndex].innerText)");
        }  Default.aspx 页面:
      后台:
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Request["state"] != null)
            {
                string state = this.Request["state"].ToString();
                string str = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=D:\ENET\txtOper\data\data.mdb";
                OleDbConnection con = new OleDbConnection(str);
                OleDbDataAdapter da = new OleDbDataAdapter("select city from example where state ='"+state+"'",con);
                DataSet ds = new DataSet("ZHENGBIAO");
                da.Fill(ds);
                XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Response.ContentEncoding);
                writer.Formatting = Formatting.Indented;
                writer.Indentation = 4;
                //writer.IndentChar =""; 
                ds.WriteXml(writer);
                writer.Flush();
                Response.End();
                writer.Close();
            }
        }
      

  5.   

       for (var item = items.nextNode(); item; item = items.nextNode()) 
        { 
         var city = item.nodeTypedValue;  
          var newOption = document.createElement("OPTION"); 
          newOption.text = city; 
          newOption.value = city; 
          drp2.options.add(newOption); 
        } 
    改下
    可以这样
       for (var item = items.nextNode(); item; item = items.nextNode()) 
        { 
         var city = item.nodeTypedValue;  
          drp2.options.add(new Option(city,city)); 
        }