选择显示字体大小

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

上次的内容说过asp.netasp的最大区别在于编程思维的转换,那么我们现在就来看看如何转换编程思想。以前的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) , 我将在下节的内容里详细解释。

  


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons