Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 902
Optimus Avatar asked Feb 19 '13 11:02

Optimus


People also ask

How do I pass values between ASP.NET pages?

The following options are available only when the source and target pages are in the same ASP.NET Web application. Use session state. Create public properties in the source page and access the property values in the target page. Get control information in the target page from controls in the source page.


2 Answers

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

like image 144
Pandian Avatar answered Oct 09 '22 08:10

Pandian


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.

Please go through the below points.

1 Query String.

FirstForm.aspx.cs

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

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

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

SecondForm.aspx.cs

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

URL Encoding

  1. Server.URLEncode
  2. HttpServerUtility.UrlDecode

2. Passing value through context object

Passing value through context object is another widely used method.

FirstForm.aspx.cs

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

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.

3. Posting form to another page instead of PostBack

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

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.

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

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

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.

FirstForm.aspx.cs

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

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.

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

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

HAPPY CODING!

like image 36
शेखर Avatar answered Oct 09 '22 08:10

शेखर