Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在不使用会话的情况下在ASP.net中跨页面传递值_C#_Asp.net_Performance - Fatal编程技术网

C# 如何在不使用会话的情况下在ASP.net中跨页面传递值

C# 如何在不使用会话的情况下在ASP.net中跨页面传递值,c#,asp.net,performance,C#,Asp.net,Performance,我正在努力提高我的web门户的性能。我正在使用会话存储状态信息 但是我听说使用会话会降低应用程序的速度。在asp.net中,是否有其他方法在页面上传递值 实现这一点有多种方法。我可以向您简要介绍我们在日常编程生命周期中使用的4种类型 请仔细检查以下几点 1个查询字符串。 FirstForm.aspx.cs Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text); TextBox1.Text = Request.QueryS

我正在努力提高我的web门户的性能。我正在使用会话存储状态信息


但是我听说使用会话会降低应用程序的速度。在asp.net中,是否有其他方法在页面上传递值

实现这一点有多种方法。我可以向您简要介绍我们在日常编程生命周期中使用的4种类型

请仔细检查以下几点

1个查询字符串。

FirstForm.aspx.cs

Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);
TextBox1.Text = Request.QueryString["Parameter"].ToString();
Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));
TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());
TextBox1.Text = this.Context.Items["Parameter"].ToString();
this.Context.Items["Parameter"] = TextBox1.Text;
Server.Transfer("SecondForm.aspx", true);
private void Page_Load(object sender, System.EventArgs e)
{
   buttonSubmit.Attributes.Add("onclick", "return PostPage();");
}
function PostPage()
{
   document.Form1.action = "SecondForm.aspx";
   document.Form1.method = "POST";
   document.Form1.submit();
}
TextBox1.Text = Request.Form["TextBox1"].ToString();
<asp:Button id=buttonPassValue style=”Z-INDEX: 102″ runat=”server” Text=”Button”         PostBackUrl=”~/SecondForm.aspx”></asp:Button>
TextBox1.Text = Request.Form["TextBox1"].ToString();
SecondForm.aspx.cs

Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);
TextBox1.Text = Request.QueryString["Parameter"].ToString();
Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));
TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());
TextBox1.Text = this.Context.Items["Parameter"].ToString();
this.Context.Items["Parameter"] = TextBox1.Text;
Server.Transfer("SecondForm.aspx", true);
private void Page_Load(object sender, System.EventArgs e)
{
   buttonSubmit.Attributes.Add("onclick", "return PostPage();");
}
function PostPage()
{
   document.Form1.action = "SecondForm.aspx";
   document.Form1.method = "POST";
   document.Form1.submit();
}
TextBox1.Text = Request.Form["TextBox1"].ToString();
<asp:Button id=buttonPassValue style=”Z-INDEX: 102″ runat=”server” Text=”Button”         PostBackUrl=”~/SecondForm.aspx”></asp:Button>
TextBox1.Text = Request.Form["TextBox1"].ToString();
这是传递整型值或其他短参数时最可靠的方法。此方法的更高级之处是,如果您在通过查询字符串传递值时在值中使用任何特殊字符,您必须在将值传递到下一页之前对其进行编码。因此,我们的代码片段如下所示:

