All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Windows下Apache+Tomcat+MySQL+jsp+php的服务器整合配置经验总结_[PHP教程]
关于线程的参数、“返回值”、及线程的中止_.net资料_编程技术
菜鸟学习php一个月总结_[PHP教程]
在ASP.NET,Atlas中创建自定义Behavior_[Asp.Net教程]
在PHP中开发XML应用程序之基础篇_php资料_编程技术
高度100%的绝对定位自适应布局技巧_[Html教程]
如何用.NET,Array类的Sort方法分类数值_.net资料_编程技术
asp.net初学者:petshop4.0设计说明_.net资料_编程技术
PHP技巧:PHP中密码加密函数_[PHP教程]
在ASP.NET,中实现单点登录_[Asp.Net教程]
Reading number is top 10 pictures
Nikon microscopic photography of the first three
Average female college students1
Lewd,it is too lewd.
A beautiful girl to bud3
Sora aoi in China3
Take you to walk into the most true north Korea rural3
Azusa Yamamoto1
青春清纯美女大集合1
乳娘帕梅拉安德森4
Chinese paper-cut grilles art appreciation5
Download software ranking
传奇私服架设教程
徐若瑄成名作“魔鬼天使”
Love the forty days
双旗镇刀客A
Unix video tutorial3
Wild things 2
Unix video tutorial7
好色的外科大夫
C#与.NET技术平台实战演练
XML+Web+Service开发教程
归海一刀 published in(发表于) 2014/1/30 1:12:22 Edit(编辑)
温故知新ASP.NET,2.0(C#)(8),-,DataSourceControl(数据源控件)_[Asp.Net教程]

温故知新ASP.NET,2.0(C#)(8),-,DataSourceControl(数据源控件)_[Asp.Net教程]

温故知新ASP.NET 2.0(C#)(8) - DataSourceControl(数据源控件)_[Asp.Net教程]

介绍
在 ASP.NET 2.0 中有几个新的数据源控件,例如,SqlDataSource、ObjectDataSource、XmlDataSource、AccessDataSource 和 SiteMapDataSource。它们全都可以用来从它们各自类型的数据源中检索数据,并且可以绑定到各种数据绑定控件。数据源控件减少了为检索和绑定数据甚至对数据进行排序、分页或编辑而需要编写的自定义代码的数量。


其中 ObjectDataSource 控件可针对各种基础数据存储区(如 SQL 数据库或 XML)启用声明性数据绑定模型。因为页开发人员也常常将数据检索(也可能包含业务逻辑)封装在一个组件对象中,从而在呈现页和数据提供程序之间引入另一个层。ObjectDataSource 控件允许开发人员使用此传统的三层结构构造应用程序,同时仍然能够利用 ASP.NET 中的声明性数据绑定模型的易用性优点。



关键
1、在数据层创建 强类型的DataSet和TableAdapter,TableAdapter查询可以使用现有的存储过程。注:直接把表或存储过程拖进来会自动创建TableAdapter


2、中间层的类用[System.ComponentModel.DataObject]声明为数据组件,CRUD方法分别对应[DataObjectMethod(DataObjectMethodType.Insert)],[DataObjectMethod(DataObjectMethodType.Select)],[DataObjectMethod(DataObjectMethodType.Update)],[DataObjectMethod(DataObjectMethodType.Delete)]


3、web层使用ObjectDataSource展现数据,ObjectDataSource就相当于一个代理。ObjectDataSource只是查找具有匹配的参数名称的方法,它不会使用参数的 Type 或 Size,而只是对参数的名称进行匹配


4、其它
·有ConvertEmptyStringToNull属性,默认是true。另外还有Direction属性
·注意的这几个属性NullDisplayText,HtmlEncode,ApplyFormatInEditMode,InsertVisible,DataFormatString,ReadOnly
·DataKeyNames有多个值的话用“,”分隔,用绑定的话给其加一个“PropertyName”属性,值类似如下“SelectedDataKey.Values[0]”
·关于绑定:简单属性<%# custID %>;集合;表达式<%# ( customer.FirstName + " " + customer.LastName ) %>;方法<%# GetBalance(custID) %>
·<%# Eval("field1") %> 和 <%# Bind("field1") %>,Eval是单向绑定,Bind是双向邦定
·有一个OldValuesParameterFormatString属性一般不用,不过如果要处理乐观并发之类的就会用到。当该属性的值为“original_{0}”的时候“original_参数名”则为初始值
·还有一些如编程方式给参数赋值,错误处理,得到返回值之类的请看源码



示例
相关存储过程和数据层略,见源码


单例模式的实现
App_Code/Singleton.cs
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;


/**////


/// Singleton 的摘要说明
///

public class Singleton where T : new()
{
public static T Instance
{
get { return SingletonCreator.instance; }
}


class SingletonCreator
{
internal static readonly T instance = new T();
}
}


中间层代码
App_Code/Test.cs
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;


using TestDatabaseTableAdapters;
using System.ComponentModel;


/**////


/// Test 的摘要说明
///

[DataObject]
public class Test
{
[DataObjectMethod(DataObjectMethodType.Select, true)]
public TestDatabase.TestDataTable GetTest()
{
return Singleton.Instance.GetTest();
}


[DataObjectMethod(DataObjectMethodType.Select, false)]
public TestDatabase.TestDataTable GetTestById(int id)
{
return Singleton.Instance.GetTestById(id);
}


[DataObjectMethod(DataObjectMethodType.Insert, true)]
public int?[] InsertTest(int? parentId, string name, DateTime? publishTime, decimal? price, bool? isGood, out int? minId)
{
// 仅为说明如何做错误处理
if (String.IsNullOrEmpty(name))
throw new ArgumentException("参数不能是空", "name");


int? id = null;
int? count = null;
minId = null;


Singleton.Instance.InsertTest(parentId, name, publishTime, price, isGood, ref id, ref count, ref minId);
return new int?[] { id, count };
}


[DataObjectMethod(DataObjectMethodType.Delete, true)]
public int? DeleteTest(int id)
{
int? rowAffected;


rowAffected = Singleton.Instance.DeleteTest(id);
return rowAffected;
}


[DataObjectMethod(DataObjectMethodType.Update, true)]
public int? UpdateTest(int? id, int? parentId, string name, DateTime? publishTime, decimal? price, bool? isGood)
{
int? rowAffected;


rowAffected = Singleton.Instance.UpdateTest(id, parentId, name, publishTime, price, isGood);
return rowAffected;
}
}


DataSourceControl/Test.aspx
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Test.aspx.cs"
Inherits="DataSourceControl_Test" Title="数据源控件测试" %>












DataKeyNames="Id" DataSourceID="ObjectDataSource1" Height="50px" Width="125px" OnItemInserted="DetailsView1_ItemInserted">

SortExpression="Id" />


InsertVisible="False" />











SortExpression="Id" />














DataSourceID="ObjectDataSource1" AllowPaging="True" AllowSorting="True" OnRowUpdating="GridView1_RowUpdating">


SortExpression="Id" />
NullDisplayText="我的值是NULL" />

ReadOnly="true" />





TypeName="Test" InsertMethod="InsertTest" DeleteMethod="DeleteTest" UpdateMethod="UpdateTest"
OnInserting="ObjectDataSource1_Inserting" OnInserted="ObjectDataSource1_Inserted">

<%--ConvertEmptyStringToNull属性默认为true--%>



















SelectMethod="GetTestById" TypeName="Test">

Type="Int32" />



DataSourceControl/Test.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;


public partial class DataSourceControl_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}


protected void ObjectDataSource1_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
{
// 编程方式给参数赋值
if (e.InputParameters["publishTime"] == null)
{
e.InputParameters["publishTime"] = DateTime.Now;
}
}


protected void ObjectDataSource1_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
// 错误处理
if (e.Exception != null)
{
if (e.Exception.InnerException != null)
{
Exception inner = e.Exception.InnerException;


if (inner is ArgumentException)
{
string paramName = ((ArgumentException)inner).ParamName;
lblMsg.Text = string.Concat("参数 ", paramName, " 有问题");
}
}
}
else
{
int?[] ary = (int?[])e.ReturnValue;
lblMsg.Text = "新插入信息的ID是:" + ary[0].ToString();
lblMsg.Text += "
数据总数是:" + ary[1].ToString();


lblMsg.Text += "
最小ID是:" + e.OutputParameters["minId"].ToString();
}
}


protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
if (e.Exception != null)
{
// 错误已处理
e.ExceptionHandled = true;
// DetailsView保持插入状态
e.KeepInInsertMode = true;
}
}


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// 价格格式转换为decimal格式
if (e.NewValues["Price"] != null)
e.NewValues["Price"] = decimal.Parse(e.NewValues["Price"].ToString(), System.Globalization.NumberStyles.Currency);
}
}


[源码下载]


作者:webabcd







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.