基于Ajaxpro.net的客户端数据绑定控件(DataList

 
基于Ajaxpro.net的客户端数据绑定控件(DataList)

Ajaxpro.net 在客户端实现了.new Ajax.Web.DataSet(); 和new Ajax.Web.DataTable();    

因此,考虑数据也应该可以实现客户端的绑定,于是用Javascript实现了DataList的简单功能:

AjaxPro.net 下载:<a href="http://www.ajaxpro.info/">AjaxPro</a>

cs代码:

  protected void Page_Load(object sender, EventArgs e)

        {



            AjaxPro.Utility.RegisterTypeForAjax(typeof(index));

          



        }

页面代码:

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

<!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">

    <title>无标题页</title>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <style type="text/css">

        .table{background:#000;border:0;}

        .table td{background:#fff;}

    </style>

</head>

<body>

    <form id="form1" runat="server">

            <input type="button" οnclick="Test()" value="test" />

                <div id="ShowData">

                    <asp:GridView ID="GridView1" runat="server">

                    </asp:GridView>

                </div>

            <script language="javascript" type="text/javascript">

                function $(sid){return document.getElementById(sid);}

                function TestDs(){

                    var ds = new Ajax.Web.DataSet();                    

                    var dt = new Ajax.Web.DataTable();             

                    dt.addColumn("FirstName", "System.String");

                    dt.addColumn("Age", "System.Int32");                     

                    dt.addRow({"FirstName":"Michael","Age":28});

                    dt.addRow({"FirstName":"Tanja","Age":25});                     

                    ds.addTable(dt);

                    return ds;

                }

                function Test(){

                    var t=new DataList(document.getElementById('ShowData'),TestDs());

                    t.CreateTable('shenjk',200,0,'table',1,1);

                    t.HeaderTemplate(new Array('姓名','年龄'));

                    t.DataBind();

//                    var t1=new DataList('',TestDs());

//                    t1.CreateTable('shenjk1',200,0,'table');

//                    t1.DataBind();

                }

                /*------------------------------------

                 *类:DataList 用于绑定ds

                 *参数:container 控件容器.

                 *      ds       数据源

                 *-------------------------------------*/

                var DataList=function(container,ds){

                    this.container=(typeof container=='undefined' || typeof container!='object')?document.body:container;                    

                    this.dt=ds.Tables[0];

                    this.rowsCount=ds.Tables[0].Rows.length;

                    this.columnsCount=ds.Tables[0].Columns.length;

                };

                DataList.prototype={

                    //创建数据表格

                    CreateTable:function(_Id,width,height,cssName,cellPadding,cellSpacing){

                        var Id='_UNDEDINEDID';

                        if(_Id!=null && _Id!='')Id=_Id;

                        if($(Id)==null){

                            var table=document.createElement('table');

                            table.id=Id;

                            this.container.appendChild(table);               

                            this.table=table;

                        }else{

                            this.table=$(Id);

                            this.table.removeChild(this.table.getElementsByTagName('tbody')[0]);//清除现有数据,处理方法有待改进                                                

                        }

                        if(width!=null && width!='' && parseInt(width)!=0)this.table['style']['width']=width+'px';

                        if(height!=null && height!='' && parseInt(height)!=0)this.table['style']['height']=height+'px';

                        if(cssName!=null && typeof(cssName=='string') && cssName!='')this.table.className=cssName;   

                        if(cellPadding!=null && typeof(cellPadding)=='number')this.table.setAttribute('cellPadding',cellPadding); 

                        if(cellSpacing!=null && typeof(cellSpacing)=='number')this.table.setAttribute('cellSpacing',cellPadding);                   

                    },

                    //自定义表格头部内容

                    HeaderTemplate:function(header){                

                        if(header.length!=this.columnsCount){

                            throw('HeaderTemplate length is not matched.');                            

                        }else{

                            var r_head=this.table.insertRow(0);

                            for(var i=0;i<this.columnsCount;i++){

                                var cell=r_head.insertCell(i);

                                cell.innerHTML=header[i];

                            }

                        }

                    },

                    //数据绑定

                    DataBind:function(){    

                        //绑定头部 

                        if(this.table.rows.length==0){   //如果没有自定头部,则采用数据字段作为头部.

                            var r_head=this.table.insertRow(0);

                            for(var i=0;i<this.columnsCount;i++){

                               var cell=r_head.insertCell(i);

                               cell.innerHTML=this.dt.Columns[i].Name;

                            }

                        }

                        //绑定数据

                        for(var i=0;i<this.rowsCount;i++){

                            var r=this.table.insertRow(i+1);

                            for(var j=0;j<this.columnsCount;j++){

                                var cell=r.insertCell(j);

                                cell.innerHTML=this.dt.Rows[i][this.dt.Columns[j].Name];

                            }

                        }                   

                    }

                };                

            </script>

    </form>

</body>

</html>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值