FirstForm.aspx.cs

Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);
TextBox1.Text = Request.QueryString["Parameter"].ToString();
Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));
TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());
TextBox1.Text = this.Context.Items["Parameter"].ToString();
this.Context.Items["Parameter"] = TextBox1.Text;
Server.Transfer("SecondForm.aspx", true);
private void Page_Load(object sender, System.EventArgs e)
{
   buttonSubmit.Attributes.Add("onclick", "return PostPage();");
}
function PostPage()
{
   document.Form1.action = "SecondForm.aspx";
   document.Form1.method = "POST";
   document.Form1.submit();
}
TextBox1.Text = Request.Form["TextBox1"].ToString();
<asp:Button id=buttonPassValue style=”Z-INDEX: 102″ runat=”server” Text=”Button”         PostBackUrl=”~/SecondForm.aspx”></asp:Button>
TextBox1.Text = Request.Form["TextBox1"].ToString();
SecondForm.aspx.cs

Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);
TextBox1.Text = Request.QueryString["Parameter"].ToString();
Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));
TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());
TextBox1.Text = this.Context.Items["Parameter"].ToString();
this.Context.Items["Parameter"] = TextBox1.Text;
Server.Transfer("SecondForm.aspx", true);
private void Page_Load(object sender, System.EventArgs e)
{
   buttonSubmit.Attributes.Add("onclick", "return PostPage();");
}
function PostPage()
{
   document.Form1.action = "SecondForm.aspx";
   document.Form1.method = "POST";
   document.Form1.submit();
}
TextBox1.Text = Request.Form["TextBox1"].ToString();
<asp:Button id=buttonPassValue style=”Z-INDEX: 102″ runat=”server” Text=”Button”         PostBackUrl=”~/SecondForm.aspx”></asp:Button>
TextBox1.Text = Request.Form["TextBox1"].ToString();
URL编码


  • 2。通过上下文对象传递值

    通过上下文对象传递值是另一种广泛使用的方法

    FirstForm.aspx.cs

    Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);
    
    TextBox1.Text = Request.QueryString["Parameter"].ToString();
    
    Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));
    
    TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());
    
    TextBox1.Text = this.Context.Items["Parameter"].ToString();
    
    this.Context.Items["Parameter"] = TextBox1.Text;
    Server.Transfer("SecondForm.aspx", true);
    
    private void Page_Load(object sender, System.EventArgs e)
    {
       buttonSubmit.Attributes.Add("onclick", "return PostPage();");
    }
    
    function PostPage()
    {
       document.Form1.action = "SecondForm.aspx";
       document.Form1.method = "POST";
       document.Form1.submit();
    }
    TextBox1.Text = Request.Form["TextBox1"].ToString();
    
    <asp:Button id=buttonPassValue style=”Z-INDEX: 102″ runat=”server” Text=”Button”         PostBackUrl=”~/SecondForm.aspx”></asp:Button>
    
    TextBox1.Text = Request.Form["TextBox1"].ToString();
    
    SecondForm.aspx.cs

    Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);
    
    TextBox1.Text = Request.QueryString["Parameter"].ToString();
    
    Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));
    
    TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());
    
    TextBox1.Text = this.Context.Items["Parameter"].ToString();
    
    this.Context.Items["Parameter"] = TextBox1.Text;
    Server.Transfer("SecondForm.aspx", true);
    
    private void Page_Load(object sender, System.EventArgs e)
    {
       buttonSubmit.Attributes.Add("onclick", "return PostPage();");
    }
    
    function PostPage()
    {
       document.Form1.action = "SecondForm.aspx";
       document.Form1.method = "POST";
       document.Form1.submit();
    }
    TextBox1.Text = Request.Form["TextBox1"].ToString();
    
    <asp:Button id=buttonPassValue style=”Z-INDEX: 102″ runat=”server” Text=”Button”         PostBackUrl=”~/SecondForm.aspx”></asp:Button>
    
    TextBox1.Text = Request.Form["TextBox1"].ToString();
    
    请注意,我们正在使用Server.Transfer而不是Response.Redirect导航到另一个页面。我们中的一些人还使用Session对象传递值。在该方法中,值存储在会话对象中,然后在第二页从会话对象中取出

    3。将表单发布到另一页而不是回发

    通过将页面发布到另一个表单来传递值的第三种方法。下面是一个例子:

    FirstForm.aspx.cs

    Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);
    
    TextBox1.Text = Request.QueryString["Parameter"].ToString();
    
    Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));
    
    TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());
    
    TextBox1.Text = this.Context.Items["Parameter"].ToString();
    
    this.Context.Items["Parameter"] = TextBox1.Text;
    Server.Transfer("SecondForm.aspx", true);
    
    private void Page_Load(object sender, System.EventArgs e)
    {
       buttonSubmit.Attributes.Add("onclick", "return PostPage();");
    }
    
    function PostPage()
    {
       document.Form1.action = "SecondForm.aspx";
       document.Form1.method = "POST";
       document.Form1.submit();
    }
    TextBox1.Text = Request.Form["TextBox1"].ToString();
    
    <asp:Button id=buttonPassValue style=”Z-INDEX: 102″ runat=”server” Text=”Button”         PostBackUrl=”~/SecondForm.aspx”></asp:Button>
    
    TextBox1.Text = Request.Form["TextBox1"].ToString();
    
    我们创建了一个javascript函数来发布表单

    SecondForm.aspx.cs

    Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);
    
    TextBox1.Text = Request.QueryString["Parameter"].ToString();
    
    Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));
    
    TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());
    
    TextBox1.Text = this.Context.Items["Parameter"].ToString();
    
    this.Context.Items["Parameter"] = TextBox1.Text;
    Server.Transfer("SecondForm.aspx", true);
    
    private void Page_Load(object sender, System.EventArgs e)
    {
       buttonSubmit.Attributes.Add("onclick", "return PostPage();");
    }
    
    function PostPage()
    {
       document.Form1.action = "SecondForm.aspx";
       document.Form1.method = "POST";
       document.Form1.submit();
    }
    TextBox1.Text = Request.Form["TextBox1"].ToString();
    
    <asp:Button id=buttonPassValue style=”Z-INDEX: 102″ runat=”server” Text=”Button”         PostBackUrl=”~/SecondForm.aspx”></asp:Button>
    
    TextBox1.Text = Request.Form["TextBox1"].ToString();
    
    在这里,我们将表单发布到另一个页面,而不是它本身。使用此方法可能会在第二页中获取viewstate无效或错误。要处理此错误,请将
    EnableViewStateMac=false

    4。另一种方法是为跨页回发添加控件的PostBackURL属性

    在ASP.NET 2.0中,Microsoft通过为跨页回发添加控件的PostBackURL属性解决了此问题。实现就是设置控件的一个属性,您就完成了

    FirstForm.aspx.cs

    Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);
    
    TextBox1.Text = Request.QueryString["Parameter"].ToString();
    
    Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));
    
    TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());
    
    TextBox1.Text = this.Context.Items["Parameter"].ToString();
    
    this.Context.Items["Parameter"] = TextBox1.Text;
    Server.Transfer("SecondForm.aspx", true);
    
    private void Page_Load(object sender, System.EventArgs e)
    {
       buttonSubmit.Attributes.Add("onclick", "return PostPage();");
    }
    
    function PostPage()
    {
       document.Form1.action = "SecondForm.aspx";
       document.Form1.method = "POST";
       document.Form1.submit();
    }
    TextBox1.Text = Request.Form["TextBox1"].ToString();
    
    <asp:Button id=buttonPassValue style=”Z-INDEX: 102″ runat=”server” Text=”Button”         PostBackUrl=”~/SecondForm.aspx”></asp:Button>
    
    TextBox1.Text = Request.Form["TextBox1"].ToString();
    
    在上面的例子中,我们为按钮分配PostBackUrl属性,我们可以确定它将发布到的页面,而不是它本身。在下一页中,我们可以使用请求对象访问上一页的所有控件

    您还可以使用PreviousPage类来访问上一页的控件,而不是使用classic请求对象

    SecondForm.aspx

    TextBox textBoxTemp = (TextBox) PreviousPage.FindControl(“TextBox1″);
    TextBox1.Text = textBoxTemp.Text;
    
    正如您所注意到的,这也是一个在页面之间传递值的简单而干净的实现

    参考:


    快乐编码

    您可以将其指定给隐藏字段,并使用

    var value= Request.Form["value"]
    

    如果它只是用于在页面之间传递值,而您只需要在一个请求中使用它。使用上下文。

    上下文

    上下文对象保存单个用户的数据 请求,并且它仅在请求期间保持。这个 上下文容器可以保存大量数据,但通常是这样 用于保存小块数据,因为它通常是为 每个请求都通过global.asax中的处理程序执行。上下文 容器(可从页面对象访问或使用 提供System.Web.HttpContext.Current)来保存需要的值 在不同的HttpModule和HttpHandler之间传递。它可以 也可用于保存与整个系统相关的信息 要求例如,IBuySpy门户填充一些配置 在应用程序\u BeginRequest期间将信息放入此容器 global.asax中的事件处理程序。请注意,这仅适用于以下情况: 当前请求;如果你需要一些仍然存在的东西 对于下一个请求,请考虑使用VIEWSTATE。设置和获取 上下文集合中的数据使用与您所使用的语法相同的语法 已经看到了与其他集合对象(如应用程序)的关系, 会话和缓存。此处显示了两个简单示例:


    使用上述方法。如果执行
    Server.Transfer
    操作,您在上下文中保存的数据现在将可用于下一页。您不必担心删除/整理此数据,因为它只适用于当前请求。

    您可以通过以下方式将值从一个页面传递到另一个页面

    Response.Redirect
    Cookies
    Application Variables
    HttpContext
    
    响应。重定向

    设置:

    获取:

    Cookies

    Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);
    
    TextBox1.Text = Request.QueryString["Parameter"].ToString();
    
    Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));
    
    TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());
    
    TextBox1.Text = this.Context.Items["Parameter"].ToString();
    
    this.Context.Items["Parameter"] = TextBox1.Text;
    Server.Transfer("SecondForm.aspx", true);
    
    private void Page_Load(object sender, System.EventArgs e)
    {
       buttonSubmit.Attributes.Add("onclick", "return PostPage();");
    }
    
    function PostPage()
    {
       document.Form1.action = "SecondForm.aspx";
       document.Form1.method = "POST";
       document.Form1.submit();
    }
    TextBox1.Text = Request.Form["TextBox1"].ToString();
    
    <asp:Button id=buttonPassValue style=”Z-INDEX: 102″ runat=”server” Text=”Button”         PostBackUrl=”~/SecondForm.aspx”></asp:Button>
    
    TextBox1.Text = Request.Form["TextBox1"].ToString();
    
    设置:

    获取:

    string name = Request.Cookies["Name"].Value;
    
    string Name = Application["Name"].ToString();
    
    应用程序变量

    Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);
    
    TextBox1.Text = Request.QueryString["Parameter"].ToString();
    
    Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));
    
    TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());
    
    TextBox1.Text = this.Context.Items["Parameter"].ToString();
    
    this.Context.Items["Parameter"] = TextBox1.Text;
    Server.Transfer("SecondForm.aspx", true);
    
    private void Page_Load(object sender, System.EventArgs e)
    {
       buttonSubmit.Attributes.Add("onclick", "return PostPage();");
    }
    
    function PostPage()
    {
       document.Form1.action = "SecondForm.aspx";
       document.Form1.method = "POST";
       document.Form1.submit();
    }
    TextBox1.Text = Request.Form["TextBox1"].ToString();
    
    <asp:Button id=buttonPassValue style=”Z-INDEX: 102″ runat=”server” Text=”Button”         PostBackUrl=”~/SecondForm.aspx”></asp:Button>
    
    TextBox1.Text = Request.Form["TextBox1"].ToString();
    
    设置:

    获取:

    string name = Request.Cookies["Name"].Value;
    
    string Name = Application["Name"].ToString();
    

    请参阅此处的完整内容:

    您可以使用查询字符串将值从一页传递到另一页

    1.使用查询字符串传递值

     Response.Redirect("Default3.aspx?value=" + txt.Text + "& number="+n);
    
    2.使用以下任何一种方法检索所需页面中的值

    方法1

        string v = Request.QueryString["value"];
        string n=Request.QueryString["number"];
    
          NameValueCollection v = Request.QueryString;
        if (v.HasKeys())
        {
            string k = v.GetKey(0);
            string n = v.Get(0);
            if (k == "value")
            {
                lbltext.Text = n.ToString();
            }
            if (k == "value1")
            {
                lbltext.Text = "error occured";
            }
        }
    
    方法2

        string v = Request.QueryString["value"];
        string n=Request.QueryString["number"];
    
          NameValueCollection v = Request.QueryString;
        if (v.HasKeys())
        {
            string k = v.GetKey(0);
            string n = v.Get(0);
            if (k == "value")
            {
                lbltext.Text = n.ToString();
            }
            if (k == "value1")
            {
                lbltext.Text = "error occured";
            }
        }
    

    注意:方法2是最快的方法。

    使用
    会话也不错
    ,但是在使用会话时可能会有一些因素。用户数量。你的带宽。服务器容量。如果您具备上述功能,您可以使用
    会话
    @ssilas777正常情况下
    缓存
    对所有用户都是相同的。因此,在他的情况下,他不能使用缓存。这取决于客户端或服务器上的缓存位置签出此MSDN状态管理帖子-它提供了对所有选项(客户端和服务器端)的深入了解,以及每个选项的优缺点。抱歉,我应该更加明确。答案现在已更新。
    -1
    用于cookies和应用程序。特皮西亚