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

Opt*_*mus 28 c# asp.net performance

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

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

Pan*_*ian 50

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

Response.Redirect
Cookies
Application Variables
HttpContext
Run Code Online (Sandbox Code Playgroud)

的Response.Redirect

SET:

Response.Redirect("Defaultaspx?Name=Pandian");
Run Code Online (Sandbox Code Playgroud)

GET:

string Name = Request.QueryString["Name"];
Run Code Online (Sandbox Code Playgroud)

饼干

SET:

HttpCookie cookName = new HttpCookie("Name");
cookName.Value = "Pandian"; 
Run Code Online (Sandbox Code Playgroud)

GET:

string name = Request.Cookies["Name"].Value;
Run Code Online (Sandbox Code Playgroud)

应用程序变量

SET:

Application["Name"] = "pandian";
Run Code Online (Sandbox Code Playgroud)

GET:

string Name = Application["Name"].ToString();
Run Code Online (Sandbox Code Playgroud)

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

  • `-1`表示cookie和应用程序.如果您使用webgarden或webfarm,则特殊应用程序无效!应用程序变量是一个简单的Dictionary <>,它只是为了与旧的asp兼容而存在,不能使用.此外,cookie不是用于从页面到页面的传输数据.非常糟糕的设计,不是很好的做法. (6认同)

शेख*_*ेखर 27

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

请仔细阅读以下几点.

1个查询字符串.

FirstForm.aspx.cs

Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);
Run Code Online (Sandbox Code Playgroud)

SecondForm.aspx.cs

TextBox1.Text = Request.QueryString["Parameter"].ToString();
Run Code Online (Sandbox Code Playgroud)

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

FirstForm.aspx.cs

Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));
Run Code Online (Sandbox Code Playgroud)

SecondForm.aspx.cs

TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());
Run Code Online (Sandbox Code Playgroud)

网址编码

  1. Server.URLEncode
  2. HttpServerUtility.UrlDecode

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

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

FirstForm.aspx.cs

TextBox1.Text = this.Context.Items["Parameter"].ToString();
Run Code Online (Sandbox Code Playgroud)

SecondForm.aspx.cs

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

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

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

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

FirstForm.aspx.cs

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

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

SecondForm.aspx.cs

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

在这里,我们将表单发布到另一个页面而不是自己.您可能会使用此方法在第二页中获取viewstate无效或错误.要处理这个错误就是放EnableViewStateMac=false

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

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

FirstForm.aspx.cs

<asp:Button id=buttonPassValue style=”Z-INDEX: 102? runat=”server” Text=”Button”         PostBackUrl=”~/SecondForm.aspx”></asp:Button>
Run Code Online (Sandbox Code Playgroud)

SecondForm.aspx.cs

TextBox1.Text = Request.Form["TextBox1"].ToString();
Run Code Online (Sandbox Code Playgroud)

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

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

SecondForm.aspx

TextBox textBoxTemp = (TextBox) PreviousPage.FindControl(“TextBox1?);
TextBox1.Text = textBoxTemp.Text;
Run Code Online (Sandbox Code Playgroud)

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

参考:MICROSOFT MSDN网站

快乐的编码!