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

Reading number is top 10 articles
使用WatiN对ASP.NET页面进行单元测试_[AJAX教程]
C#中的异常处理语句
C#教程:C#2.0 新特性 迭代器
asp.net判断文件或文件夹是否存在_[Asp.Net教程]
c#生成图片验证码代码_[Asp.Net教程]
如何重起,停止,启动httpd服务?_php资料_编程技术
asp.net页面导出为Excel文档_[Asp.Net教程]
结合泛型与模板的STL.NET探索_.net资料_编程技术
PHP脚本的8个技巧(8)PHP和XML_php资料_编程技术
详细讲解PHP的Jmai组件及发送邮件实例_php资料_编程技术
Reading number is top 10 pictures
NeedWallpaper14
YangYuYing and ZhengShaoQiu dance on the generous come interest dye-in-the-wood
Average female college students3
西游日记3
张家界的玻璃桥
南昌铁路局宜春车务段攸县车站铁路职工福利房被开发商侵占
一个武林高手的故事
Group of female porn in 《westwards》, uninhibited woman threatened to not the bottom line1
教你22句话
非笑不可:最强爆笑图片精选
Download software ranking
Tram sex maniac 2 (H) rar bag14
塘西风月痕
Visual C++界面编程技术
致我们终将逝去的青春
I'm come from Beijing2
Tram sex maniac 2 (H) rar bag8
Boxer vs Yellow4
Tram sex maniac 2 (H) rar bag5
linux安装大全
美女写真2
delv published in(发表于) 2014/1/6 8:48:11 Edit(编辑)
ASP.NET,2.0之ObjectDataSourc控件_[Asp.Net教程]

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 sqlserver
using 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的数据控件给我们带来了很大的便利,使我们只需要拖动一些控件书写少量代码甚至是不需要写代码都能让我们轻而易举地显示数据表中的记录.


作者:maypaopao 来源:蓝色理想





添加到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.