AjaxPro 返回 DataSet/DataTable/DataView 如何呈现?

此示例展示通过 AjaxPro 返回一个 DataSet 并在页面上通过 html table 输出。

主要代码
// js
ExpandedBlockStart.gif ContractedBlock.gif      < script  type ="text/javascript" > dot.gif
InBlock.gif    
function GetProductData()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
var cb = function(res) dot.gif{
InBlock.gif            
if(res.error) return alert("发生错误\n" + res.error.Message);
InBlock.gif            
//debugger;
InBlock.gif
            //alert(res);
InBlock.gif
            var ds = res.value;
InBlock.gif            
var tbl = ds.Tables[0]; 
InBlock.gif            
var tblHtml = "<table border=1>";
InBlock.gif            
InBlock.gif            
// 表头
InBlock.gif
            tblHtml += "<tr>";
ExpandedSubBlockStart.gifContractedSubBlock.gif            
for(var j = 0; j < tbl.Columns.length; j++dot.gif{
InBlock.gif                tblHtml 
+= "<th>" + tbl.Columns[j].Name + "</th>";
ExpandedSubBlockEnd.gif            }

InBlock.gif            tblHtml 
+= "</tr>";
InBlock.gif            
InBlock.gif            
// 数据
ExpandedSubBlockStart.gifContractedSubBlock.gif
            for(var i = 0; i < tbl.Rows.length; i++dot.gif{
InBlock.gif                tblHtml 
+= "<tr>";
ExpandedSubBlockStart.gifContractedSubBlock.gif                
for(var j = 0; j < tbl.Columns.length; j++dot.gif{
InBlock.gif                    tblHtml 
+= "<td>" + tbl.Rows[i][tbl.Columns[j].Name] + "</td>";
ExpandedSubBlockEnd.gif                }

InBlock.gif                tblHtml 
+= "</tr>";
ExpandedSubBlockEnd.gif            }

InBlock.gif            tblHtml 
+= "</table>";
InBlock.gif            
var divPro = document.getElementById("divPro");
InBlock.gif            divPro.innerHTML 
= tblHtml;
ExpandedSubBlockEnd.gif        }

InBlock.gif        AjaxProSample.GetProductSet(cb);        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif    
</ script >
None.gif

// .aspx.cs
None.gif [AjaxPro.AjaxNamespace( " AjaxProSample " )]
None.gif
public  partial  class  AjaxPro_ReturnDataSet : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        AjaxPro.Utility.RegisterTypeForAjax(
typeof(AjaxPro_ReturnDataSet));
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif
InBlock.gif    [AjaxPro.AjaxMethod]
InBlock.gif    
public static DataSet GetProductSet()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
return CreateSampleProductSet();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
sample data#region sample data
InBlock.gif
InBlock.gif    
static DataSet CreateSampleProductSet()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        DataSet ds 
= new DataSet();
InBlock.gif        ds.Tables.Add(CreateSampleProductData());
InBlock.gif        
return ds;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
static DataTable CreateSampleProductData()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        DataTable tbl 
= new DataTable("Products");
InBlock.gif
InBlock.gif        tbl.Columns.Add(
"ProductID"typeof(int));
InBlock.gif        tbl.Columns.Add(
"ProductName"typeof(string));
InBlock.gif        tbl.Columns.Add(
"UnitPrice"typeof(decimal));
InBlock.gif        tbl.Columns.Add(
"CategoryID"typeof(int));
InBlock.gif
InBlock.gif        tbl.Rows.Add(
1"Chai"181);
InBlock.gif        tbl.Rows.Add(
2"Chang"191);
InBlock.gif        tbl.Rows.Add(
3"Aniseed Syrup"102);
InBlock.gif        tbl.Rows.Add(
4"Chef Anton's Cajun Seasoning"222);
InBlock.gif        tbl.Rows.Add(
5"Chef Anton's Gumbo Mix"21.352);
InBlock.gif        tbl.Rows.Add(
47"Zaanse koeken"9.53);
InBlock.gif        tbl.Rows.Add(
48"Chocolade"12.753);
InBlock.gif        tbl.Rows.Add(
49"Maxilaku"203);
InBlock.gif
InBlock.gif        
return tbl;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockEnd.gif    
#endregion
    
ExpandedBlockEnd.gif}

None.gif


AjaxPro 支持直接返回 DataTable 和 DataView ,客户端读取方式同 DataSet

None.gif var  tbl  =  res.value;   //  直接访问 DataTable
None.gif

需要注意的是,返回 DataView,实际上是返回 DataView 关联的 DataTable 。

完整代码下载

转载于:https://www.cnblogs.com/Jinglecat/archive/2007/07/30/835850.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值