讀書筆記:《亮劍 .Net》——Context.Handler以及Context.Items

來源:互聯網
上載者:User

(1)Context.Handler擷取控制項

發送頁WebFormE1.aspx的HTML

 

<form id="form1" runat="server">
    <div>
<asp:TextBox ID="TextBox1" Text="litianping" runat="server"></asp:TextBox>       
<asp:Button ID="Button1" runat="server" Text="進入WebFormE2.aspx" 
    onclick="Button1_Click" />
    </div>
    </form>

 

發送頁:WebFormE1.aspx的CS代碼

protected void Button1_Click(object sender, EventArgs e)
        {
            Server.Transfer("WebFormE2.aspx");
        }

 

接收頁WebFormE2.aspx的HTML

 

<form id="form1" runat="server">
    <div>
    接收值: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </div>
    </form>

 

接收頁WebFormE2.aspx的CS代碼

 

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;
    }
}

 

 

(2)Context.Handler擷取公開變數

發送頁:WebFormF1.aspx的CS代碼

//定義一個公開變數
        public string strname = "litianping";
        protected void Page_Load(object sender, EventArgs e){}

        protected void Button1_Click(object sender, EventArgs e)
        {            
            Server.Transfer("WebFormF2.aspx");
        }

 

接收頁:

 

protected void Page_Load(object sender, EventArgs e)
{
    //擷取post過來的頁面對象
    if (Context.Handler is WebFormF1)
    {
        //取得頁面對象
        WebFormF1 poster = (WebFormF1)Context.Handler;
        this.TextBox1.Text = poster.strname;
    }
}

 


(3)Context.Items變數

發送頁:WebFormG1.aspx的HTML

 

<form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" Text="litianping" runat="server"></asp:TextBox>  
    <asp:Button ID="Button1" runat="server" Text="進入WebFormG2.aspx" 
    onclick="Button1_Click" />
    </div>
    </form>

 

發送頁:WebFormG1.aspx的CS代碼

 

protected void Button1_Click(object sender, EventArgs e)
{
    Context.Items["name"] = TextBox1.Text;
    Server.Transfer("WebFormG2.aspx");
}

 

接收頁:WebFormG2.aspx的CS代碼

 

protected void Page_Load(object sender, EventArgs e)
{
    //擷取post過來的頁面對象
    if (Context.Handler is WebFormG1)
    {                
        this.TextBox1.Text = Context.Items["name"].ToString();
    }
}

 

Context對象提供了對整個當前上下文(包括請求對象)的訪問。可以使用此對象共用頁面之間的資訊。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.