簡體   English   中英

Master.cs在頁面內容代碼后面運行

[英]Master.cs running after the page content codebehind

我在使用母版頁設置會話變量,然后使用這些會話變量處理占位符中的內容時遇到問題。 但是,會話變量在內容中未顯示為已更新。 我在下面做了一個小例子。

我需要在母版頁和內容中都更新會話變量。 現在,仿佛背后的內容代碼在母版頁之前或同時運行。 我假設后面的母版頁代碼將首先運行,從而設置會話變量。 任何幫助將不勝感激。 謝謝! (使用C#構建.NET 2.0)

測試質量大師

<%@ Master Language="C#" inherits="gpworklist.testmass"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

    <html> 
    <head runat="server">
    </head>
    <body>
    <form id="testthisform">
    <select id="test1sel" name="test1" runat="server"></select>
    <select id="test2sel" name="test2" runat="server"></select>
    <input type="submit" value="Submit" />
    </form>
    <div id="testsess" runat="server"></div>
    <asp:ContentPlaceHolder id="cntnt_phldr" runat="server">
    </asp:ContentPlaceHolder>
    </body>
</html>

testmass.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace gpworklist
{
    public class testmass: MasterPage
    {
        public string defProv;

        public testmass()
        {
        }

        public void Page_Load()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
            Response.Cache.SetExpires(DateTime.MinValue);

            HtmlSelect sel1 = (HtmlSelect)this.FindControl("ctl00$test1sel");
            HtmlSelect sel2 = (HtmlSelect)this.FindControl("ctl00$test2sel");
            HtmlGenericControl div = (HtmlGenericControl)this.FindControl("ctl00$testsess");

            string def1;
            string def2;

            ListItem item;

            if (Request["ctl00$test1sel"] != null && Request["ctl00$test1sel"] != "")
            {
                Session["test1"] = Request["ctl00$test1sel"];
            }
            if (Request["ctl00$test2sel"] != null && Request["ctl00$test2sel"] != "")
            {
                Session["test2"] = Request["ctl00$test2sel"];
            }

            if (Session["test1"] != null)
            {
                def1 = Session["test1"].ToString();
            }
            else
            {
                def1 = "";
            }
            if (Session["test2"] != null)
            {
                def2 = Session["test2"].ToString();
            }
            else
            {
                def2 = "";
            }

            for (int i = 0; i <= 10; i++)
            {
                item = new ListItem(i.ToString(), i.ToString());
                if (i.ToString() == def1)
                {
                    item.Selected = true;
                }
                sel1.Items.Add(item);
            }
            //Session["test1"] = sel1.Value;

            for (int i = 0; i <= 10; i++)
            {
                item = new ListItem(i.ToString(), i.ToString());
                if (i.ToString() == def2)
                {
                    item.Selected = true;
                }
                sel2.Items.Add(item);
            }
            Session["test2"] = sel2.Value;

            div.InnerHtml = Session["test1"] + " - " + Session["test2"];

        }
    }
}

testpage.aspx

<%@ Page Language="C#" MasterPageFile="testmass.master" Inherits="gpworklist.testpage"%>
<asp:content ID="contentstuff" contentplaceholderid="cntnt_phldr" runat="server">
    <div id="testpagesess" runat="server"></div>
</asp:content>

testpage.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace gpworklist
{
    public class testpage: Page
    {
        public testpage()
        {
        }

        public void Page_Load()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
            Response.Cache.SetExpires(DateTime.MinValue);

            HtmlGenericControl div = (HtmlGenericControl)this.FindControl("ctl00$cntnt_phldr$testpagesess");

            div.InnerHtml = Session["test1"] + " - " + Session["test2"];
        }
    }
}

內容頁面的事件確實在同一母版頁事件之前觸發。 參見http://msdn.microsoft.com/zh-cn/library/vstudio/dct97kc3(v=vs.100).aspx

您應該使用MasterPageInit事件,而不是Load事件。 這將在Page.Load之前Page.Load

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM