使用Web窗体
引言
前段时间客户A突然提出,B项目希望可以做成BS形式的,之前是CS的,因为我之前也没学过ASP.NET,于是一边百度,一边Coding,马马虎虎的把功能流程给调通,然后就交差了,想着闲下来再系统地学习一下ASP.NET。最近,有新接手一个项目,优势设计ASP.NET的,一边写一边百度,写着写着发现首先对于什么时候用服务器控件,什么时候用可编程html元素等细化的东西始终没有清晰的认识,总是挑实现简单的写,结果心里很没有底,终于狠下心来从头认真的学习一下ASP.NET.
开始前的准备
创建Web Form项目
使用ASP.NET EMPTY WEB APPLICATION模板新建一个Visual Studio项目:
在项目上右键-Add-New Item选择Web模板中的Web Form项目,这里会创建3个文件,第一个是Web窗体本身,他的扩展名为Aspx,文件内容如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebForm.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>
第二个文件为代码隐藏类,扩展名cs,他默认以与其关联的Web窗体来命名,文件内容如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebForm { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } } }
最后一个文件为设计器文件,扩展名designer.cs---可视化设计工具需要使用该文件(因为在实际操作中,我们不需要对该文件进行任何修改,因此这里不对其中内容进行贴图)。
代码片段,可编程HTML元素和控件
代码片段指放置在<%与%>标签之间的C#表达式,有各种不同的代码片段可供使用:
以上为Pro ASP.NET 4.5 IN C# 第十二章的截图。
对于标准代码片段<%,内容代码片段<%=,数据绑定代码片段<%#,以及指令由于较为常用,下面只对属性代码片段进行举例说明:
在Web.Config文件中添加配置元素
<?xml version="1.0" encoding="utf-8"?> <!-- 有关如何配置 ASP.NET 应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.5.1" /> <httpRuntime targetFramework="4.5.1" /> </system.web> <appSettings> <add key="title" value="touha‘s first web form"/> </appSettings> </configuration>
修改web窗体文件,读取前面定义的title属性的值:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebForm.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title> <asp:Literal ID="Literal1" runat="server" Text="<%$ AppSettings:title%>" /></title> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>
注意:属性代码片段必须放在Literal控件中,否则将提示如下错误
关于其详细用法,请参考:https://msdn.microsoft.com/zh-cn/library/d5bd1tad(VS.80).aspx
对于可编程HTML元素,这里暂时不做讲解。
控件是属于ASP.NET很大的一块,她是用于生成HTML片段的可重用的功能块,可分为:用户控件,服务器控件以及内置控件,本节不做叙述,后面会对其将会深入讨论之。
aspx web窗体最后究竟会生成怎样一个C#类
查看Windows7 和 Window8 上的c:\Users\<userName>\AppData\Local\Temp\Temporary ASP.NET Files目录(注意AppData默认为隐藏),下面贴出代码:
//------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行时版本:4.0.30319.34209 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ [assembly: System.CodeDom.Compiler.GeneratedCodeAttribute("ASP.NET", "4.0.30319.34274")] [assembly: System.Security.SecurityRulesAttribute(System.Security.SecurityRuleSet.Level2)] [assembly: System.Runtime.Versioning.TargetFrameworkAttribute(".NETFramework,Version=v4.5.1")] namespace @__ASP { internal class FastObjectFactory_app_web_yc3poujj { #line 1 "c:\\dummy.txt" #line default #line hidden private FastObjectFactory_app_web_yc3poujj() { } static object Create_ASP_default_aspx() { return new ASP.default_aspx(); } } }
#pragma checksum "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "FF4B8FBE55EE3CCAF50519550FD9CA46EAB26B31" //------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行时版本:4.0.30319.34209 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ namespace ASP { #line 392 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System.Linq; #line default #line hidden #line 399 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System.Web.Security; #line default #line hidden #line 390 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System.ComponentModel.DataAnnotations; #line default #line hidden #line 388 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System.Collections.Generic; #line default #line hidden #line 394 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System.Text.RegularExpressions; #line default #line hidden #line 8 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" using System.Web.UI.WebControls; #line default #line hidden #line 405 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System.Xml.Linq; #line default #line hidden #line 8 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" using System.Web.UI; #line default #line hidden #line 404 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System.Web.UI.HtmlControls; #line default #line hidden #line 395 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System.Web; #line default #line hidden #line 391 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System.Configuration; #line default #line hidden #line 386 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System; #line default #line hidden #line 393 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System.Text; #line default #line hidden #line 400 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System.Web.Profile; #line default #line hidden #line 396 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System.Web.Caching; #line default #line hidden #line 387 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System.Collections; #line default #line hidden #line 8 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" using System.Web.UI.WebControls.WebParts; #line default #line hidden #line 8 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" using System.Web.UI.WebControls.Expressions; #line default #line hidden #line 389 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System.Collections.Specialized; #line default #line hidden #line 398 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config" using System.Web.SessionState; #line default #line hidden #line 8 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" using System.Web.DynamicData; #line default #line hidden using Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms; [System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()] public class default_aspx : global::WebForm.Default, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler { #line 8 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" protected global::System.Web.UI.WebControls.Literal Literal1; #line default #line hidden private static bool @__initialized; private static object @__fileDependencies; [System.Diagnostics.DebuggerNonUserCodeAttribute()] public default_aspx() { string[] dependencies; ((global::WebForm.Default)(this)).AppRelativeVirtualPath = "~/Default.aspx"; if ((global::[email protected]__initialized == false)) { dependencies = new string[1]; dependencies[0] = "~/Default.aspx"; global::[email protected]__fileDependencies = this.GetWrappedFileDependencies(dependencies); global::[email protected]__initialized = true; } this.Server.ScriptTimeout = 30000000; } protected System.Web.Profile.DefaultProfile Profile { get { return ((System.Web.Profile.DefaultProfile)(this.Context.Profile)); } } protected System.Web.HttpApplication ApplicationInstance { get { return ((System.Web.HttpApplication)(this.Context.ApplicationInstance)); } } [System.Diagnostics.DebuggerNonUserCodeAttribute()] private global::System.Web.UI.LiteralControl @__BuildControl__control2() { global::System.Web.UI.LiteralControl @__ctrl; @__ctrl = new global::System.Web.UI.LiteralControl("\r\n\r\n<!DOCTYPE html>\r\n\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n"); @__ctrl.SetTraceData(typeof(Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData), new Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData(104, 68, true)); return @__ctrl; } [System.Diagnostics.DebuggerNonUserCodeAttribute()] private global::System.Web.UI.HtmlControls.HtmlMeta @__BuildControl__control4() { global::System.Web.UI.HtmlControls.HtmlMeta @__ctrl; #line 7 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl = new global::System.Web.UI.HtmlControls.HtmlMeta(); #line default #line hidden #line 7 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" ((System.Web.UI.IAttributeAccessor)(@__ctrl)).SetAttribute("http-equiv", "Content-Type"); #line default #line hidden #line 7 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl.Content = "text/html; charset=utf-8"; #line default #line hidden @__ctrl.SetTraceData(typeof(Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData), new Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData(195, 68, false)); return @__ctrl; } [System.Diagnostics.DebuggerNonUserCodeAttribute()] private global::System.Web.UI.LiteralControl @__BuildControl__control6() { global::System.Web.UI.LiteralControl @__ctrl; @__ctrl = new global::System.Web.UI.LiteralControl(" "); @__ctrl.SetTraceData(typeof(Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData), new Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData(276, 1, true)); return @__ctrl; } [System.Diagnostics.DebuggerNonUserCodeAttribute()] private global::System.Web.UI.WebControls.Literal @__BuildControlLiteral1() { global::System.Web.UI.WebControls.Literal @__ctrl; #line 8 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl = new global::System.Web.UI.WebControls.Literal(); #line default #line hidden this.Literal1 = @__ctrl; #line 8 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl.ID = "Literal1"; #line default #line hidden @__ctrl.Text = global::System.Convert.ToString(System.Web.Compilation.AppSettingsExpressionBuilder.GetAppSetting("title", typeof(System.Web.UI.WebControls.Literal), "Text"), global::System.Globalization.CultureInfo.CurrentCulture); @__ctrl.SetTraceData(typeof(Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData), new Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData(277, 75, false)); return @__ctrl; } [System.Diagnostics.DebuggerNonUserCodeAttribute()] private global::System.Web.UI.HtmlControls.HtmlTitle @__BuildControl__control5() { global::System.Web.UI.HtmlControls.HtmlTitle @__ctrl; #line 8 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl = new global::System.Web.UI.HtmlControls.HtmlTitle(); #line default #line hidden global::System.Web.UI.LiteralControl @__ctrl1; #line 8 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl1 = this[email protected]__BuildControl__control6(); #line default #line hidden System.Web.UI.IParserAccessor @__parser = ((System.Web.UI.IParserAccessor)(@__ctrl)); #line 8 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__parser.AddParsedSubObject(@__ctrl1); #line default #line hidden global::System.Web.UI.WebControls.Literal @__ctrl2; #line 8 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl2 = this[email protected]__BuildControlLiteral1(); #line default #line hidden #line 8 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__parser.AddParsedSubObject(@__ctrl2); #line default #line hidden @__ctrl.SetTraceData(typeof(Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData), new Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData(269, 91, false)); return @__ctrl; } [System.Diagnostics.DebuggerNonUserCodeAttribute()] private global::System.Web.UI.HtmlControls.HtmlHead @__BuildControl__control3() { global::System.Web.UI.HtmlControls.HtmlHead @__ctrl; #line 6 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl = new global::System.Web.UI.HtmlControls.HtmlHead("head"); #line default #line hidden global::System.Web.UI.HtmlControls.HtmlMeta @__ctrl1; #line 6 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl1 = this[email protected]__BuildControl__control4(); #line default #line hidden System.Web.UI.IParserAccessor @__parser = ((System.Web.UI.IParserAccessor)(@__ctrl)); #line 6 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__parser.AddParsedSubObject(@__ctrl1); #line default #line hidden global::System.Web.UI.HtmlControls.HtmlTitle @__ctrl2; #line 6 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl2 = this[email protected]__BuildControl__control5(); #line default #line hidden #line 6 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__parser.AddParsedSubObject(@__ctrl2); #line default #line hidden @__ctrl.SetTraceData(typeof(Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData), new Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData(172, 197, false)); return @__ctrl; } [System.Diagnostics.DebuggerNonUserCodeAttribute()] private global::System.Web.UI.LiteralControl @__BuildControl__control7() { global::System.Web.UI.LiteralControl @__ctrl; @__ctrl = new global::System.Web.UI.LiteralControl("\r\n<body>\r\n "); @__ctrl.SetTraceData(typeof(Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData), new Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData(369, 14, true)); return @__ctrl; } [System.Diagnostics.DebuggerNonUserCodeAttribute()] private global::System.Web.UI.LiteralControl @__BuildControl__control8() { global::System.Web.UI.LiteralControl @__ctrl; @__ctrl = new global::System.Web.UI.LiteralControl("\r\n <div>\r\n </div>\r\n "); @__ctrl.SetTraceData(typeof(Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData), new Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData(415, 29, true)); return @__ctrl; } [System.Diagnostics.DebuggerNonUserCodeAttribute()] private global::System.Web.UI.HtmlControls.HtmlForm @__BuildControlform1() { global::System.Web.UI.HtmlControls.HtmlForm @__ctrl; #line 11 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl = new global::System.Web.UI.HtmlControls.HtmlForm(); #line default #line hidden this.form1 = @__ctrl; #line 11 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl.ID = "form1"; #line default #line hidden global::System.Web.UI.LiteralControl @__ctrl1; #line 11 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl1 = this[email protected]__BuildControl__control8(); #line default #line hidden System.Web.UI.IParserAccessor @__parser = ((System.Web.UI.IParserAccessor)(@__ctrl)); #line 11 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__parser.AddParsedSubObject(@__ctrl1); #line default #line hidden @__ctrl.SetTraceData(typeof(Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData), new Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData(383, 68, false)); return @__ctrl; } [System.Diagnostics.DebuggerNonUserCodeAttribute()] private global::System.Web.UI.LiteralControl @__BuildControl__control9() { global::System.Web.UI.LiteralControl @__ctrl; @__ctrl = new global::System.Web.UI.LiteralControl("\r\n</body>\r\n</html>\r\n"); @__ctrl.SetTraceData(typeof(Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData), new Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.TraceData(451, 20, true)); return @__ctrl; } [System.Diagnostics.DebuggerNonUserCodeAttribute()] private void @__BuildControlTree(default_aspx @__ctrl) { #line 1 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" this.InitializeCulture(); #line default #line hidden global::System.Web.UI.LiteralControl @__ctrl1; #line 1 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl1 = this[email protected]__BuildControl__control2(); #line default #line hidden System.Web.UI.IParserAccessor @__parser = ((System.Web.UI.IParserAccessor)(@__ctrl)); #line 1 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__parser.AddParsedSubObject(@__ctrl1); #line default #line hidden global::System.Web.UI.HtmlControls.HtmlHead @__ctrl2; #line 1 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl2 = this[email protected]__BuildControl__control3(); #line default #line hidden #line 1 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__parser.AddParsedSubObject(@__ctrl2); #line default #line hidden global::System.Web.UI.LiteralControl @__ctrl3; #line 1 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl3 = this[email protected]__BuildControl__control7(); #line default #line hidden #line 1 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__parser.AddParsedSubObject(@__ctrl3); #line default #line hidden global::System.Web.UI.HtmlControls.HtmlForm @__ctrl4; #line 1 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl4 = this[email protected]__BuildControlform1(); #line default #line hidden #line 1 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__parser.AddParsedSubObject(@__ctrl4); #line default #line hidden global::System.Web.UI.LiteralControl @__ctrl5; #line 1 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__ctrl5 = this[email protected]__BuildControl__control9(); #line default #line hidden #line 1 "c:\users\tangbo\documents\visual studio 2012\Projects\WebForm\WebForm\Default.aspx" @__parser.AddParsedSubObject(@__ctrl5); #line default #line hidden } [System.Diagnostics.DebuggerNonUserCodeAttribute()] protected override void FrameworkInitialize() { base.FrameworkInitialize(); this[email protected]__BuildControlTree(this); this.AddWrappedFileDependencies(global::[email protected]__fileDependencies); this.Request.ValidateInput(); } [System.Diagnostics.DebuggerNonUserCodeAttribute()] public override int GetTypeHashCode() { return 1423172938; } [System.Diagnostics.DebuggerNonUserCodeAttribute()] public override void ProcessRequest(System.Web.HttpContext context) { base.ProcessRequest(context); } } }
从上面代码,可以看出,aspx Web窗体文件生成的类实现了IHttpHandler接口,并且继承了其对应的代码隐藏类文件,另外我们之前加的Literal控件也作为其成员
下面的代码,显示了属性代码片段的实际工作原理
通过Page.FrameworkInitialize方法,可以看出,其主要通过@__BuildControlTree方法,初始化控件树。
总结语
本章对于Web窗体,及其工作原理做了一个粗略的介绍。