发布于 2016-06-15 05:38:54 | 92 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

ASP.NET

ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。


本文主要介绍ObjectDataSource控件和DataObjectTypeName属性的用法,希望能给小伙伴们一些帮助。

一、ObjectDataSource 控件说明

获取或设置某个类的名称,ObjectDataSource 控件将该类用于更新、插入或删除数据操作中的参数,而不是从数据绑定控件传递个别的值。

您不用指定传递给 Update、Insert 和 Delete 方法的多个参数,而是可以创建一个累计多个数据字段值的对象。仅给方法传递这一个对象,而不是多个参数。

绑定到数据绑定控件的 ObjectDataSource 控件的默认行为是,数据绑定控件为数据源中的每个参数创建一个 Parameter 对象。如果业务对象有很多字段,则结果方法也有很多字段。DataObjectTypeName 属性允许您为每个数据字段都指定一个具有属性的类型。这样,运行时不是给方法传递多个参数,而是创建一个对象并设置它的所有属性。这一个对象添加到方法调用的参数集合中。

二、DataObjectTypeName 属性的使用

DataObjectTypeName 属性指定的类型必须有一个不带参数的默认构造函数,以便 ObjectDataSource 控件可以创建此类型的实例。此类型还必须具有可设置的属性,允许 ObjectDataSource 控件用数据绑定控件传递的值填充对象。ObjectDataSource 控件的属性名应该与数据绑定控件传递的值的参数名完全匹配。

当设置了 DataObjectTypeName 属性并且 ObjectDataSource 控件与数据绑定控件关联时,由 InsertMethod 和 DeleteMethod 属性指定的方法必须各有一个在 DataObjectTypeName 属性中指定的类型的参数。如果 ConflictDetection 属性设置为 OverwriteChanges 值,则由 UpdateMethod 属性指定的方法必须有一个在 DataObjectTypeName 属性中指定的类型的参数。如果 ConflictDetection 属性设置为 CompareAllValues 值,则由 UpdateMethod 属性指定的方法必须有两个在 DataObjectTypeName 属性中指定的类型的参数。第一个参数包含原始值;第二个参数包含新值。

DataObjectTypeName 属性委托给与 ObjectDataSource 控件关联的 ObjectDataSourceView 的 DataObjectTypeName 属性。

三、示例代码

下面的代码示例演示如何使用 DataObjectTypeName 属性,实现一个将所有参数值合并为一个对象的类型。AggregateData 类的选择方法返回一个有两个名为 Name 和 Number 的列的 DataTable 对象。同样,NewData 类定义两个读/写属性 Name 和 Number。AggregateData 类的 Insert 方法带 NewData 类型的一个参数。ObjectDataSource 的 TypeName 属性设置为 AggregateData,DataObjectTypeName 属性设置为 NewData。

前台代码:


<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" Assembly="Samples.AspNet.CS" %>
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>ObjectDataSource - DataObjectTypeName Property Example</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:DetailsView 
      ID="DetailsView1" 
      runat="server" 
      AllowPaging="True" 
      AutoGenerateInsertButton="True"
      DataSourceID="ObjectDataSource1" 
      Height="50px" 
      Width="125px">
    </asp:DetailsView>
    <asp:ObjectDataSource 
      ID="ObjectDataSource1" 
      runat="server" 
      DataObjectTypeName="Samples.AspNet.CS.NewData"
      InsertMethod="Insert" 
      SelectMethod="Select" 
      TypeName="Samples.AspNet.CS.AggregateData">
    </asp:ObjectDataSource>
  </div>
  </form>
</body>
</html>

后台代码:


using System;
using System.Data;
using System.Configuration;
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;

namespace Samples.AspNet.CS
{

  /// <summary>
  /// Summary description for AggregateData
  /// </summary>
  public class AggregateData
  {

    public AggregateData()
    {

    }

    static DataTable table;

    private DataTable CreateData()
    {
      table = new DataTable();
      table.Columns.Add("Name", typeof(string));
      table.Columns.Add("Number", typeof(int));
      table.Rows.Add(new object[] { "one", 1 });
      table.Rows.Add(new object[] { "two", 2 });
      table.Rows.Add(new object[] { "three", 3 });
      return table;
    }

    public DataTable Select()
    {
      if (table == null)
      {
        return CreateData();
      }
      else
      {
        return table;
      }
    }

    public int Insert(NewData newRecord)
    {
      table.Rows.Add(new object[] { newRecord.Name, newRecord.Number });
      return 1;
    }
  }

  public class NewData
  {
    private string nameValue;
    private int numberValue;

    public string Name
    {
      get { return nameValue; }
      set { nameValue = value; }
    }

    public int Number
    {
      get { return numberValue; }
      set { numberValue = value; }
    }

  }
}



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务