webform页面传值

1、get方式

发送页

	<form id="form1" runat="server">
		<div>
			<a href="WebForm2.aspx?name=5">调转到Form2</a>
			<asp:Button ID="button2" Text="跳转页面" runat="server" onclick="button2_Click"/>
		</div>
	</form>
			
	protected void button2_Click(object sender, EventArgs e)
	{
		 Response.Redirect("WebForm2.aspx?name=5");
	}

接受页

 	this.Label1.Text = Request["name"];
    //this.Label2.Text = Request.Params["name"];
	//this.Label3.Text = Request.QueryString[0];

2、post方式

a\不带 runat="server"形式

发送页

	<form id="form2" action="WebForm2.aspx" method="post">
	        <input name="txtname" type="text" value="lilili"  />
	        <input type="submit" value="提交网页" />
	</form>

接受页

	this.Label1.Text =Request.Form["txtname"];

b\带 runat=“server”

发送页

	<form runat="server" id="form3">
		<input id="btnTransfer" type="button" onclick="post();" runat="server" value="跳转" /> 
	</form>
	<form id="form4" method="post">
		<input type="text" runat="server" id="txtname" value="lili" />
	</form>
	<script type="text/javascript">
		function post() {
			form4.action = "WebForm2.aspx";
			form4.submit();
		}
	</script>

接受页

	this.Label1.Text =Request.Form["txtname"];

3、Session 和 Application

	Session["name2"] = "sessontest";
    Application["name3"] = "applicationtest";

	this.Label2.Text =(string)Session["name2"];
	this.Label3.Text =(string)Application["name3"];

4、静态变量

发送页

	public static string statest="static string";
    protected void button2_Click(object sender, EventArgs e)
    {
        Server.Transfer("WebForm2.aspx");
    }

接受页

	this.Label1.Text = WebForm1.statest;

5、Context.Handler 获取控件

发送页

	<asp:TextBox ID="TextBox1" runat="server" Text="lilili"></asp:TextBox>
	<asp:Button ID="button2" Text="跳转页面" runat="server" onclick="button2_Click"/>
	
	protected void button2_Click(object sender, EventArgs e)
	{
	     Server.Transfer("WebForm2.aspx");
	}

接受页

	//获取post传过来的对象
	if (Context.Handler is WebForm1)
	{
	    WebForm1 poster = (WebForm1)Context.Handler;
	    this.Label1.Text = ((TextBox)poster.FindControl("TextBox1")).Text;
	}

6、Context.Handler 获取公共变量

发送页

	public string testpost = "testpost";
	protected void button2_Click(object sender, EventArgs e)
	{
	    Server.Transfer("WebForm2.aspx");
	}

接受页

	//获取post传过来的对象
	if (Context.Handler is WebForm1)
	{
	     WebForm1 poster = (WebForm1)Context.Handler;
	     this.Label2.Text = poster.testpost;
	 }

7、Context.Items 变量

发送页

	protected void button2_Click(object sender, EventArgs e)
    {
          Context.Items["name"] = "contextItems";
          Server.Transfer("WebForm2.aspx");
    }

接受页

	//获取post传过来的对象
	if (Context.Handler is WebForm1)
	{
	     this.Label3.Text = Context.Items["name"].ToString();
	}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值