在 .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.