服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - ASP.NET中 ObjectDataSource控件的DataObjectTypeName属性

ASP.NET中 ObjectDataSource控件的DataObjectTypeName属性

2020-01-06 13:56Fskjb ASP.NET教程

本文主要介绍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。

前台代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<%@ 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>

后台代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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; }
    }
 
  }
}

延伸 · 阅读

精彩推荐