| 加入收藏| 设为首页| 联系我们

首页 站长学习 站长之家 源码下载 建站素材 书籍教程 常用工具
 您现在的位置: 动力中国 >> 网络编程 >> Ajax教程 >> 文章正文  
 [推荐]轻松实现无刷新三级联动菜单[VS2005与AjaxPro]
 

轻松实现无刷新三级联动菜单[VS2005与AjaxPro]

http://www.domcn.org  文章来源:本站原创  点击数:

  关键字:轻松实现无刷新三级联动菜单[VS2005与AjaxPro]

 

最近做一些网站程序,经常要用到多个下拉菜单选择,看了介绍开始用AjaxPro这个控件,感觉效果不错。以前使用过MagicAjax,很久不用了忘记了,最麻烦的就是在虚拟目录的时候比较麻烦,呵呵,在网上也有很多,不过重要的地方经常没提醒新手,俺也是菜鸟,高手请忽略。看到这个AjaxPro使用比较简单,这次使用的是6.x的,最新的是7.x的,觉得6.0系列的方便,就选它了。
在重要的地方都有提示了,相信很容易看懂。
      首先在web.config添加这个接点 在<system.web>与</system.web>之间,如下:
   <system.web>
    <!--for Ajaxnet-->
    <httpHandlers>
      <add verb=POST,GET path=ajaxpro/*.ashx type=AjaxPro.AjaxHandlerFactory, AjaxPro.2/>
    </httpHandlers>
     <!--其他设置-->
   </system.web>
     然后把AjaxPro.2.dll丢到bin文件夹,然后引用它就可以了。
    还是发代码吧,麻烦死了下面代码是 Default.aspx.cs的


using System;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{  
   
    protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default)); //必要的
        if(!IsPostBack)BindDc();
    }

    /**//// <summary>
    /// 数据库连接 http://www.knowsky.com
    /// </summary>
    /// <returns></returns>
    public OleDbConnection myConn()
    {
        string ConnStr = Provider=Microsoft.Jet.OLEDB.4.0;Data Source= + System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings[dbpath]);
        OleDbConnection conn = new OleDbConnection(ConnStr);
        try
        {
            conn.Open();
            return conn;
        }
        catch
        {
            throw;
        }
    }
   
    /**//// <summary>
    /// 获取下级分类
    /// </summary>
    [AjaxPro.AjaxMethod]
    public DataSet getNextClass(string cid)
    {
        //因为不希望页面可以知道字段名称 所以 as txt,id as vol 如果是sql ser 可以用 =
        //页面获取的 列名称 必须跟这个一同样 而且区分大小写 一般都是这个地方容易疏忽了
        //所以二级分类没变化
        string sql = @select cname as txt,id as vol from webclass where parentid= + cid;
        try
        {
            return getDs(sql);
        }
        catch
        {
            //throw;
            return null;
        }
    }
   

    /**//// <summary>
    /// 返回一个DataSet
    /// </summary>
    /// <param name=SQL></param>
    /// <returns></returns>
    public DataSet getDs(string SQL)
    {
        OleDbConnection conn = myConn();
        DataSet Ds = new DataSet();
        OleDbDataAdapter Da = new OleDbDataAdapter(SQL, conn);
        try
        {  
            Da.Fill(Ds);
            return Ds;
        }
        catch
        {
            return null;
            //throw;
        }

    }
    /**//// <summary>
    /// //数据绑定
    /// </summary>
    private void BindDc()
    {
        //第一个
        string sql = @select * from webclass where Parentid=0;
        ddl1.DataSource = getDs(sql);
        ddl1.DataTextField = cname;
        ddl1.DataValueField = id;
        ddl1.DataBind();
        if (ddl1.DataSource != null) ddl1.Attributes.Add(onchange, showNext(this.options[selectedIndex].value,ddl2););

      
        //可以先判断 DropDownList.SelectedItem.Value
        //第二个
        sql = @select * from webclass where parentid= + ddl1.SelectedItem.Value;
        ddl2.DataSource = getDs(sql);
        ddl2.DataTextField = cname;
        ddl2.DataValueField = id;
        ddl2.DataBind();
       
        //第三个
        if (ddl2.DataSource != null) ddl2.Attributes.Add(onchange, showNext(this.options[selectedIndex].value,ddl3););
        sql = @select * from webclass where parentid= + ddl2.SelectedItem.Value;
        ddl3.DataSource = getDs(sql);
        ddl3.DataTextField = cname;
        ddl3.DataValueField = id;
        ddl3.DataBind();
   
    }
}

default.aspx内容:


<%@ Page Language=C# AutoEventWireup=true CodeFile=Default.aspx.cs Inherits=_Default %>

<!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>AjaxPro实现无刷新三级联动</title>
</head>
<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 = _Default.getNextClass(sid).value; // 类的名称
          //alert(v);
          //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].txt; //这个地方需要注意区分大小写
                      var vol = v.Tables[0].Rows[i].vol; //跟dataset表的列名称要一致
                      slt.options.add(new Option(txt,vol));
                    }
                    }
           }   
           return;
        }
        -->
</script>
<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>
相关文件
http://www.cnblogs.com/Files/asboy/AjaxDropDownlist.rar
代码文件于2005-5-13更新过 列出了取值的方法


轻松实现无刷新三级联动菜单[VS2005与AjaxPro]
  • 上一篇文章:

  • 下一篇文章:
  •  热门文章
    普通文章 电子邮件改头换面 四公司畅谈未
    普通文章 PC病毒史上最声名狼藉的八大病
    普通文章 Rails系统中的AJAX开发技术简析
    普通文章 基于ASP.NET AJAX框架实现表单
    普通文章 开发ASP.NET AJAX客户端定制行
    普通文章 用JFreeChart对JSP报表进行增强
    普通文章 SQL Server 2005上的CLR和ADO.
    普通文章 SQL Server 2005的XML支持机制
    普通文章 Firefox中标签式浏览技巧大全
    普通文章 Tomcat中的Session和Cookie大揭
     
     推荐文章
    推荐文章 把Google地图嵌入网页 就是这么
    推荐文章 迅雷搜索候选资源出错的解决
    推荐文章 轻松去除迅雷里的各种广告和资
    推荐文章 突破限制 免费领养到QQ空间五级
    推荐文章 Rational统一过程RUP贴近中小软
    推荐文章 构建自己的轻量级XML DOM分析程
    推荐文章 WPS Office 2007技巧:妙用配置
    推荐文章 Excel 2007:求余数函数实用进阶
    推荐文章 浅谈ASP.NET的Postback
    推荐文章 软件开发中项目需求管理简述
     
     相关文章
    没有相关文章
    设为首页 | 加入收藏 | 广告合作 | 联系站长 | 版权申明 |
    动力中国为网友提供免费学习资料,可用资源,如果您认为我们的相关内容侵害到了您的权利请联系管理员
    Copyright © 2006-2008 domcn.org All Rights Reserved.