读书笔记:《亮剑 .Net》——Context.Handler以及Context.Items
(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>
<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");
}
{
Server.Transfer("WebFormE2.aspx");
}
接收页WebFormE2.aspx的HTML
<form id="form1" runat="server">
<div>
接收值: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
<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;
}
}
{
//获取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");
}
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;
}
}
{
//获取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>
<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");
}
{
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();
}
}
{
//获取post过来的页面对象
if (Context.Handler is WebFormG1)
{
this.TextBox1.Text = Context.Items["name"].ToString();
}
}
Context对象提供了对整个当前上下文(包括请求对象)的访问。可以使用此对象共享页面之间的信息。