频道直达 - 学院 - 下载 - 交易 - 特效 - 字库 - 手册 -排名-工具- 繁體
网页教学网站开发 设为首页
加入收藏
联系我们
建站搜索: 常用广告代码   用户注册 | 用户登陆
您当前的位置:中国建站之家 -> 网站开发设计技术教程 -> Asp.Net教程 -> asp.net高级教程(二)-转换编程思维

asp.net高级教程(二)-转换编程思维

作者:未知  来源:http://edu.chinaz.com  发布时间:2005-7-18 22:20:57  发布人:acx

减小字体 增大字体

上次的内容说过asp.net和asp的最大区别在于编程思维的转换,那么我们现在就来看看如何转换编程思想。以前的web编程从cgi(perl)到asp,php,jsp的编程过程都是这样:美工人员给出页面原型,编程人员照页面填空,最后堆起来算完,下次如果原型变动,那么就再修改程序,这样业务逻辑和html页面混在一起,可以说是事倍功半。那么,现在有了asp.net,我们应该怎么做呢?

让我们找个实际的例子,就拿论坛来说吧,先从顶至下看看它的业务逻辑。我们可以把一个论坛视做一个对象,它有自己的属性和方法,常见的属性有名称、贴子数、用户数、版面数等等,这样的话,我们就可以这样来构造论坛对象:

namespace MyOwnClass
{
using System;
using System.Data.SQL ;
using System.Data ;

////////////////////////////////////////////////////////////////////
//
// Class Name : BBS
//
// Description: 论坛类,构造一个论坛对象
//
// date: 2000/02/03
//
/// ////////////////////////////////////////////////////////////////
public class BBS
{
//私有变量
private string m_strTitle ; //bbs名称
private int m_intForumCount ; //版面数
private int m_intTopicCount ; //贴子数
private int m_intUserCount ; //注册用户数

//属性
public string Title
{
get
{
return m_strTitle ;
}
}

public int ForumCount
{
get
{
return m_intForumCount ;
}
}

public int TopicCount
{
get
{
return m_intTopicCount ;
}
}

public int UserCount
{
get
{
return m_intUserCount ;
}
}

//构造函数
public BBS(string a_strTitle)
{
//
// TODO: Add Constructor Logic here
//
m_strTitle = a_strTitle ;

//读取数据库
MyConnection myConn = new MyConnection() ;
SQLCommand myCommand = new SQLCommand() ;
myCommand.ActiveConnection = myConn ;
myCommand.CommandText = "up_GetBBSInfo" ; //调用存储过程
myCommand.CommandType = CommandType.StoredProcedure ;

try
{
myConn.Open() ;
SQLDataReader myReader ;
myCommand.Execute(out myReader) ;
if (myReader.Read())
{
m_intForumCount = (int)myReader["ForumCount"] ;
m_intTopicCount = (int)myReader["TopicCount"] ;
m_intUserCount = (int)myReader["UserCount"] ;
}
else
{
throw(new Exception("表或存储过程不存在")) ;
}

//清场
myReader.Close();
myConn.Close() ;
}
catch(SQLException e)
{
throw(new Exception("数据库出错:" + e.Message)) ;
}

}
}
}

这个bbs类很简单,有四个私有变量,对应四个只读属性,方法只有一个带参数的构造函数,作用是从数据库中读取相应的数据,填充四个私有变量。类构造好了,让我们看看如何使用,在需要显示论坛这些属性的页面文件里(.aspx)里,构造四个Label,象这样:
<table width=140 cellpadding=4 cellspacing=1 border=0>
<tr>
<td class=cn>
<font color=white>注册用户数:</font>
</td>
<td>
<asp:label id="lblUserCount" runat=Server class=cn></asp:label>
</td>
</tr>
<tr>
<td class=cn>
<font color=white>贴子总数:</font>
</td>
<td>
<asp:label id="lblTopicCount" runat=Server class=cn></asp:label>
</td>
</tr>
<tr>
<td class=cn>
<font color=white>版面数:</font>
</td>
<td>
<asp:label id="lblForumCount" runat=Server class=cn></asp:label>
</td>
</tr>
</table>
然后在对应的c#文件里这样使用:

protected void Page_Init(object sender, EventArgs e)
{
//
// CODEGEN: This call is required by the ASP+ Windows Form Designer.
//
InitializeComponent();

//初始化页面对象
//创建bbs对象
try
{
m_objBBS = new BBS("鹰翔山庄论坛") ;
}
catch(Exception exp)
{
#if DEBUG
Response.Write ("初始化bbs对象出错:" + exp.Message + "<br>") ;
return ;
#endif//DEBUG
Server.Transfer("error.aspx") ;
}

//论坛名称
lblBBSName.ForeColor = Color.White ;
lblBBSName.Text = m_objBBS.Title ;

//用户数
lblUserCount.ForeColor = Color.White ;
lblUserCount.Text = m_objBBS.UserCount.ToString() ;

//文章数
lblTopicCount.ForeColor = Color.White ;
lblTopicCount.Text = m_objBBS.TopicCount.ToString() ;

//版面数
lblForumCount.ForeColor = Color.White ;
lblForumCount.Text = m_objBBS.ForumCount.ToString() ;
}

看出这样使用的好处吗?对,就是业务逻辑和html代码分开,这样无论页面原型如何修改,代码都不需要做丝毫改动。bbs对象构造好了,让我们看看论坛的其他对象,他们分别是用户(BBSUser)、版面(Forum)和贴子(Topic) , 我将在下节的内容里详细解释。


将本文收藏到QQ书签与更多好友分享
[打 印]
[] [返回上一页] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
精彩推荐
热门文章
· 注册码大全二
· 注册码大全四
· 注册码大全一
· 要10G免费网络硬盘的请进..
· 通过google 赶快来赚美金..
· 注册码大全十
· 头像-qq头像(qq新头像)4..
· 让你轻松架设FTP服务器1..
· 注册码大全三
· 梦幻背景图片7
· 卡通动物图片6
· 网页制作素材-按钮素材2..
· 让你轻松架设FTP服务器5..
· 风景图片8
· 注册码大全九
· 让你轻松架设FTP服务器2..
关注此文读者还看过
· 排序方式解决“上下主题..
· 网页制作 设计师的框架
· 用PHP制作静态网站的模板..
· 在Web项目开发中使用Bas..
· AJAX实现web页面中级联菜..
· 中国移动启动WAP登陆提示..
· Apache, PHP在Windows 9..
· Access数据库技术(54)
· Flash MX2004入门与进阶..
· ASP+中文显示之两种解决..
· 处理二进制数据
· eBay易趣联手永乐抢夺白..
· 如何用Asp动态生成xml文..
· "中国PC数量有限" 百度扩..
· FWMX简易遮罩制作双色波..
· 学好设计之前----美术基..
相关文章
· ASP.NET连接Access和SQL Se..
· Asp.net中防止用户多次登录..
· ASP.NET 与 Ajax 的实现方式..
· ASP.net平台社区软件Discuz..
· ASP.NET AJAX中的异步..
· 解析:如何在 ASP.NET 中下..
· ASP.Net中保护自定义的服务..
· ASP.NET中的doPostBack脚本..
· 不使用VS进行ASP.NET Membe..
· 关于ASP.NET编程中的嵌套If..
· ASP.NET 2.0 页面状态持续程..
· ASP.NET中的日期与时间的处..
· Asp.net编程中的数组基础实..
· 简单介绍 ASP.NET 中的运算..
· Asp.Net 构架(Http请..
· Asp.net中的Popup控件..
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 人才招聘
网站合作、内容监督、商务咨询:QQ: 9576619
Copyright ? 2005--2008 中国建站之家版权所有
粤ICP备05092265号