欢迎光临
我们一直在努力

Asp.net中的代码与表现分离-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

.net framework version 2.0出来好久了,vs 2005 beta2也有了,近段时间一直在断断续续地玩asp.net,发现刚学不久的东西马上就得更新,不更新还真不行。

先从asp.net中的代码与表现分离说起,这是一个非常有用的功能,特别是在team work中,我想。

在 .net framework version 1.0/1.1中,微软是这么教我们代码与表现分离的:

1、首先要在.aspx文件的@page指令中加入如下一行:

<%@ page language="vb" autoeventwireup="false" codebehind="samplepage.aspx.vb" inherits="sampleproject.samplepage"%>

*注:这里的codebehind属性换成src属性亦可

2、在使用后台代码文件时,也就是.vb文件时,必须在后台代码中为表现文件内使用的每个控件声明实例,可以如下声明:

protected withevents lblmessage as label

忘了可不行,浏览器会告诉你“the name "lblmessage is not declared”!

按照msdn上的原话是这样讲的:

the code-behind class is a complete class definition; it contains instance variables for all controls on the page, explicit event binding using delegates, and so on.

以上都是以前的事了,说说现在的情况。

在.net framework version 2.0中,微软告诉我们以前这样实现代码与表现分离太麻烦了,兄弟,现在我们可以这样来实现它:

1、在.aspx文件的@page指令还是要写的,不过改成这样子写:

<%@ page language="vb" compilewith="samplepage.aspx.vb" classname="samplepage_aspx" %>

用compilewith属性来替换codebehind和src属性,这越来越多的属性,我想应该是为了向后兼容付出的必要代价吧,classname指明后台文件所使用的类。

2、使用后台代码文件时,不必为表现文件内使用的每个控件声明实例,这里微软takes advantage of a new language feature known as partial classes.

the code-behind file for a page is not a complete class definition. instead, it includes only the application code you need, such as event handlers. the code-behind partial class does not need to include instance variables or explicit event binding. asp.net can infer the control instances and derive event bindings from the markup during compilation.

这样一来确实减少些代码,在vs2005中微软声称可以减少70%的代码,我暂且学到这里,不知道还有没有其它减少代码的地方。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Asp.net中的代码与表现分离-.NET教程,Asp.Net开发
分享到: 更多 (0)

相关推荐

  • 暂无文章