1.简单数据绑定定
 <!--ASP.NET 1.x data binding expression -->
<!--ASP.NET 1.x data binding expression --> <%# DataBinder.Eval(Container.DataItem, "Price") %>
<%# DataBinder.Eval(Container.DataItem, "Price") %> <!--Equivalent ASP.NET 2.0 data binding expression -->
<!--Equivalent ASP.NET 2.0 data binding expression --> <%# Eval("Price") %>
<%# Eval("Price") %> <!--XML data binding -->
<!--XML data binding --> <%# XPath("Price") %>
<%# XPath("Price") %> 控件名                                     控件描述
控件名                                     控件描述 SqlDataSource                       一切支持SQL语句的数据源控件
SqlDataSource                       一切支持SQL语句的数据源控件 AccessDataSource               Access数据源控件
AccessDataSource               Access数据源控件 XmlDataSource                     XML数据源控件
XmlDataSource                     XML数据源控件 ObjectDataSource                自行编写组件的数据源控件
ObjectDataSource                自行编写组件的数据源控件 SiteMapDataSource             页面导航控件的数据源控件
SiteMapDataSource             页面导航控件的数据源控件 名称                                               描述
名称                                               描述 ConnectionString                        连接数据库的连接字符串
ConnectionString                        连接数据库的连接字符串 SelectCommand                           用于执行查询的命令
SelectCommand                           用于执行查询的命令 InsertCommand                           用于执行插入的命令
InsertCommand                           用于执行插入的命令 UpdateCommand                        用于执行更新的命令
UpdateCommand                        用于执行更新的命令 DeleteCommand                          用于执行删除的命令
DeleteCommand                          用于执行删除的命令 DataSourceMode                        指定数据源类型是DataSet 或DataReader( 默认值= DataSet)
DataSourceMode                        指定数据源类型是DataSet 或DataReader( 默认值= DataSet) ProviderName                              指定供应商(默认值= SQL Server .NET provider)
ProviderName                              指定供应商(默认值= SQL Server .NET provider) 属性名                                         描述
属性名                                         描述 EnableCaching                           指定是否打开缓存(默认值= false)
EnableCaching                           指定是否打开缓存(默认值= false) CacheDuration                           指定结果被缓存多少妙
CacheDuration                           指定结果被缓存多少妙 CacheExpirationPolicy              指定缓存间隔是sliding 还是absolute
CacheExpirationPolicy              指定缓存间隔是sliding 还是absolute CacheKeyDependency             使缓存依赖于一个特定键值
CacheKeyDependency             使缓存依赖于一个特定键值 SqlCacheDependency               使缓存依赖于一个特定数据库实体
SqlCacheDependency               使缓存依赖于一个特定数据库实体 名称                                      描述
名称                                      描述 SelectParameters                为查询命令指定参数
SelectParameters                为查询命令指定参数 InsertParameters                为插入命令指定参数
InsertParameters                为插入命令指定参数 UpdateParameters              为更新命令指定参数
UpdateParameters              为更新命令指定参数 DeleteParameters               为删除命令指定参数
DeleteParameters               为删除命令指定参数 FilterParameters                 为过滤器命令指定参数
FilterParameters                 为过滤器命令指定参数 名称                                               描述
名称                                               描述 ControlParameter                         指定一个源自于控件的参数
ControlParameter                         指定一个源自于控件的参数 CookieParameter                          指定一个源自于cookie的参数
CookieParameter                          指定一个源自于cookie的参数 FormParameter                             指定一个源自于表单的参数
FormParameter                             指定一个源自于表单的参数 ProfileParameter                           指定一个源自于profile的参数
ProfileParameter                           指定一个源自于profile的参数 QueryStringParameter                 制定于一个来源于查询字符串的参数
QueryStringParameter                 制定于一个来源于查询字符串的参数 Parameter                                      为数据源绑定一个参数
Parameter                                      为数据源绑定一个参数 SessionParameter                        指定一个源自于session的参数
SessionParameter                        指定一个源自于session的参数 <asp:SqlDataSourceID="Countries" RunAt="server"
<asp:SqlDataSourceID="Countries" RunAt="server" ConnectionString="server=localhost;database=northwind;"
ConnectionString="server=localhost;database=northwind;" SelectCommand="select distinct country from customers order by country" />
SelectCommand="select distinct country from customers order by country" /> <asp:SqlDataSourceID="Customers" RunAt="server"
<asp:SqlDataSourceID="Customers" RunAt="server" ConnectionString="server=localhost;database=northwind;"
ConnectionString="server=localhost;database=northwind;" SelectCommand="select * from customers where country =@Country">
SelectCommand="select * from customers where country =@Country"> <SelectParameters>
<SelectParameters> <asp:ControlParameterName="Country" ControlID="MyDropDownList"
<asp:ControlParameterName="Country" ControlID="MyDropDownList" PropertyName="SelectedValue" />
PropertyName="SelectedValue" /> </SelectParameters>
</SelectParameters> </asp:SqlDataSource>
</asp:SqlDataSource> <asp:DropDownListID=" MyDropDownList" DataSourceID="Countries"
<asp:DropDownListID=" MyDropDownList" DataSourceID="Countries" DataTextField="country" AutoPostBack="true" RunAt="server" />
DataTextField="country" AutoPostBack="true" RunAt="server" /> <asp:DataGridDataSourceID="Customers" RunAt="server" />
<asp:DataGridDataSourceID="Customers" RunAt="server" /> <asp:SqlDataSourceID="Countries" RunAt="server"
<asp:SqlDataSourceID="Countries" RunAt="server" ConnectionString="server=localhost;database=northwind;"
ConnectionString="server=localhost;database=northwind;" SelectCommand="proc_GetCountries" />
SelectCommand="proc_GetCountries" /> <asp:SqlDataSourceID="Customers" RunAt="server"
<asp:SqlDataSourceID="Customers" RunAt="server" ConnectionString="server=localhost;database=northwind;"
ConnectionString="server=localhost;database=northwind;" SelectCommand="proc_GetCustomers">
SelectCommand="proc_GetCustomers"> <SelectParameters>
<SelectParameters> <asp:ControlParameterName="Country" ControlID="MyDropDownList"
<asp:ControlParameterName="Country" ControlID="MyDropDownList" PropertyName="SelectedValue" />
PropertyName="SelectedValue" /> </SelectParameters>
</SelectParameters> </asp:SqlDataSource>
</asp:SqlDataSource> <asp:DropDownListID="MyDropDownList" DataSourceID="Countries"
<asp:DropDownListID="MyDropDownList" DataSourceID="Countries" DataTextField="country" AutoPostBack="true" RunAt="server" />
DataTextField="country" AutoPostBack="true" RunAt="server" /> <asp:DataGridDataSourceID="Customers" RunAt="server" />
<asp:DataGridDataSourceID="Customers" RunAt="server" /> CREATE PROCEDURE proc_GetCustomers
CREATE PROCEDURE proc_GetCustomers @Country nvarchar(32) AS
@Country nvarchar(32) AS SELECT * FROM Customers
SELECT * FROM Customers WHERE Country = @Country
WHERE Country = @Country GO
GO CREATE PROCEDURE proc_GetCustomers
CREATE PROCEDURE proc_GetCustomers CREATE PROCEDURE proc_GetCountriesAS
CREATE PROCEDURE proc_GetCountriesAS SELECT DISTINCT Country
SELECT DISTINCT Country FROM Customers
FROM Customers ORDER BY Country
ORDER BY Country GO
GO支持缓存与XSL 转换,只支持查询绑定,不支持更新
 <asp:XmlDataSourceID="Rates" DataFile="Rates.xml" RunAt="server" />
