• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

Asp.net中的代码与表现分离

发布: 2007-6-30 23:38 | 作者: admin | 来源: | 查看: 15次 | 进入软件测试论坛讨论

领测软件测试网 .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%的代码,我暂且学到这里,不知道还有没有其它减少代码的地方。



延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网