ObjectDataSource&&Gridview
2006-09-28 18:03
288 查看
ASP.NET 1.x 中DataGrid的功能在ASP.NET 2.0中得到了增强。DataGrid增强的功能在2.0中分散到了GridView、DetailsView 和FormView 三个控件中。今天讨论一下GridView与DetailsView如何联合使用。
GridView和DetailsView的用法与1.x中有很大的不同,主要是2.0新增了4个DataSource控件。其中有3个直接或间接继承自System.Web.UI.DataSourceControl 另一个(SiteMapDataSource)继承自System.Web.UI.HierarchicalDataSourceControl。在实际应用中,用的较多的是ObjectDataSource。ObjectDataSource控件支持Web开发的三层架构,把表示层和数据层真正分开了。
ObjectDataSource允许指定一个类给它的TypeName属性。同时指定这个类的一个公共方法给它的SelectMethod属性。这可以是一个中间层的类,在这个类中可以调用数据层的方法也可以与数据层完全无关。与ObjectDataSource相连的GridView或DetailsView则只负责显示,而与数据逻辑无关。
要在GridView中显示数据只要把一个ObjectDataSource的ID赋给GridView的DataSourceID属性(实际上GridView中绑定的数据源只需要实现IEnumerable 或 ItypedList接口即可)。
void Page_Load(object sender, EventArgs e)
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" InsertMethod="InsertStudent" SelectMethod="GetStudents" TypeName="Student" UpdateMethod="UpdateStudent" OldValuesParameterFormatString="Original_{0}" DataObjectTypeName="StudentData" DeleteMethod="DeleteStudent">
</asp:ObjectDataSource>
<div>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" CellPadding="4" DataSourceID="ObjectDataSource1" ForeColor="#333333" GridLines="None" DataKeyNames="ID" AutoGenerateColumns="False">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" DataObjectTypeName="StudentData"
DeleteMethod="DeleteStudent" InsertMethod="InsertStudent" SelectMethod="SelectStudent"
TypeName="Student" UpdateMethod="UpdateStudent">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="i" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" CellPadding="8" DataSourceID="ObjectDataSource2" ForeColor="#333333" GridLines="None" AutoGenerateRows="False" Font-Names="宋体" Font-Overline="False" Font-Size="10pt" Font-Strikeout="False" Font-Underline="False" DataKeyNames="ID">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
<RowStyle BackColor="#EFF3FB" />
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Left" />
<Fields>
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:CheckBoxField DataField="Sex" HeaderText="Sex" SortExpression="Sex" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:DetailsView>
</form>
</body>
</html>
后台代码:
Default.aspx.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;
public partial class _Default : System.Web.UI.Page
App_Code/Students.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Generic;
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 class Student
public class StudentData
注意:Students.cs必须放在App_Code目录里,或者编译后放在bin目录里,否则前面选择Business Object时找不到这个类。
今天只讨论了ASP.NET 2.0中GridView与DetailsView的联合使用的问题,GridView和DetailsView的用法的其它方面将会在后续的文章中陆续介绍。
GridView和DetailsView的用法与1.x中有很大的不同,主要是2.0新增了4个DataSource控件。其中有3个直接或间接继承自System.Web.UI.DataSourceControl 另一个(SiteMapDataSource)继承自System.Web.UI.HierarchicalDataSourceControl。在实际应用中,用的较多的是ObjectDataSource。ObjectDataSource控件支持Web开发的三层架构,把表示层和数据层真正分开了。
ObjectDataSource允许指定一个类给它的TypeName属性。同时指定这个类的一个公共方法给它的SelectMethod属性。这可以是一个中间层的类,在这个类中可以调用数据层的方法也可以与数据层完全无关。与ObjectDataSource相连的GridView或DetailsView则只负责显示,而与数据逻辑无关。
要在GridView中显示数据只要把一个ObjectDataSource的ID赋给GridView的DataSourceID属性(实际上GridView中绑定的数据源只需要实现IEnumerable 或 ItypedList接口即可)。
void Page_Load(object sender, EventArgs e)
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" InsertMethod="InsertStudent" SelectMethod="GetStudents" TypeName="Student" UpdateMethod="UpdateStudent" OldValuesParameterFormatString="Original_{0}" DataObjectTypeName="StudentData" DeleteMethod="DeleteStudent">
</asp:ObjectDataSource>
<div>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" CellPadding="4" DataSourceID="ObjectDataSource1" ForeColor="#333333" GridLines="None" DataKeyNames="ID" AutoGenerateColumns="False">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" DataObjectTypeName="StudentData"
DeleteMethod="DeleteStudent" InsertMethod="InsertStudent" SelectMethod="SelectStudent"
TypeName="Student" UpdateMethod="UpdateStudent">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="i" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</div>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" CellPadding="8" DataSourceID="ObjectDataSource2" ForeColor="#333333" GridLines="None" AutoGenerateRows="False" Font-Names="宋体" Font-Overline="False" Font-Size="10pt" Font-Strikeout="False" Font-Underline="False" DataKeyNames="ID">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
<RowStyle BackColor="#EFF3FB" />
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Left" />
<Fields>
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:CheckBoxField DataField="Sex" HeaderText="Sex" SortExpression="Sex" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:DetailsView>
</form>
</body>
</html>
后台代码:
Default.aspx.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;
public partial class _Default : System.Web.UI.Page
App_Code/Students.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Generic;
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 class Student
public class StudentData
注意:Students.cs必须放在App_Code目录里,或者编译后放在bin目录里,否则前面选择Business Object时找不到这个类。
今天只讨论了ASP.NET 2.0中GridView与DetailsView的联合使用的问题,GridView和DetailsView的用法的其它方面将会在后续的文章中陆续介绍。
相关文章推荐
- Gridview, ObjectDataSource Making life easy
- asp:ObjectDataSource+asp:GridView 实现真分页
- Asp.net学习笔记----使用GridView+ObjectDataSource进行自定义分页排序
- GridView 绑定 ObjectDataSource
- gridview+objectdatasource+aspnetpager整合实例
- GridView&ObjectDataSource新特性小记 懒人篇(一) 分页上路
- GridViewObjectDataSource
- 【转贴】GridView With ObjectDataSource
- ObjectDataSource + Sql Server 2005 + GridView的自定义分页
- GridView&ObjectDataSource 新特性小记 懒人篇(二) 分页加速
- 一个objectdatasource+gridview+multiple row update的例子
- GridView + ObjectDatasource 的一个范例代码
- gridview+objectdatasource+aspnetpager整合实例
- GridView/ObjectDataSource和AspNetPager整合示例
- GridView/ListView + ObjectDataSource + Entity 实现Webform列表真分页
- [C#]ADO.NET #3 (GridView + SqlDataSource)完全手写、后置程序代码,兼论 SqlDataSource与UpdateParameter/DeleteParameter的用法
- ASP.NET GridView + SqlDataSource + 分页+ 搜索
- 绑定SqlDataSource的Gridview字符串字段长度截取
- “GridView1”上同时定义了 DataSource 和 DataSourceID。请移除一个定义。
- Dropdownlist+objectdatasource设定“请选择”默认选项