如何获取文本值

时间:2011-12-08 08:41:43

标签: asp.net c#-4.0

当我点击提交按钮时,我有一个名称和年龄的文本字段,这些值应该传递给另一个页面我该怎么做? 名称 AGE

SUBMIT

protected void Page_Load(object sender, EventArgs e)

{

       {

            litText.Text = Request.Form["tbName"] + ": " + Request.Form["tbAge"];
        }
    }

    public Default3()
    {
        Load += Page_Load;
    }
} 

1 个答案:

答案 0 :(得分:4)

在.aspx文件中:

<asp:TextBox id="txbAge" runat="server"></asp:TextBox>
<asp:TextBox id="txbName" runat="server"></asp:TextBox>
<asp:Button id="btnSubmit" runat="server" onclick="btnSubmit_Click" />

在aspx.cs文件中:

protected void btnSubmit_Click(object sender, EventArgs e)
{
  string age = txbAge.Text;
  string name = txbName.Text;
  string url = string.Format("~/anotherPage.aspx?age={0}&name={1}", age, name);
  Response.Redirect(url);
}

在第二个.aspx文件中:

<aspx:Label id="lblAge" runat="server"></aspx:Label>
<aspx:Label id="lblName" runat="server"></aspx:Label>

在第二个.aspx.cs文件中:

protected void Page_Load(object sender, EventArgs e)
{
  string age = string.Empty;
  string name = string.Empty;

  if(!String.IsNullOrEmpty(Request.QueryString["age"]))
    age = Request.QueryString["age"];
  if(!String.IsNullOrEmpty(Request.QueryString["name "]))
    age = Request.QueryString["name "];

  lblAge.Text = age;
  lblName.Text = name;

}

这就是如何在查询字符串中的另一个页面上获取这些值的方法。

马里乌什