Ajaxpro 三级联动

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="BoroughAdd.aspx.cs" Inherits="System_Borough_BoroughAdd" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题页</title><script language="javascript" type="text/javascript">
       // ACLOUD 常用JS函数
       function getBid(s){
    return document.getElementById(s);
  }
  function getBmc(s){
    return document.getElementByName(s);
  }
  
       //显示分类列表
    function showNext(sid,obj)//选上一个的时候,给下一个控件赋值
        {
          if(sid==null || sid=="" || sid.length<1)return;//如果当前下拉框的值为空 返回
          var slt =getBid(obj);//获得下一个控件对象
       var v = System_Borough_BoroughAdd.getNextClass(sid).value; // 获得下一个类的数据源
//        alert(v.Tables[0].Rows[i].txt);
       //return;
       if (v != null)
       {      
      if(v != null && typeof(v) == "object" && v.Tables != null)
     {  
         slt.length = 0;//初始化下级控件
         slt.options.add(new Option("请选择",0));//添加一行
         //加了个“请选择”主要为了触发onchange事件
        
         //如果选第一个的时候第三个默认为 请选择
         if(obj=="ddl2")
         {
             getBid("ddl3").options.length=0;
             getBid("ddl3").options.add(new Option("请选择",0));
         }
        
      for(var i=0; i<v.Tables[0].Rows.length; i++)
        {
         var txt = v.Tables[0].Rows[i].City_Name; //这个地方需要注意区分大小写
          var vol = v.Tables[0].Rows[i].City_PID; //跟dataset表的列名称要一致
          slt.options.add(new Option(txt,vol));
        }
     }
     }
     return;
        }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table width="500" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td width="99">&nbsp;</td>
          <td width="401">
              省<asp:DropDownList ID="ddl1" runat="server">
              </asp:DropDownList>
              市<asp:DropDownList ID="ddl2" runat="server">
              </asp:DropDownList>
              区域<asp:DropDownList ID="ddl3" runat="server">
              </asp:DropDownList></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
      </table>
    
    </div>
    </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HouseCust.FK.Common;
using HouseCust.FK.Model;
using HouseCust.FK.BLL;
public partial class System_Borough_BoroughAdd : System.Web.UI.Page
{
    BLL_FK_Sys_CityErea BllErea = new BLL_FK_Sys_CityErea();
    protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(typeof(System_Borough_BoroughAdd));
        if (!IsPostBack)
        {
            BindDc();
        }
    }
    [AjaxPro.AjaxMethod]
    public DataSet getNextClass(string cid)
    {
        try
        {
            return BllErea.GetList(int.Parse(cid));
        }
        catch
        {
            return null;
        }
    }
    private void BindDc()
    {
        //第一个
        ddl1.DataSource = BllErea.GetList(0).Tables[0].DefaultView;
        ddl1.DataTextField = "City_Name";
        ddl1.DataValueField = "City_PID";
        ddl1.DataBind();
        if (ddl1.DataSource != null)
        {
            ddl1.Attributes.Add("onchange", "showNext(this.options[selectedIndex].value,'ddl2');");
        }
        //可以先判断 DropDownList.SelectedItem.Value
        //第二个
        ddl2.DataSource = BllErea.GetList(int.Parse(ddl1.SelectedItem.Value)).Tables[0].DefaultView;
        ddl2.DataTextField = "City_Name";
        ddl2.DataValueField = "City_PID";
        ddl2.DataBind();
        //第三个
        if (ddl2.DataSource != null)
        {
            ddl2.Attributes.Add("onchange", "showNext(this.options[selectedIndex].value,'ddl3');");
        }
        ddl3.DataSource = BllErea.GetList(int.Parse(ddl2.SelectedItem.Value)).Tables[0].DefaultView;
        ddl3.DataTextField = "City_Name";
        ddl3.DataValueField = "City_PID";
        ddl3.DataBind();
    }
}

create table FK_Sys_CityErea
(
   City_ID              int                  identity(1,1) primary key, --自增ID
   City_PID             int                  null,    --区域父类ID
   City_CID             int                  null,    --区域子类ID
   City_Name            nvarchar(50)         null,    --区域名称
   City_SName  nvarchar(10)      null,    --区域简称
   Add_Time             datetime             default getdate()   --添加时间
)

CREATE PROCEDURE Sp_Dic_GetListByDic_CID
@Dic_CID int
AS
SELECT
Dic_ID,Dic_PID,Dic_Name
  FROM FK_Sys_Dic
  WHERE Dic_CID=@Dic_CID

GO
posted @ 2008-05-18 22:06  熊忠荣  阅读(681)  评论(0编辑  收藏  举报