zoukankan      html  css  js  c++  java
  • .net深入体验与实战精要--ASP.NET开发大杂烩(转)

      转自:http://www.cnblogs.com/sunhan/p/3371337.html

    正巧今天遇到一个获取动态生成table中的一个动态生成的TextBox的值的时候总是findcontrol不到。后来经过我们的徐总,瞬间解决,但是我觉得对于一个页面的声明周期,我了解的还是很少,今天希望能通过这篇文章,再次了解一遍。

    3.1页面生命周期

    3.1.1独立页面生命周期事件顺序

    复制代码
     public partial class _Default : System.Web.UI.Page
        {
            protected void Page_PreInit(object sender, EventArgs e)
            {
                Response.Write("Page_PreInit<br/>");
            }
            protected void Page_Init(object sender, EventArgs e)
            {
                Response.Write("Page_Init<br/>");
    
            }
            protected void Page_InitComplete(object sender, EventArgs e)
            {
                Response.Write("Page_InitComplete<br/>");
    
            }
            protected void Page_PreLoad(object sender, EventArgs e)
            {
                Response.Write("Page_PreLoad<br/>");
    
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                Response.Write("Page_Load<br/>");
    
            }
            protected void Page_LoadComplete(object sender, EventArgs e)
            {
                Response.Write("Page_LoadComplete<br/>");
    
            }
            protected void Page_PreRender(object sender, EventArgs e)
            {
                Response.Write("Page_PreRender<br/>");
    
            }
            protected void Page_PreRenderComplete(object sender, EventArgs e)
            {
                Response.Write("Page_PreRenderComplete<br/>");
    
            }
            protected void Page_SaveStateComplete(object sender, EventArgs e)
            {
                Response.Write("Page_SaveStateComplete<br/>");
    
            }
            protected void Page_Unload(object sender, EventArgs e)
            {
                //Response.Write("Page_Unload<br/>");
                int i = 0;
                i++;//这行代码是用来设置断点的,这里不能用Response.Write
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                Response.Write("Button事件触发!<br/>");
            }
        }
    复制代码

    输出结果:

    复制代码
    Page_PreInit
    Page_Init
    Page_InitComplete
    Page_PreLoad
    Page_Load
    Page_LoadComplete
    Page_PreRender
    Page_PreRenderComplete
    Page_SaveStateComplete
    复制代码

    这就是独立页面的执行顺序,但我点击Button按钮的时候页面会进行Postback并重新加载页面,这个过程事件顺序:

    复制代码
    Page_PreInit
    Page_Init
    Page_InitComplete
    Page_PreLoad
    Page_Load
    Button事件触发!
    Page_LoadComplete
    Page_PreRender
    Page_PreRenderComplete
    Page_SaveStateComplete
    复制代码

    这部分虽然很简单,但是在我们获取页面控件或者相关值的时候,常常找不到控件,很大一部分原因是因为没有了解页面的生命周期事件顺序

    3.1.2具有Master页的声明周期事件顺序

    当页面有Master页的时候,将上面代码分别复制到Site.Matster和ContentPage.aspx,再次执行ContentPage.aspx会看到如下结果:

    复制代码
    Page_PreInit
    MasterPage_Init
    Page_Init
    Page_InitComplete
    Page_PreLoad
    Page_Load
    MasterPage_Load
    Page_LoadComplete
    Page_PreRender
    MasterPage_PreRender
    Page_PreRenderComplete
    Page_SaveStateComplete
    复制代码

    当点击按钮的时候

    复制代码
    Page_PreInit
    MasterPage_Init
    Page_Init
    Page_InitComplete
    Page_PreLoad
    Page_Load
    MasterPage_Load
    Button事件触发!
    Page_LoadComplete
    Page_PreRender
    MasterPage_PreRender
    Page_PreRenderComplete
    Page_SaveStateComplete
    复制代码

    3.1.3APS.NET声明周期详解

    网页事件

    典型的使用方式

    PreInit

    PreInit事件是网页生命周期中非常早起的一个事件,在PreInit事件触发之后,就会加载用户设置信息与网页主题。我们通常使用PreInit事件来执行下列处理:

    l         检查IsPostBack属性,来确认网页是否为第一次被处理。

    l         创建或重新创建动态控件。

    l         动态设置一个母版页。

    l         动态设置Theme属性。

    l         读取或设置用户设置文件属性值。

    Init

    在所有的控件都已经被初始化,而且所有的面板设置都已经应用之后,就会触发Init事件。可以使用此事件来读取或初始化控件属性。

    InitComplete

    此事件由Page对象触发。当网页初始化完成之后,就会触发此事件。

    PreLoad

    如果希望在Load事件触发之前,针对您的网页或控件执行一些处理,就可以使用此事件。在Page触发此事件之后,它会加载它本身与所有控件的视图状态,然后处理Request实例的回发数据。

    Load

    Page会调用Page上的Load事件处理例程,然后按照顺序为每一个子控件执行相同的操作,直到网页与所有控件被加载为止。我们通常会使用Load事件来设置控件的属性并创建数据库连接。

    控件事件

    我们通常使用控件的各个事件来完成各项互动操作。

    LoadComplete

    在网页上的所有其他控件都已经被加载之后,要执行的处理就可以通过此事件来完成。

    PreRender

    在控件已经被加载但是还没有解释之前,就会触发此事件。在此事件之前会执行下列操作:

    l         Page对象会调用网页与每一个控件的EnsureChildControls方法。

    l         DataSourceID属性被设置的数据绑定控件会调用其DataBind方法。

    网页上的每一个控件都会触发PreRender事件,您可以利用它对网页或其控件的内容做最后的更改。

    SavaStateComplete

    在已经为网页和网页上的所有控件保存了视图状态与控制状态信息后,就会触发此事件。此时对网页与控件所作的任何更改都会被忽略。

    如果您的操作是要在视图状态被保存之后才执行,则非常适合使用SaveStateComplete事件,但是请不要对控件进行任何更改。

    Render

    Render不是一个事件,而是一个方法。在此阶段,Page对象会调用每一个控件的Render方法。所有的ASP.NET服务器控件都拥有一个Render方法,它会输出要传送给浏览器的控件标记。当我们创建一个自定义控件时,通常会重写Render方法来输出控件的标记。然而,如果您的自定义控件仅仅是标准的ASP.NET服务器控件,而没有任何的自定义标记,则不需要重写Render方法。

    Unload

    每一个控件的Unload事件被触发之后,才会触发网页的Unload事件。对于控件而言,我们会使用此事件为特定的控件进行最后的清理工作,比方说,关闭某控件所使用的数据库连接。

    对于网页本身而言,同样会使用此事件来进行最后的清理工作,比方说,关闭已打开的文件与数据库连接、完成记录写入操作等。

    3.2页面状态管理

    3.2.1 Cookie

    1.创建

    复制代码
    HttpCookie cookie=new HttpCookie("user");
    
    cookie.Value="sunhan"//赋值只能通过字符串
    
    cookie["sex"]="";  //多个cookie赋值,或者:
    cookie.Values.Add("age","20");
    
    cookie.Expires=DateTime.Now.AddHours(1);//控制有效期,从当前时间之后的一个小时
    
    //将Cookie添加到内部的Cookie集合。Cookie集合中所有Cookie均通过Http输出流在set—Cookie头中发送到客户端
    Response.AppendCookie(cookie);
    //Reponse.Cookie.Add(cookie);
    复制代码

    2.读取

    复制代码
    HttpCookie cookie=Request.Cookies["user"];
    if(null==cookie)
    {
        Response.Write("木有")
    }
    else
    {
        Response.Write("cookie的全部值是:"+cookie.Value+"<br>");
        Response.Write("sex值是:"+cookie["sex"]+"<br>");
        Response.Write("age值是:"+cookie.Values["age"]+"<br>");
    }
    复制代码

    删除Cookie

    cookie.Expires=DateTime.Now.AddHours(-1);

    跨域读取Cookie

    3.2.2 HtmlInputHidden隐藏域

    要使用隐藏域,就必须使用Http-Post方法提交互联网网页,尽管其名字是隐藏域,但他的值并不是隐藏的,我们可以通过查看源代码进行查看。

    3.2.3 ViewState

    例如:

    复制代码
    protected void Page_Load(object sender,EventArgs e)
    {
         if (ViewState["arrList"]!=null)
         {
             PageArrayList=(ArrayList)ViewState["arrList"];//从viewstate中获取数据
         }
         else
         {
            PageArrayList=CreateArray();
         }
         this.GridView1.DataSoure=arrayList;
         this.GridView.DataBind();
    }
    复制代码

    保存:

    protected void Page_PreRender(object sender, EventArgs e )
    {
        //可以在页面呈现之前保存或更改ViewState数据
        ViewState.Add("arrList",PageArrayList);
    }

    注意点:

    • 与隐藏域不同的是,在使用查看源代码功能是,ViewState属性的值是不可见,他被压缩和加密的。
    • ViewState存在客户端,减轻服务器段压力
    • ViewState智能保存可序列化的对象
    • ViewState不适宜存储大量信息,会加重服务器解析负担
    • ViewState安全性低。
    • 可以关闭ViewState:EnableViewState="false".提高性能。

    3.2.4 Request

    3.2.5 Aplication对象

    如果没有使用程序强制释放,则Application对象存在于整个引用程序的生命周期内

    Application对象本质上就是一个Hash表,按键值存放了对象,由于对象时全局并且存在在服务器上,而且存在多线程同事访问,Application里面存在应该是访问较多,就该较少并且是全局,至少大部分功能会使用的数据,如计数器,或者数据库连接串等。

    3.2.6 Session对象

    1.Web.Config文件中的Session配置

    复制代码
    < sessionState mode="Off|InProc|StateServer|SQLServer" 
                  cookieless="true|false" 
                  timeout="number of minutes" 
                  stateConnectionString="tcpip=server:port" 
                  sqlConnectionString="sql connection string" 
                  stateNetworkTimeout="number of seconds" 
    />
    复制代码

    必备属性:

    • mode 设置将Session信息存储到哪里
    • Off 设置为不使用Session功能
    • InProc 设置为将Session存储在进程内,就是ASP中的存储方式,这是默认值。
    • StateServer 设置为将Session存储在独立的状态服务中。
    • SQLServer 设置将Session存储在SQL Server中。

    可选的属性是:

    • cookieless 设置客户端的Session信息存储到哪里
    • ture 使用Cookieless模式
    • false 使用Cookie模式,这是默认值。
    • timeout 设置经过多少分钟后服务器自动放弃Session信息。默认为20分钟  stateConnectionString 设置将Session信息存储在状态服务中时使用的服务器名称和端口号

    2.Session具体操作

    复制代码
    //存储
    Session["myname"]="Mike";
    //获取信息
    myname=Session["myname"];
    
    Session.Clear();//从session状态集合中移除所有键和值
    Session.Abandon();//取消当前session回话
    复制代码

    3.Session超时和莫名丢失处理

    原因:

    • 改动global.asax、Web.config、bin目录里的东西,导致Web Application重启。
    • 有些杀毒软件会扫描你的WebConfig。
    • 服务器内存不足
    • 程序内部让session丢失代码
    • 程序有框架页面和跨域情况

    解决方法:

    • 修改Web.config中timeout的时效事件
    • 建议让网上使用独立的引用程序池
    • ISS设置Session超时事件。网站属性->主目录->配置->应用程序池->响应
    • 在应用程序池上打开网站对应的应用程序池将web数量改为1,重启iss
    • 在如页面里嵌入一个iframe,设置宽度和高度为0,在里面加载页面的<head>里面加入<meta http-equiv="refresh" content="1080">就是定时请求。

    4.Session变量和Cookie区别

    存储位置不同,保存时间也不同

    3.3服务器和客户端数据交互

    3.3.1页面数据绑定全攻略

    1.在页面显示CS后台代码的字符串变量<%=%>、<%#%>

    复制代码
            <div>
                <p>
                    姓名:<%= name %>
                </p>
                <p>
                    性别:<%= sex %>
                </p>
                <p>
                    年龄:<%# old %>
                </p>
            </div>
            <a href="<%=url%>/test.aspx">进入</a>
    复制代码
    复制代码
    public partial class WebForm1 : System.Web.UI.Page
        {
            public string name = "ltp";
            public string sex = "man";
            public string old = "25";
            public string url = ConfigurationSettings.AppSettings["Url"];
            protected void Page_Load(object sender, EventArgs e)
            {
                //Page.DataBind(); //千万不能忘! <%# %>只有DataBind()后才有效
            }
        }
    复制代码

    如果Page.DataBing()被注销掉,则年龄不会显示。

    两者区别:<%#%>专门用于数据绑定。

    2.动态绑定服务器空间属性值

    复制代码
    public partial class WebForm2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Page.DataBind();
            }
        }
    复制代码
    复制代码
    <form id="form1" runat="server">
            <div>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
                    <asp:ListItem Value="images1.jpg" Selected="True">图1</asp:ListItem>
                    <asp:ListItem Value="images2.jpg">图2</asp:ListItem>
                    <asp:ListItem Value="images3.jpg">图3</asp:ListItem>
                </asp:DropDownList><br />
                <asp:Image ID="Image1" runat="server"
                    ImageUrl="<%# DropDownList1.SelectedItem.Value %>"></asp:Image>
            </div>
        </form>
    复制代码

    我本想把Page.DataBind()注销掉的。但是后来想到前台有<%#%>

    3.绑定HashTable到DataList

    复制代码
    <asp:DataList ID="DataList1" runat="server">
            <HeaderTemplate>
                <table width="200">
                    <tr>
                        <td width="100">
                            ENGLISH
                        </td>
                        <td>
                            中文
                        </td>
                    </tr>
                </table>
            </HeaderTemplate>
            <ItemTemplate>
                <table width="200">
                    <tr>
                        <td width="100">
                            <%# ((DictionaryEntry)Container.DataItem).Key %>
                        </td>
                        <td>
                            <%# ((DictionaryEntry)Container.DataItem).Value %>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>
    复制代码
    复制代码
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Hashtable list = new Hashtable();
                list.Add("ltp", "李天平");
                list.Add("zs", "张三");
                list.Add("ls", "李四");
                this.DataList1.DataSource = list;
                this.DataList1.DataBind();
            }
    
        }
    复制代码

    4.页面绑定使用服务器方法

     <%# GetSumCount(1,int.Parse(((DictionaryEntry)Container.DataItem).Key.ToString()))%>

    后台有这个方法。

    5.将DataView绑定到DataList,并调用服务器方法。

    6.使用DataBinder类进行绑定

    7.绑定格式化输出

    8.直接绑定执行javascript方法

    复制代码
        <script language="javascript">
            function DeleteForum(id) {
                if (confirm('你确认要删除这个论坛及它下面所有的主题吗?')) {
                    document.forms['Forums'].elements['paramID'].value = id;
                    __doPostBack('DeleteForum', '');
                }
            }
    
            function DeleteCategory(id) {
                if (confirm('你确认要删除这个类别和它的子论坛吗?')) {
                    document.forms['Forums'].elements['paramID'].value = id;
                    __doPostBack('DeleteCategory', '');
                }
            }
        </script>
        <form id="form1" runat="server">
            <div>
                <a href='<%# string.Format("javascript:DeleteCategory({0});",
            DataBinder.Eval(Container.DataItem, "CategoryID")) %>'>删除</a>
                <asp:HyperLink ID="HyperLink1" runat="server"
                    NavigateUrl='<%# "javascript:DeleteForum(" + DataBinder.Eval(Container.DataItem, "ForumID") + ")" %>'
                    Visible='<%# CanAdministerCategories %>' Text="删除" />
            </div>
        </form>
    复制代码

    3.4 ASP.NET编程中的技巧

    3.4.1页面之间传值的7种方法。

    1.get方法--也就是Request[""];

    2.使用内存变量Session和Application

    注:Application实质上市整个虚拟目录中所有文件的集合,如果想在整个应用范围内使用某个变量值,则Application对象时最佳选择。

    3.Post方法

    没有runat=server

        <form id="form1" action="WebFormC2.aspx" method="post">
            <input name="txtname" type="text" value="litianping" />
            <input type="submit" value="提交到WebFormC2.aspx" />
        </form>
    if (Request.Form["txtname"] != null)
                {
                    TextBox1.Text = Request.Form["txtname"];
                    //TextBox1.Text = Request.Form["SourceData"];
                }

    这是有runat=server

    复制代码
        <form id="Form1" method="post" runat="server">
            <input id="btnTransfer" type="button" onclick="post();" runat="server" value="提交到WebFormC2.aspx">
        </form>
        <form id="forPost" method="post">
            <input type="text" runat="server" id="txtname" value="litianping">
        </form>
    
        <script language="javascript">
            function post() {
                forPost.action = "WebFormC2.aspx";
                forPost.submit();
            }
        </script>
    复制代码

    4.静态变量

    复制代码
    //fasong
    //定义一个公共变量
            public static string str = "";
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                str = this.TextBox1.Text;
                Server.Transfer("WebFormD2.aspx");
            }
    //jieshou
    protected void Page_Load(object sender, EventArgs e)
            {
                this.TextBox1.Text = WebFormD1.str;
            }
    复制代码

    5.Context.Handler获取控件

                <asp:TextBox ID="TextBox1" Text="litianping" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" Text="进入WebFormE2.aspx"
                    OnClick="Button1_Click" />       // 后台事件       //Server.Tansfer("WebFormE2.aspx");

    接受页

    复制代码
    protected void Page_Load(object sender, EventArgs e)
            {
                //获取post过来的页面对象
                if (Context.Handler is WebFormE1)
                {
                    //取得页面对象
                    WebFormE1 poster = (WebFormE1)Context.Handler;
    
                    //取得控件                
                    this.TextBox1.Text = ((TextBox)poster.FindControl("TextBox1")).Text;
                    //this.TextBox1.Text = poster.TextBox1.Text;
                }
            }
    复制代码

    6.Context.Items变量

    发送:

            protected void Button1_Click(object sender, EventArgs e)
            {
                Context.Items["name"] = TextBox1.Text;
                Server.Transfer("WebFormG2.aspx");
            }
    复制代码
    //接收 protected void Page_Load(object sender, EventArgs e)
            {
                //获取post过来的页面对象
                if (Context.Handler is WebFormG1)
                {
                    this.TextBox1.Text = Context.Items["name"].ToString();
    
                }
            }
    复制代码

    3.4.4 Server.Transfer和Response.Redirect的区别

    Server.Transfer没有离开服务器,request,session等内部控件保存的信息不变。

    Response.Redirect发送一个HTTP响应到客户端浏览器,要进行页面跳转。

    3.4.5 刷新页面汇总

    1.实现页面自动刷新

    <meta http-equiv="refesh" content="5">

    每隔5秒刷新一次

    2.页面自动跳转

    <meta http-equiv="refesh" content="5";url=www.baidu.com>

    3.setTimeout实现

    <body onload="setTimeout('history.go(0)',5000)">

    <body onload="setTimeout('this.location.reload();',5000)">

    按钮刷新的n中方法

    复制代码
    <input type=button value="刷新" onclick="history.go()">
    <input type=button value="刷新" onclick="locaction.reload()">
    <input type=button value="刷新" onclick="location=location">
    <input type=button value="刷新" onclick="location.assign(location)">
    <input type=button value="刷新" onclick="document.execCommand('Refresh')">
    <input type=button value="刷新" onclick="window.navigate(lacation)">
    <input type=button value="刷新" onclick="location.replace(location)">
    <input type=button value="刷新" onclick="history.go()">
    <input type=button value="刷新" onclick="window.open
    ('www.baidu.com','_self')">
    <input type=button value="刷新" onclick="document.all.WebBrowser.ExecWB(22,1)">
    复制代码
  • 相关阅读:
    python基础1.0
    时间紧的跟狗一样,不动摇坚持下去!
    STM32(十三)- SDIO_SD读写测试
    STM32(十二)- TIM基本定时器和高级定时器
    STM32(十一)- 串行FLASH文件系统FatFs
    STM32(十)- SPI
    STM32(九)- I2C
    STM32(八)- DMA和常用存储器
    STM32(七)- UART
    STM32(六)- SysTick
  • 原文地址:https://www.cnblogs.com/bicabo/p/3375746.html
Copyright © 2011-2022 走看看