简体   繁体   English

如何在不使用Session的情况下在ASP.net中传递值

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

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. 是否有任何其他方法可以在asp.net中的页面中传递值。

You can pass values from one page to another by followings.. 您可以通过以下方式将值从一个页面传递到另一个页面。

Response.Redirect
Cookies
Application Variables
HttpContext

Response.Redirect 的Response.Redirect

SET : SET:

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

GET : GET:

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

Cookies 饼干

SET : SET:

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

GET : GET:

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

Application Variables 应用程序变量

SET : SET:

Application["Name"] = "pandian";

GET : GET:

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

Refer the full content here : Pass values from one to another 请参阅此处的完整内容:将值从一个传递到另一个

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 FirstForm.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 FirstForm.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 Server.URLEncode
  2. HttpServerUtility.UrlDecode HttpServerUtility.UrlDecode

2. Passing value through context object 2.通过上下文对象传递值

Passing value through context object is another widely used method. 通过上下文对象传递值是另一种广泛使用的方法。

FirstForm.aspx.cs FirstForm.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. 请注意,我们使用Server.Transfer而不是Response.Redirect导航到另一个页面。我们中的一些人也使用Session对象传递值。 In that method, value is store in Session object and then later pulled out from Session object in Second page. 在该方法中,值存储在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 FirstForm.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. 您可能会使用此方法在第二页中获取viewstate无效或错误。 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 另一种方法是为跨页回发添加控制的PostBackURL属性

In ASP.NET 2.0, Microsoft has solved this problem by adding PostBackURL property of control for cross page post back. 在ASP.NET 2.0中,Microsoft通过为跨页回发添加控件的PostBackURL属性来解决此问题。 Implementation is a matter of setting one property of control and you are done. 实现是设置一个控制属性的问题,你就完成了。

FirstForm.aspx.cs FirstForm.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. 在上面的示例中,我们分配了按钮的PostBackUrl属性,我们可以确定它将发布到的页面而不是它自己。 In next page, we can access all controls of the previous page using Request object. 在下一页中,我们可以使用Request对象访问上一页的所有控件。

You can also use PreviousPage class to access controls of previous page instead of using classic Request object. 您还可以使用PreviousPage类访问上一页的控件,而不是使用经典的Request对象。

SecondForm.aspx SecondForm.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! 快乐的编码!

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. Context对象为单个用户保存单个请求的数据,并且仅在请求期间保留。 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. Context容器可以容纳大量数据,但通常用于保存小块数据,因为它通常通过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. Context容器(可从Page对象访问或使用System.Web.HttpContext.Current)用于保存需要在不同的HttpModule和HttpHandler之间传递的值。 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. 例如,IBuySpy门户在global.asax中的Application_BeginRequest事件处理程序期间将一些配置信息填充到此容器中。 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. 如果你需要的东西仍然可以用于下一个请求,请考虑使用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. 从Context集合中设置和获取数据使用的语法与您在其他集合对象(例如Application,Session和Cache)中看到的语法相同。 Two simple examples are shown here: 这里显示了两个简单的例子:

// 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.Transfer the data you've saved in the context will now be available to the next page. 如果您随后执行Server.Transfer ,您在上下文中保存的数据现在可用于下一页。 You don't have to concern yourself with removing/tidying up this data as it is only scoped to the current request. 您不必担心删除/整理此数据,因为它只限于当前请求。

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

var value= Request.Form["value"]

You can use query string to 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 : 方法1

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

Method2 : 方法2

      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是最快的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 跨页面ASP.net传递值 - Passing values across pages ASP.net 如何:在ASP.NET网页之间传递敏感值? - How to: Pass sensitive values Between ASP.NET Web Pages? MS Lightswitch-如何设置在Silverlight Web客户端和asp.net页面之间共享的会话变量 - MS Lightswitch - How to set session variable shared across silverlight web client and asp.net pages 如何使用 Postman 将值传递给 ASP.Net Core APiController - How to pass values to an ASP.Net Core APiController using Postman 在 ASP.Net Core Razor Pages 中使用会话包装器 - Using a session wrapper in ASP.Net Core Razor Pages 使用Session asp.net将登录信息传输到不同的页面 - transfer login information to different pages using Session asp.net 如何在ASP.net c#中的页面之间传递对象而不是值? - How to pass objects, not values, between pages in ASP.net c#? 如何在ASP.NET的所有页面中维护会话? - How to maintain the session throughout all pages in ASP.NET? 在具有母版页的asp.net上的会话超时 - Session timeout on a asp.net with master pages 如何在 ASP.NET 中跨实体请求保存状态而不使用 EntityFramework 保存到数据库? - How to save state across requests for an entity in ASP.NET without saving to database using EntityFramework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM