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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14956027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:32:42  来源:igfitidea点击:

How to pass values across the pages in ASP.net without using Session

c#asp.netperformance

提问by Optimus

I am trying to improve performance of my web portal. I'm using Session to store state information.

我正在努力提高我的门户网站的性能。我正在使用 Session 来存储状态信息。

But I heard that using session will decrease the speed of the application. Is there any other way to pass values across the page in asp.net.

但是我听说使用 session 会降低应用程序的速度。有没有其他方法可以在 asp.net 中跨页面传递值。

采纳答案by Pandian

You can pass values from one page to another by followings..

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

Response.Redirect
Cookies
Application Variables
HttpContext

Response.Redirect

响应.重定向

SET :

放 :

Response.Redirect("Defaultaspx?Name=Pandian");

GET :

得到 :

string Name = Request.QueryString["Name"];

Cookies

饼干

SET :

放 :

HttpCookie cookName = new HttpCookie("Name");
cookName.Value = "Pandian"; 

GET :

得到 :

string name = Request.Cookies["Name"].Value;

Application Variables

应用变量

SET :

放 :

Application["Name"] = "pandian";

GET :

得到 :

string Name = Application["Name"].ToString();

Refer the full content here : Pass values from one to another

请参阅此处的完整内容:将值从一个传递到另一个

回答by ????

There are multiple ways to achieve this. I can explain you in brief about the 4 types which we use in our daily programming life cycle.

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

Please go through the below points.

请通过以下几点。

1 Query String.

1 查询字符串。

FirstForm.aspx.cs

第一窗体.aspx.cs

Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);

SecondForm.aspx.cs

SecondForm.aspx.cs

TextBox1.Text = Request.QueryString["Parameter"].ToString();

This is the most reliable way when you are passing integer kind of value or other short parameters. More advance in this method if you are using any special characters in the value while passing it through query string, you must encode the value before passing it to next page.So our code snippet of will be something like this:

当您传递整数类型的值或其他短参数时,这是最可靠的方法。如果您在通过查询字符串传递值时使用值中的任何特殊字符,则此方法的更多进展,您必须在将其传递到下一页之前对该值进行编码。所以我们的代码片段将是这样的:

FirstForm.aspx.cs

第一窗体.aspx.cs

Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));

SecondForm.aspx.cs

SecondForm.aspx.cs

TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());

URL Encoding

网址编码

  1. Server.URLEncode
  2. HttpServerUtility.UrlDecode
  1. 服务器.URLEncode
  2. HttpServerUtility.UrlDecode

2. Passing value through context object

2. 通过上下文对象传递值

Passing value through context object is another widely used method.

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

FirstForm.aspx.cs

第一窗体.aspx.cs

TextBox1.Text = this.Context.Items["Parameter"].ToString();

SecondForm.aspx.cs

SecondForm.aspx.cs

this.Context.Items["Parameter"] = TextBox1.Text;
Server.Transfer("SecondForm.aspx", true);

Note that we are navigating to another page using Server.Transfer instead of Response.Redirect.Some of us also use Session object to pass values. In that method, value is store in Session object and then later pulled out from Session object in Second page.

请注意,我们使用 Server.Transfer 而不是 Response.Redirect 导航到另一个页面。我们中的一些人还使用 Session 对象来传递值。在该方法中,值存储在 Session 对象中,然后在第二页中从 Session 对象中取出。

3. Posting form to another page instead of PostBack

3. 将表单发布到另一个页面而不是 PostBack

Third method of passing value by posting page to another form. Here is the example of that:

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

FirstForm.aspx.cs

第一窗体.aspx.cs

private void Page_Load(object sender, System.EventArgs e)
{
   buttonSubmit.Attributes.Add("onclick", "return PostPage();");
}

And we create a javascript function to post the form.

我们创建了一个 javascript 函数来发布表单。

SecondForm.aspx.cs

SecondForm.aspx.cs

function PostPage()
{
   document.Form1.action = "SecondForm.aspx";
   document.Form1.method = "POST";
   document.Form1.submit();
}
TextBox1.Text = Request.Form["TextBox1"].ToString();

Here we are posting the form to another page instead of itself. You might get viewstate invalid or error in second page using this method. To handle this error is to put EnableViewStateMac=false

在这里,我们将表单发布到另一个页面而不是它本身。使用此方法,您可能会在第二页中获得视图状态无效或错误。处理这个错误是把EnableViewStateMac=false

4. Another method is by adding PostBackURL property of control for cross page post back

4.另一种方法是添加控件的PostBackURL属性进行跨页回发

In ASP.NET 2.0, Microsoft has solved this problem by adding PostBackURL property of control for cross page post back. Implementation is a matter of setting one property of control and you are done.

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

FirstForm.aspx.cs

第一窗体.aspx.cs

<asp:Button id=buttonPassValue style=”Z-INDEX: 102″ runat=”server” Text=”Button”         PostBackUrl=”~/SecondForm.aspx”></asp:Button>

SecondForm.aspx.cs

SecondForm.aspx.cs

TextBox1.Text = Request.Form["TextBox1"].ToString();

In above example, we are assigning PostBackUrl property of the button we can determine the page to which it will post instead of itself. In next page, we can access all controls of the previous page using Request object.

在上面的例子中,我们分配了按钮的 PostBackUrl 属性,我们可以确定它将发布到的页面而不是它本身。在下一页中,我们可以使用 Request 对象访问上一页的所有控件。

You can also use PreviousPage class to access controls of previous page instead of using classic Request object.

您还可以使用 PreviousPage 类来访问上一页的控件,而不是使用经典的 Request 对象。

SecondForm.aspx

二级表格.aspx

TextBox textBoxTemp = (TextBox) PreviousPage.FindControl(“TextBox1″);
TextBox1.Text = textBoxTemp.Text;

As you have noticed, this is also a simple and clean implementation of passing value between pages.

正如您所注意到的,这也是在页面之间传递值的简单而干净的实现。

Reference: MICROSOFT MSDN WEBSITE

参考:MICROSOFT MSDN 网站

HAPPY CODING!

快乐编码!

回答by Matt Evans

You can assign it to a hidden field, and retrieve it using

您可以将其分配给隐藏字段,并使用

var value= Request.Form["value"]

回答by Sam Shiles

If it's just for passing values between pages and you only require it for the one request. Use Context.

如果它只是为了在页面之间传递值而您只需要它用于一个请求。用Context.

Context

The Context object holds data for a single user, for a single request, and it is only persisted for the duration of the request. The Context container can hold large amounts of data, but typically it is used to hold small pieces of data because it is often implemented for every request through a handler in the global.asax. The Context container (accessible from the Page object or using System.Web.HttpContext.Current) is provided to hold values that need to be passed between different HttpModules and HttpHandlers. It can also be used to hold information that is relevant for an entire request. For example, the IBuySpy portal stuffs some configuration information into this container during the Application_BeginRequest event handler in the global.asax. Note that this only applies during the current request; if you need something that will still be around for the next request, consider using ViewState. Setting and getting data from the Context collection uses syntax identical to what you have already seen with other collection objects, like the Application, Session, and Cache. Two simple examples are shown here:

语境

Context 对象保存单个用户、单个请求的数据,并且仅在请求期间保留。Context 容器可以保存大量数据,但通常用于保存小块数据,因为它通常通过 global.asax 中的处理程序为每个请求实现。提供 Context 容器(可从 Page 对象或使用 System.Web.HttpContext.Current 访问)来保存需要在不同 HttpModules 和 HttpHandlers 之间传递的值。它还可用于保存与整个请求相关的信息。例如,IBuySpy 门户在 global.asax 中的 Application_BeginRequest 事件处理程序期间将一些配置信息填充到此容器中。请注意,这仅适用于当前请求;如果您需要下一个请求仍然存在的东西,请考虑使用 ViewState。从 Context 集合设置和获取数据使用的语法与您已经在其他集合对象(如 Application、Session 和 Cache)中看到的相同。这里展示了两个简单的例子:

// Add item to
Context Context.Items["myKey"] = myValue;

// Read an item from the
 Context Response.Write(Context["myKey"]);

http://msdn.microsoft.com/en-us/magazine/cc300437.aspx#S6

http://msdn.microsoft.com/en-us/magazine/cc300437.aspx#S6

Using the above. If you then do a Server.Transferthe data you've saved in the context will now be available to the next page. You don't have to concern yourself with removing/tidying up this data as it is only scoped to the current request.

使用以上。如果您随后执行了操作,Server.Transfer则您在上下文中保存的数据现在将可用于下一页。您不必担心删除/整理这些数据,因为它仅适用于当前请求。

回答by coder

You can use query stringto pass value from one page to another..

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

1.pass the value using querystring

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

 Response.Redirect("Default3.aspx?value=" + txt.Text + "& number="+n);

2.Retrive the value in the page u want by using any of these methods..

2.使用这些方法中的任何一种检索您想要的页面中的值..

Method1:

方法一

    string v = Request.QueryString["value"];
    string n=Request.QueryString["number"];

Method2:

方法二

      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";
        }
    }

NOTE:Method 2 is the fastest method.

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