<asp:XmlDataSourceID="Rates" DataFile="Rates.xml" RunAt="server" /> <asp:TreeViewID="MyTreeView" DataSourceID="Rates" RunAt="server" />
<asp:TreeViewID="MyTreeView" DataSourceID="Rates" RunAt="server" /> 名称                                                    描述
名称                                                    描述 DataFile XML                                    数据文件的路径
DataFile XML                                    数据文件的路径 TransformFile                                    含有XSL 风格定义的数据文件路径
TransformFile                                    含有XSL 风格定义的数据文件路径 EnableCaching                                  指定是否开启cache (默认值= false)
EnableCaching                                  指定是否开启cache (默认值= false) XPath XPath                                      表达式用来确认数据
XPath XPath                                      表达式用来确认数据 CacheDuration                                  以秒为单位的时间间隔
CacheDuration                                  以秒为单位的时间间隔 CacheExpirationPolicy                     指定时间间隔是sliding 还是absolute
CacheExpirationPolicy                     指定时间间隔是sliding 还是absolute CacheKeyDependency                    创建缓存依存于某个键
CacheKeyDependency                    创建缓存依存于某个键
从数据组件绑定数据,提供中间件的数据绑定,使数据访问和UI脱离,两种数据绑定方式
SelectMethod, InsertMethod, UpdateMethod,and DeleteMethod
可选择是否使用缓存,可选择是否使用参数
4.1 ODS 关键属性
 ODS 关键属性
ODS 关键属性 名称                                                         描述
名称                                                         描述 InsertParameters                                    指定插入方法参数
InsertParameters                                    指定插入方法参数 UpdateParameters                                 指定更新方法参数
UpdateParameters                                 指定更新方法参数 DeleteParameters                                   指定删除方法参数
DeleteParameters                                   指定删除方法参数 SelectParameters                                    指定查询方法参数
SelectParameters                                    指定查询方法参数 CacheDuration                                       缓存间隔时间以秒为单位
CacheDuration                                       缓存间隔时间以秒为单位 SqlCacheDependency                          基于某个数据实体的缓存
SqlCacheDependency                          基于某个数据实体的缓存 创建和清除
创建和清除 ObjectDataSource.SelectMethod 可以使用静态方法也可以使用一个类的新实例
ObjectDataSource.SelectMethod 可以使用静态方法也可以使用一个类的新实例 如果使用实例方法:ODS 在每次调用的时候创建一个新实例类必须具有公共的建构函数
如果使用实例方法:ODS 在每次调用的时候创建一个新实例类必须具有公共的建构函数 使用ObjectCreated 和ObjectDisposing 元素可以初始化和撤销函数
使用ObjectCreated 和ObjectDisposing 元素可以初始化和撤销函数支持复杂的数据单元格类型,包括CheckBoxFields在<Columns> 元素中声明高可定制的用户界面
gridView 列类型:
 名称                                             描述
