asp.net高级教程(二)-转换编程思维
来源:互联网 发布:淘宝收藏软件手机版 编辑:程序博客网 时间:2024/05/22 06:38
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>上次的内容说过asp.net和asp的最大区别在于编程思维的转换,那么我们现在就来看看如何转换编程思想。以前的web编程从cgi(perl)到asp,php,jsp的编程过程都是这样:美工人员给出页面原型,编程人员照页面填空,最后堆起来算完,下次如果原型变动,那么就再修改程序,这样业务逻辑和html页面混在一起,可以说是事倍功半。那么,现在有了asp.net,我们应该怎么做呢?
让我们找个实际的例子,就拿论坛来说吧,先从顶至下看看它的业务逻辑。我们可以把一个论坛视做一个对象,它有自己的属性和方法,常见的属性有名称、贴子数、用户数、版面数等等,这样的话,我们就可以这样来构造论坛对象:
namespaceMyOwnClass
{
usingSystem;
usingSystem.Data.SQL;
usingSystem.Data;
////////////////////////////////////////////////////////////////////
//
//ClassName: BBS
//
//Description: 论坛类,构造一个论坛对象
//
//date: 2000/02/03
//
///////////////////////////////////////////////////////////////////
publicclassBBS
{
//私有变量
privatestringm_strTitle; //bbs名称
privateintm_intForumCount; //版面数
privateintm_intTopicCount; //贴子数
privateintm_intUserCount; //注册用户数
//属性
publicstringTitle
{
get
{
returnm_strTitle;
}
}
publicintForumCount
{
get
{
returnm_intForumCount;
}
}
publicintTopicCount
{
get
{
returnm_intTopicCount;
}
}
publicintUserCount
{
get
{
returnm_intUserCount;
}
}
//构造函数
publicBBS(stringa_strTitle)
{
//
//TODO:AddConstructorLogichere
//
m_strTitle=a_strTitle;
//读取数据库
MyConnectionmyConn=newMyConnection();
SQLCommandmyCommand=newSQLCommand();
myCommand.ActiveConnection=myConn;
myCommand.CommandText="up_GetBBSInfo"; //调用存储过程
myCommand.CommandType=CommandType.StoredProcedure;
try
{
myConn.Open();
SQLDataReadermyReader;
myCommand.Execute(outmyReader);
if(myReader.Read())
{
m_intForumCount=(int)myReader["ForumCount"];
m_intTopicCount=(int)myReader["TopicCount"];
m_intUserCount =(int)myReader["UserCount"];
}
else
{
throw(newException("表或存储过程不存在"));
}
//清场
myReader.Close();
myConn.Close();
}
catch(SQLExceptione)
{
throw(newException("数据库出错:"+e.Message));
}
}
}
}
这个bbs类很简单,有四个私有变量,对应四个只读属性,方法只有一个带参数的构造函数,作用是从数据库中读取相应的数据,填充四个私有变量。类构造好了,让我们看看如何使用,在需要显示论坛这些属性的页面文件里(.aspx)里,构造四个Label,象这样:
<tablewidth=140cellpadding=4cellspacing=1border=0>
<tr>
<tdclass=cn>
<fontcolor=white>注册用户数:</font>
</td>
<td>
<asp:labelid="lblUserCount"runat=Serverclass=cn></asp:label>
</td>
</tr>
<tr>
<tdclass=cn>
<fontcolor=white>贴子总数:</font>
</td>
<td>
<asp:labelid="lblTopicCount"runat=Serverclass=cn></asp:label>
</td>
</tr>
<tr>
<tdclass=cn>
<fontcolor=white>版面数:</font>
</td>
<td>
<asp:labelid="lblForumCount"runat=Serverclass=cn></asp:label>
</td>
</tr>
</table>
然后在对应的c#文件里这样使用:
protectedvoidPage_Init(objectsender,EventArgse)
{
//
//CODEGEN:Thiscallisrequiredbytheasp+WindowsFormDesigner.
//
InitializeComponent();
//初始化页面对象
//创建bbs对象
try
{
m_objBBS=newBBS("鹰翔山庄论坛");
}
catch(Exceptionexp)
{
#ifDEBUG
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),我将在下节的内容里详细解释。
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
document.write(baiduCproIFrame());-->让我们找个实际的例子,就拿论坛来说吧,先从顶至下看看它的业务逻辑。我们可以把一个论坛视做一个对象,它有自己的属性和方法,常见的属性有名称、贴子数、用户数、版面数等等,这样的话,我们就可以这样来构造论坛对象:
namespaceMyOwnClass
{
usingSystem;
usingSystem.Data.SQL;
usingSystem.Data;
////////////////////////////////////////////////////////////////////
//
//ClassName: BBS
//
//Description: 论坛类,构造一个论坛对象
//
//date: 2000/02/03
//
///////////////////////////////////////////////////////////////////
publicclassBBS
{
//私有变量
privatestringm_strTitle; //bbs名称
privateintm_intForumCount; //版面数
privateintm_intTopicCount; //贴子数
privateintm_intUserCount; //注册用户数
//属性
publicstringTitle
{
get
{
returnm_strTitle;
}
}
publicintForumCount
{
get
{
returnm_intForumCount;
}
}
publicintTopicCount
{
get
{
returnm_intTopicCount;
}
}
publicintUserCount
{
get
{
returnm_intUserCount;
}
}
//构造函数
publicBBS(stringa_strTitle)
{
//
//TODO:AddConstructorLogichere
//
m_strTitle=a_strTitle;
//读取数据库
MyConnectionmyConn=newMyConnection();
SQLCommandmyCommand=newSQLCommand();
myCommand.ActiveConnection=myConn;
myCommand.CommandText="up_GetBBSInfo"; //调用存储过程
myCommand.CommandType=CommandType.StoredProcedure;
try
{
myConn.Open();
SQLDataReadermyReader;
myCommand.Execute(outmyReader);
if(myReader.Read())
{
m_intForumCount=(int)myReader["ForumCount"];
m_intTopicCount=(int)myReader["TopicCount"];
m_intUserCount =(int)myReader["UserCount"];
}
else
{
throw(newException("表或存储过程不存在"));
}
//清场
myReader.Close();
myConn.Close();
}
catch(SQLExceptione)
{
throw(newException("数据库出错:"+e.Message));
}
}
}
}
这个bbs类很简单,有四个私有变量,对应四个只读属性,方法只有一个带参数的构造函数,作用是从数据库中读取相应的数据,填充四个私有变量。类构造好了,让我们看看如何使用,在需要显示论坛这些属性的页面文件里(.aspx)里,构造四个Label,象这样:
<tablewidth=140cellpadding=4cellspacing=1border=0>
<tr>
<tdclass=cn>
<fontcolor=white>注册用户数:</font>
</td>
<td>
<asp:labelid="lblUserCount"runat=Serverclass=cn></asp:label>
</td>
</tr>
<tr>
<tdclass=cn>
<fontcolor=white>贴子总数:</font>
</td>
<td>
<asp:labelid="lblTopicCount"runat=Serverclass=cn></asp:label>
</td>
</tr>
<tr>
<tdclass=cn>
<fontcolor=white>版面数:</font>
</td>
<td>
<asp:labelid="lblForumCount"runat=Serverclass=cn></asp:label>
</td>
</tr>
</table>
然后在对应的c#文件里这样使用:
protectedvoidPage_Init(objectsender,EventArgse)
{
//
//CODEGEN:Thiscallisrequiredbytheasp+WindowsFormDesigner.
//
InitializeComponent();
//初始化页面对象
//创建bbs对象
try
{
m_objBBS=newBBS("鹰翔山庄论坛");
}
catch(Exceptionexp)
{
#ifDEBUG
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),我将在下节的内容里详细解释。
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- asp.net高级教程(二)--- 转换编程思维
- asp.net高级教程(二)-转换编程思维
- asp.net高级教程(二):转换编程思维
- ASP.NET XML高级编程(二、XmlDocument)
- asp.net高级教程
- asp.net高级教程
- asp.net高级教程
- asp.net高级教程(一)---asp.net , 还是asp+ ?
- asp.net高级教程(一)-asp.net还是asp?
- asp.net高级教程(一)-asp.net还是asp?
- asp.net高级教程(一)-asp.net还是asp?
- asp.net高级教程(一)-asp.net还是asp+?
- asp.net高级教程(三)---对象
- asp.net高级教程(三)-对象
- asp.net高级教程(三)-对象
- asp.net高级教程(三)-对象
- asp.net高级教程(三)-对象
- asp.net高级教程(续)
- 一个简单实现多条件查询的例子
- asp.net高级教程(三)-实战篇
- 用PHP和ACCESS写聊天室(十)
- asp.net高级教程(三)-对象
- 用PHP和ACCESS写聊天室(八)
- asp.net高级教程(二)-转换编程思维
- 用PHP和ACCESS写聊天室(九)
- asp.net高级教程(一)-asp.net还是asp+?
- 用PHP和ACCESS写聊天室(七)
- ASP.NET中的CodeBehind技术(4)
- 一个简洁的多级别论坛
- ASP.NET中的CodeBehind技术(3)
- 基于数据库的在线人数,日访问量等统计
- ASP.NET中的CodeBehind技术(2)