我的博客

asp.net 自学笔记及开发过程中的经验、技巧、摘录
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

省市二级联动(vb代码)

Posted on 2006-04-18 15:47  Net_Learner  阅读(456)  评论(0编辑  收藏  举报

webform2.aspx

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" Inherits="salonmaster.WebForm2"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    
<HEAD>
        
<title>WebForm1</title>
        
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        
<meta content="C#" name="CODE_LANGUAGE">
        
<meta content="JavaScript" name="vs_defaultClientScript">
        
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
        
<script language="javascript">
         
//jb函数会根据不同的浏览器初始化个xmlhttp对象
         
function jb()
         {
            var A
=null
               try 
               { 
                   A
=new ActiveXObject("Msxml2.XMLHTTP"); 
                } 
            catch(e)
            { 
                  try 
                   { 
                      A
=new ActiveXObject("Microsoft.XMLHTTP"); 
                   }
             catch(oc)
            { 
                     A
=null 
                   } 
              } 
           
if ( !A && typeof XMLHttpRequest != "undefined" ) 
            { 
               A
=new XMLHttpRequest() 
             } 
           return A 
         }
         
         
//下面Go函数是父列表框改变的时候调用,参数是选择的条目
         
function Go(obj)
         {
            
//得到选择框的下拉列表的value
           var svalue 
= obj.value;
            
//定义要处理数据的页面
            var weburl 
= "webform2.aspx?parent_id="+svalue;
            
//初始化个xmlhttp对象
            var xmlhttp 
= jb();
            
//提交数据,第一个参数最好为get,第三个参数最好为true
            xmlhttp.open(
"get",weburl,true);
           
// alert(xmlhttp.responseText);
            
//如果已经成功的返回了数据
            xmlhttp.onreadystatechange
=function()
            {
              
if(xmlhttp.readyState==4)//4代表成功返回数据
               {
                  var result 
= xmlhttp.responseText;//得到服务器返回的数据
                  
//先清空dListChild的所有下拉项
                 document.getElementById(
"dListChild").length = 0;
                  
//给dListChild加个全部型号的,注意是Option不是option
                  document.getElementById(
"dListChild").options.add(new Option("所有城市","0"));
                  
if(result!="")//如果返回的数据不是空
                  {
                     
//把收到的字符串按照,分割成数组
                     var allArray 
= result.split(",");
                    
//循环这个数组,注意是从1开始,因为收到的字符串第一个字符是,号,所以分割后第一个数组为空
                     
for(var i=1;i<allArray.length;i++)
                     {
                        
//在把这个字符串按照|分割成数组
                       var thisArray 
= allArray[i].split("|");
                        
//为dListChild添加条目
                        document.getElementById(
"dListChild").options.add(new Option(thisArray[1].toString(),thisArray[0].toString()));
                     }
                  }
               }
            }
            
//发送数据,请注意顺序和参数,参数一定为null或者""
            xmlhttp.send(
null);
         }
        
</script>
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<asp:dropdownlist id="dListParent" style="Z-INDEX: 101; LEFT: 160px; POSITION: absolute; TOP: 56px"
                runat
="server" onchange="Go(this)">                
            
</asp:dropdownlist>
            
<asp:dropdownlist id="dListChild" style="Z-INDEX: 102; LEFT: 304px; POSITION: absolute; TOP: 56px"
                runat
="server"></asp:dropdownlist><asp:button id="Button1" style="Z-INDEX: 103; LEFT: 176px; POSITION: absolute; TOP: 128px" runat="server"
                Text
="提交"></asp:button>
            
<asp:Label id="Label1" style="Z-INDEX: 104; LEFT: 168px; POSITION: absolute; TOP: 24px" runat="server">Label</asp:Label>
        
</form>
    
</body>
</HTML>

后台代码

Imports System.Data.OleDb
Public Class WebForm2
    
Inherits System.Web.UI.Page

Web 窗体设计器生成的代码

    
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
' 在此处放置用户代码以初始化页面
        If Not IsPostBack Then
            bindPrivance()
        
End If
        BindDrop()
    
End Sub
 

    
Protected Sub bindPrivance()
        
Dim conn As New OleDbConnection(ConfigurationSettings.AppSettings("area").ToString)
        
Dim commStr As String = "select * from povince"
        
Dim adp As New OleDbDataAdapter(commStr, conn)
        
Dim ds As New DataSet
        adp.Fill(ds, 
"area")
        dListParent.DataSource 
= ds.Tables("area").DefaultView
        dListParent.DataTextField 
= "province"
        dListParent.DataValueField 
= "provinceID"
        dListParent.DataBind()
    
End Sub


    
Protected Sub BindDrop()
        
'首先我想父dropdownlist也绑定数据库,后面想没必要
        'if(!IsPostBack)
        '{
        '绑定父dListParent
        ' BindParent();
        '}
        '获得传递过来的parent_id值,如果是第一次请求他为null
        Dim str As String = Request.QueryString("parent_id")
        
Dim str1 As String = dListParent.SelectedValue
        
'  Response.Write(str1)
        '如果str加个字符串!=原来的字符串则说明触发过dListParent的onchange事件
        If str + "abc" <> "abc" Then
            
'绑定 dListChild控件
            BindChild(str'把传来的父DropDownList的value做为参数
        Else
            BindParent(str1)
        
End If
    
End Sub
 'BindDrop


    
Protected Sub BindParent(ByVal str As String)
        
'如果是第一次请求或者是刷新这个页面则根据dListParent的值来选择
        '把参数转化成int
        ' Dim i As Integer = Convert.ToInt32(str)
        dListChild.Items.Clear()
        dListChild.Items.Add(
New ListItem("全部城市""0"))
        
'得到数据库连接字符串
        Dim connStr As String = ConfigurationSettings.AppSettings("area").ToString
        
'初始化个conn对象
        Dim conn As New OleDbConnection(connStr)
        
'数据库语句
        Dim commStr As String = "select cityID,city from city where father = '" & str & "'"
        
'建立数据库命令对象
        Dim comm As New OleDbCommand(commStr, conn)
        
'打开数据库
        conn.Open()
        
'执行命令
        Dim dr As OleDbDataReader = comm.ExecuteReader()
        
'循环dr,给dListParent添加条目
        While dr.Read()
            dListChild.Items.Add(
New ListItem(dr(1).ToString(), dr(0).ToString()))
        
End While '也可以这样
        '添加下面这话的意思是当点提交按钮提交窗体的时候第二个dListChild的状态能够得到保存
        dListChild.SelectedValue = Request.Form("dListChild")
        dr.Close()
        conn.Close()
    
End Sub
 'BindParent

    
Protected Sub BindChild(ByVal str As String)
        
'通过js给包括dropdownlist任何控件添加的内容不会被保存状态
        '把参数转化成int
        '定义个字符串用保存从数据库返回的数据
        Dim result As String = ""
        
'先清空输出的东西
        Response.Clear()
        
Dim connStr As String = ConfigurationSettings.AppSettings("area").ToString
        
Dim conn As New OleDbConnection(connStr)
        
Dim comm As OleDbCommand = conn.CreateCommand()
        
Dim commStr As String = "select cityID,city from city where father = '" & str & "'"
        comm.CommandText 
= commStr
        conn.Open()
        
Dim dr As OleDbDataReader = comm.ExecuteReader()
        
While dr.Read()
            result 
+= "," + dr(0).ToString() + "|" + dr(1).ToString()
        
End While
        
'把从数据库得到的信息输出到客户端
        Response.Write(result)
        
'输出完成关闭Response,以免造成不必要的输出
        Response.Flush()
        Response.Close()
        dr.Close()
        conn.Close()
    
End Sub


    
''*******************************************************************************************************
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
If dListChild.SelectedValue = 0 Then
            Label1.Text 
= dListParent.SelectedItem.Text
        
Else
            Label1.Text 
= dListChild.Items(dListChild.SelectedIndex).Text
        
End If
    
End Sub

End Class


呵呵...参考C#联动的.附加上个数据库...不过还没有写三级联运呢..