C# 在ASP.net 4.0中,在另一个页面上检索post值的最佳方法是什么

C# 在ASP.net 4.0中,在另一个页面上检索post值的最佳方法是什么,c#,asp.net,C#,Asp.net,我在互联网上搜索,但并没有找到任何最好的解决方案,我找到了一种方法,在那个里你们需要像编码html标签 <input type='text'> ... etc etc 是否有更好的方法检索该post值???您提供的示例对asp页面很有用,但我建议使用集合在asp.net中的webforms之间传递参数 下面是一个如何使用它的示例: using System; using System.Web.UI; public partial class _Default : Page {

我在互联网上搜索,但并没有找到任何最好的解决方案,我找到了一种方法,在那个里你们需要像编码html标签

<input type='text'> ... etc etc


是否有更好的方法检索该post值???

您提供的示例对asp页面很有用,但我建议使用集合在asp.net中的webforms之间传递参数

下面是一个如何使用它的示例:

using System;
using System.Web.UI;

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    string v = Request.QueryString["Param1"];
    if (v != null)
    {
        Response.Write("param is ");
        Response.Write(v);
    }
    }
}
您可以使用Response.Redirect连接URL:

string _url = string.Format("../WebFormA.aspx?Param1={0}&Param2={1}", _param1, _param2);
Response.Redirect(_url);

注意。

ASP.NET WebForms支持跨页发布。阅读和了解更多信息

示例:

WebForm1.aspx: 注意:按钮设置了PostBackUrl属性

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebFormApplication.WebForm1" EnableViewStateMac="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server" >
    <div>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="WebForm2.aspx" />

    </div>
    </form>
</body>
</html>

澄清“最佳”对你意味着什么。演出打字?简洁?健壮?我想你的意思是获取请求的post值(当然是到与初始页面不同的页面)。如果是这样,是的,就是这样。你面临的问题是什么?这真的很简单。我花了一段时间才知道你在问什么,但我使用的是yeh查询字符串:)RefFilter:best意味着优秀程序员采用哪一步……我看不出使用QueryString(GET)而不是表单(POST)有什么真正的好处。当你可以简单地使用Request.Form[“TextBox1”],访问上一页将执行上一页的整个页面生命周期page@AntonioBakula:AFAIK您将不会使用上一页运行上一页的生命周期。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebFormApplication.WebForm1" EnableViewStateMac="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server" >
    <div>

        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="WebForm2.aspx" />

    </div>
    </form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebFormApplication.WebForm2" %>
<%@ PreviousPageType VirtualPath="WebForm1.aspx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        This is page 2
        <asp:Label Text="n/a" runat="server" ID="Label1" ></asp:Label>
    </div>
    </form>
</body>
</html>
public partial class WebForm2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(PreviousPage.IsCrossPagePostBack)
        {
            var tb = PreviousPage.FindControl("TextBox1") as TextBox;
            Label1.Text = tb.Text;
        }
    }
}