日期:2014-05-18  浏览次数:20628 次

Server.Transfer 传值问题
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>无标题页</title>
</head>
<body>
  <form id="form1" method="get" runat="server" > //注意 method="get"
  <div>
  <input type="text" id="txtName" value="hello" runat="server" />
  <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Transfer" /></div>
  </form>
</body>
</html>

using System;
using System.Data;
using System.Web;
public partial class Study_Default3 : System.Web.UI.Page
{
  public string mValue = "i'm transfer data";

  protected void Page_Load(object sender, EventArgs e)
  {
  Response.Write(txtName.Value);
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
  Server.Transfer("Default4.aspx",true);
   
  }
}
----------------------------
public partial class Study_Default4 : System.Web.UI.Page //Default4.aspx.cs
{
  
  protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
  Study_Default3 obj = (Study_Default3)this.Context.Handler;
  Response.Write(obj.mValue);
  }
  }
  protected void Button1_Click(object sender, EventArgs e)
  {
  Response.Write(Request.QueryString["txtName"].ToString()); 
  //问题?这里可以得到 default3(method="get")的txtName 如果method="post" 能得到txtName 的值吗 如何得到
  }
}

------解决方案--------------------
注意一下:
if(Request["txtName"]!= null)
txtName是你的Input控件的name,要一致
<input type="text" id="txtName" value="hello" runat="server" />