名称                                             描述 BoundField                                 显示数据库中取出的文本
BoundField                                 显示数据库中取出的文本 ButtonField                                显示按钮
ButtonField                                显示按钮 CheckBoxField                           使用check boxes显示一个boolean型变量
CheckBoxField                           使用check boxes显示一个boolean型变量 HyperLinkField                          显示一个超链接
HyperLinkField                          显示一个超链接 TemplateField                            显示一个自定义的HTML模板
TemplateField                            显示一个自定义的HTML模板 CommandField                          显示一个查询或者编辑按钮
CommandField                          显示一个查询或者编辑按钮 ImageField                                 显示一个图片
ImageField                                 显示一个图片 先入胜利
先入胜利 如果数据在取出之后被改变,则修改失败
如果数据在取出之后被改变,则修改失败 UpdateCommand结构构成指定ConflictDetection=“CompareAllValues”来实现
UpdateCommand结构构成指定ConflictDetection=“CompareAllValues”来实现 后入胜利
后入胜利 无论数据是否被修改,该修改都会成功
无论数据是否被修改,该修改都会成功 UpdateCommand结构构成指定ConflictDetection=“OverwriteChanges”来实现
UpdateCommand结构构成指定ConflictDetection=“OverwriteChanges”来实现 <asp:SqlDataSourceID="Employees" RunAt="server"
<asp:SqlDataSourceID="Employees" RunAt="server" ConnectionString="server=localhost;database=northwind;"
ConnectionString="server=localhost;database=northwind;" SelectCommand="select employeeid, lastname, firstnamefrom employees"
SelectCommand="select employeeid, lastname, firstnamefrom employees" UpdateCommand="update employees set lastname=@lastname, firstname=
UpdateCommand="update employees set lastname=@lastname, firstname= @firstnamewhere employeeid=@original_employeeid and lastname=
@firstnamewhere employeeid=@original_employeeid and lastname= @original_lastnameand firstname=@original_firstname"
@original_lastnameand firstname=@original_firstname" ConflictDetection="CompareAllValues">
ConflictDetection="CompareAllValues"> <UpdateParameters>
<UpdateParameters> <asp:ParameterName="EmployeeID" Type="Int32" />
<asp:ParameterName="EmployeeID" Type="Int32" /> <asp:ParameterName="lastname" Type="String" />
<asp:ParameterName="lastname" Type="String" /> <asp:ParameterName="firstname" Type="String" />
<asp:ParameterName="firstname" Type="String" /> </UpdateParameters>
</UpdateParameters> </asp:SqlDataSource>
</asp:SqlDataSource> <asp:GridViewDataSourceID="Employees" Width="100%" RunAt="server"
<asp:GridViewDataSourceID="Employees" Width="100%" RunAt="server" DataKeyNames="EmployeeID" AutoGenerateEditButton="true" />
DataKeyNames="EmployeeID" AutoGenerateEditButton="true" />数据更新后控件调用的事件GridView.RowUpdated,DetailsView.ItemUpdated,SqlDataSource.Updated, etc.
处理“status”的事件,无论数据库是否异常允许数据库异常被处理或者再次抛弃,显示多少数据库行被修改
处理更新错误
 <asp:SqlDataSourceID="Employees" RunAt="server"
<asp:SqlDataSourceID="Employees" RunAt="server"  UpdateCommand="" OnUpdated="OnUpdateComplete">
UpdateCommand="" OnUpdated="OnUpdateComplete">
 </asp:SqlDataSource>
</asp:SqlDataSource>
 void OnUpdateComplete (Object source, SqlDataSourceStatusEventsArgse)
void OnUpdateComplete (Object source, SqlDataSourceStatusEventsArgse) {
{ if (e.Exception!= null) {
if (e.Exception!= null) { // Exception thrown. Set e.ExceptionHandledto true to prevent
// Exception thrown. Set e.ExceptionHandledto true to prevent // the SqlDataSourcefrom throwing an exception, or leave it set
// the SqlDataSourcefrom throwing an exception, or leave it set // to false to allow SqlDataSourceto rethrowthe exception
// to false to allow SqlDataSourceto rethrowthe exception }
} else if (e.AffectedRows== 0) {
else if (e.AffectedRows== 0) { // No exception was thrown, but no records were updated,either.
// No exception was thrown, but no records were updated,either. // Might want to let the user know that the update failed
// Might want to let the user know that the update failed }
} }
} 
                    
                     
                    
                 
                    
                 
 
        

 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号