Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)delvpublished in(发表于) 2014/1/6 8:48:11 ASP.NET,2.0之ObjectDataSourc控件_[Asp.Net教程]
ASP.NET 2.0之ObjectDataSourc控件_[Asp.Net教程]
学习asp.net2.0有一段时间了,我不是从1.1开始的,所以,也说不出2.0相对于1.1来讲有多大的变化或是在操作上有多大的便利,我这里只把我学习2.0的一些小经验与大家分享.
2.0中有一个ObjectDataSource数据源控件,通过它,可以把中间层或其它层的数据绑定到dropdownlist/datalist/repeater/gridview等数据控件中,为什么这里只ObjectDataSource而不提SqlDataSource或其它的数据源控件呢?因为我觉得SqlDataSource体现不出asp.net在进行项目开发的多层性,在用SqlDataSource的过程中,所有的数据库连接串、sql语句都会在.aspx页面中显示出来,为以后的维护再来诸多不便.为了体现出asp.net的多层开发,我选择了ObjectDataSource,通过它,可以体现出asp.net进行多层开发的优势.首先,我用的数据库是MS sqlserver2000,开发工具当然是vs2005了.
大家先看一下我的解决方案
这里采用了通用的多层架构,当然,只是简单的实体层/数据层和表现层,这里只为了演示.在这个小项目中,读取的是数据库中一个名为Links数据表的内容.
Entity层:实体层.这里只是对于LinkEntity实体的一个描述.
using System;using System.Collections.Generic;using System.Text;namespace Blog.Entity{ public class LinksEntity { private string _title; private string _description; private string _url; private string _logo; private int _id; public int ID { get { return this._id; } set { this._id = value; } } public string Title { get { return this._title; } set { this._title = value; } } public string Description { get { return this._description; } set { this._description = value; } } public string Url { get { return this._url; } set { this._url = value; } } public string Logo { get { return this._logo; } set { this._logo = value; } } }}
DAL层:看名字就知道是一个数据访问层了,所有的数据库读写操作都会在这里.
using System;using System.Collections.Generic;using System.Collections;using System.Text;using System.Configuration;using System.Web.Configuration;using System.Data.SqlClient; //使用ms sqlserverusing System.Data;namespace Blog.DAL{ public class Links { private string _connStr = WebConfigurationManager.ConnectionStrings["defaultConnections"].ConnectionString; //获取在web.config中定义的数据库连接串 public Links() { }//该方法获取所有links中的内容,这里不用以常用的Dataset/datatable/dataReader返回,而是linksEntity的实例数组 public Blog.Entity.LinksEntity[] getList() { ArrayList al = new ArrayList(); string sql = "select * from links"; using (SqlConnection conn = new SqlConnection(_connStr)) { SqlCommand comm = new SqlCommand(sql, conn); conn.Open(); using (SqlDataReader r = comm.ExecuteReader()) { while (r.Read()) { Blog.Entity.LinksEntity Entity = new Blog.Entity.LinksEntity(); //创建一个新的linksEntity实体 Entity.Description = r["description"].ToString(); //赋值 Entity.ID = (int)r["id"]; //Entity.isIndex = (int)r["isindex"]; // Entity.isLogo = (int)r["isLogo"]; Entity.Logo = r["logo"].ToString(); Entity.Title = r["title"].ToString(); Entity.Url = r["url"].ToString(); al.Add(Entity); //将实体添加至ArrayList } } } return (Blog.Entity.LinksEntity[])al.ToArray(typeof(Entity.LinksEntity)); //返回 } public void updateLinks(Blog.Entity.LinksEntity obj) { ......... } public void deleteLinks(Entity.LinksEntity obj) { ...... } public Blog.Entity.LinksEntity getDetail(int id) { .... } public void insertLinks(Entity.LinksEntity obj) { ..... } }}
这里是DAL项目中linksDal.cs的所有内容,该代码中实现的功能有:显示所有记录,更新一条记录,删除一条记录,得到一个记录的详细信息,增加一条记录.
完成这些以后,就来完成表示层了:新建一个web窗体,取名为Default.aspx.拖动一个GridView到default.aspx页面中,用来显示所有的记录,在属性窗口中,把"允许选中行"复选上,表示,当我们选中某一行时,作相应的操作,这里的操作就是指FormView中显示详细的记录.
... Style="font-size: 9pt; font-family: 'Courier New'; text-decoration: none" DataSourceID="ObjectDataSource1" AutoGenerateEditButton="True" AutoGenerateDeleteButton="True" DataKeyNames="id" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
这里需要书写代码了,当然,你也可以用向导完成,这段代码用来填充GridView,注意DataSourceID和DataKeyNames属性,DatakeyNames属性一般是表的主键
ID属性就不说了,上面的GridView的DataSourceID属性值就是这里的ID属性,DataObjectTypeName是指"用于更新、插入或删除数据操作中的参数,而不是从数据绑定控件传递个别的值(从MSDN上看的)",具体什么意思呢?就是表示在UpdateMethod或Deletemethod事件中,传递给相关方法的参数不是一个控件的值,而是一个Blog.Entity.LinksEntity类型的值.回到DAL层就明白了吧.
public void updateLinks(Blog.Entity.LinksEntity obj) { ......... } public void deleteLinks(Entity.LinksEntity obj) { ...... } public void insertLinks(Entity.LinksEntity obj) { ..... }
注意参数,如果你不设定这里的DataObjectTypeName的话,在进行更新操作时会失败,TypeName是ObjectDataSource控件的另一个重要属性,意思是你这里的数据取自于哪一个类,这里我指定的是Blog.Dal.Links类
namespace Blog.DAL{ public class Links .....
与这里是对应的,如果写错的话,是会报错的.在这里执行一下会看到:
表明绑定成功了,你可以试一下选取/编辑/删除操作再来拖动一个FormView控件
BorderStyle="None" BorderWidth="1px" CellPadding="4" DataSourceID="ObjectDataSource2" GridLines="Both" Style="font-size: 9pt; font-family: 'Courier New'; text-decoration: none" Width="348px" DataKeyNames="id"> ... ... .....
省略号码是自动生成的模板,代码较长,就没有帖上来了注意FormView的DataSourceID属性和DataKeyNames属性,再来看ObjectDataSource2的代码
DeleteMethod="deleteLinks" InsertMethod="insertLinks" SelectMethod="getDetail" TypeName="Blog.DAL.Links" UpdateMethod="updateLinks">
DataObjectTypeName/TypeName就不多说了,DeleteMethod/InsertMethod/SelectMethod/UpdateMethod是指当进行Delete/Insert/Select/Update事件时执行的方法,这里分别取自DAL层中的相关方法名.注意这里多了一个,因为我们显示详细记录时,要传递一个ID过来
public Blog.Entity.LinksEntity getDetail(int id) { .... }
最后一步,选中GridView控件,属性窗口中找到SelectedIndexChanged事件,双击它,进入.aspx.cs中书写,在这个事件中,要把ID的值传到ObjectDataSoruce2中,用于填充ID这个参数
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { ObjectDataSource2.SelectParameters["id"].DefaultValue = GridView1.SelectedValue.ToString(); form1.DataBind(); GridView1.DataBind(); }
ok,看效果吧
最后,asp.net2.0的数据控件给我们带来了很大的便利,使我们只需要拖动一些控件书写少量代码甚至是不需要写代码都能让我们轻而易举地显示数据表中的记录.
赞