ASP.NET4.5从零开始(1)

使用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窗体,及其工作原理做了一个粗略的介绍。

时间: 2024-08-11 03:36:43

ASP.NET4.5从零开始(1)的相关文章

ASP.NET4 与 VS2010 Web 开发页面服务改进

转:http://blog.163.com/kele_lipeng/blog/static/81345278201132754729336/ 作者:朱先忠 本文将接着上一篇 ASP.NET4与VS2010Web开发核心服务改进 继续讨论核心ASP.NET服务方面的改进. 四.永久重定向页面 一种常见的Web应用程序做法是,随着时间的推移经常移动网页和其他内容,从而导致搜索引擎失效链接的积累.在ASP.NET中,开发商通常采用的处理旧网址请求的方案是,使用 Response.Redirect方法把

转:通过代码理解Asp.net4中的几种ClientIDMode设置.

转:http://www.cnblogs.com/xray2005/archive/2011/07/05/2097881.html 以前我们可以通过ClientID在JavaScript脚本中服务器端控件.比如: document.getElementById("<%=控件.ClientID %>"); 这种方式不方便,而且如果在有数据绑定的控件中嵌套着子控件,则访问更不太方便了. 现在,在Asp.Net4中,对于控件增加了一种新的属性,名为ClientIDMode.通过设

&lt;ASP.NET4 从入门到精通&gt;学习笔记3

第三部分,状态管理与缓存 何为状态管理,起始对于web而言,经过前面章节的讲解,已经理解,对于web程序,就是一个无状态的程序,每次的请求与每次的响应,两者之间本身就是独立存在的,这一点对于早期的静态网页来说,倒没什么,因为每次的请求和响应其实都是固定不变的.但是到了动态网页时代,就不行了,web开发的很大一部分工作将变为状态管理.这一点,可能举一个例子,最具有代表性,比如说购物车,那么对于购物车而言,就需要知道他的访问者是谁?虽然他们可以在不同的页面切换,但是购物车不能够变化,此时就是一种状态

《ASP.NET4 从入门到精通》学习笔记4

第4部分诊断与插件 刚开始看这章的时候,真实一头雾水,不知道在讲什么,不过看了关于http pipeline之后,才了解相关说明.因此对于这一章的学习,建议各位首先看看http pipeline然后再进行学习. 第17章诊断与调测 该部分讲解了基本的调试方法,不过个人认为调测方法其实涉及到很多方面的内容,这章也只是大概介绍几个基本的方法. l  在页面启动 trace功能,然后在页面上就可以看到调用状态了. l  除此以外,还可以修改web.configtrace enable="true&qu

Asp.Net4.0/VS2010新变化(3):webform中也可以直接url路由

以前在做asp的时候,要把 /default.asp?id=123映射成/default/123,需要借助IISRewriter这个组件,到了asp.net以后,可以用代码写了,但是个人觉得很麻烦,要写一堆代码,还要修改web.config,现在好了:asp.net4.0中 asp.net mvc中的路由规则全部可以用于webform了 使用步骤: 1.Global.ascx.cs中先注册路由规则 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Asp.Net4.0/VS2010新变化(4):SEO的改进

asp.net4.0的webform中,对于Page对象新增加了二个属性:MetaKeywords,MetaDescription 后端代码上,写法也更简单,以前可能需要这样写: ? 1 2 this.Header.Controls.AddAt(0, new HtmlMeta() { Name = "keywords", Content = Website.MetaKeyword });//关键字 this.Header.Controls.AddAt(1, new HtmlMeta()

ASP.NET4.0新特性

原文:ASP.NET4.0新特性 在以前试用VS2010的时候已经关注到它在Web开发支持上的一些变化了,为此我还专门做了一个ppt,当初是计划在4月12日那天讲的,结果因为莫名其妙的原因导致没有语音以致放弃在LiveMeeting上的这次讲课,也导致了本篇的产生. 新增了项目模板 在创建Web项目时可以看到增加了更多的Web项目模板在VS2008中对应的情况如下: 在新模板中有如下改进:基础MemberShip功能.在大多数网站和应用程序中需要进行认证,因此在新模板中增加了认证功能使得用户能在

《ASP.NET4从入门到精通》学习笔记5

这个部分吗,可以说是这本书最最没有价值的部分,他自己的作者感觉都没有弄明白,就换乱介绍,而且介绍的不知所云!该部分,只是罗列的目的,建议大家不要细读这个部分的内容,浪费时间而且伤脑筋!(感慨:总是感觉国内的书,看着500多页,有含金量的太少了,大部分页面都是抽数的.可怜我的买书钱.) 第五部分,高级功能介绍 这部分的主要目的是介绍ASP.NET引入的新的技术方案,包括了AJAX,MVC,动态数据,XBAP,silverlight,WPF等等. 第20章动态数据 第21章 WPF 第22章 MVC

&lt;ASP.NET4 从入门到精通&gt;学习笔记1

很久没有写东西了,今日有时间,开始整理一下关于ASP.NET 4的学习笔记,提醒自己,也欢迎评论. 概述一共分为6个大的部分,后续文章,将根据每个部分进行整理,本读书笔记只是整理关键点,对于啰嗦的内容一概省略. 第一部分,基础知识部分 我认为这个部分是这部书作为入门部分讲解比较深入和清楚地部分,对于ASP.NET,他的本质是web开发,不管是web form还是MVC,那么在进行web开发之前,首先就需要了解,为什么会出现ASP.NET,其实,在早期的时候,应该说是HTML和HTTP的天下,在计