How to Pass Values Across the Pages in ASP.NET Without Using Session

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

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

passing values between pages

As answered previously:

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

Passing session variable from page to page

You say that you've set the PostBackUrl to your second page. If you're going to do it that way, you need to use Page.PreviousPage to get access to your textbox. But this is the easiest way:

Firstly, leave the PostBackUrl alone. Setting the PostBackUrl to your second page means that you're telling the SECOND PAGE to handle your button click, not the first page. Hence, your session variable never gets set, and is null when you try to pull it.

This should work for ya.

And yes, you can also do this with a QueryString, but if its something that you don't want the user to see/edit, then a Session variable is better.

 protected void submit_Click(object sender, EventArgs e)
{
string name = txtFirstName.Text.Trim();
Session["name"] = name;
Response.Redirect("PageTwo.aspx");
}

Then in the second page (You don't REALLY need the ToString()):

 protected void Page_Load(object sender, EventArgs e)
{
if (Session["name"] != null)
{
lblName.Text = Session["name"].ToString();
}
}

EDIT -- Make sure that your button click actually gets fired. Someone can correct me wrong on this, as I do most of my work in VB.NET, not C#. But if you don't specify the OnClick value, your function won't get called.

 <asp:Button ID="Button1" runat="server" Text="Click Me!" OnClick="submit_Click" />

Best Practices for Passing Data Between Pages

Several months later, I thought I would update this question with the technique I ended up going with, since it has worked out so well.

After playing with more involved session state handling (which resulted in a lot of broken back buttons and so on) I ended up rolling my own code to handle encrypted QueryStrings. It's been a huge win -- all of my problem scenarios (back button, multiple tabs open at the same time, lost session state, etc) are solved and the complexity is minimal since the usage is very familiar.

This is still not a magic bullet for everything but I think it's good for about 90% of the scenarios you run into.

Details

I built a class called CorePage that inherits from Page. It has methods called SecureRequest and SecureRedirect.

So you might call:

 SecureRedirect(String.Format("Orders.aspx?ClientID={0}&OrderID={1}, ClientID, OrderID)

CorePage parses out the QueryString and encrypts it into a QueryString variable called CoreSecure. So the actual request looks like this:

Orders.aspx?CoreSecure=1IHXaPzUCYrdmWPkkkuThEes%2fIs4l6grKaznFGAeDDI%3d

If available, the currently logged in UserID is added to the encryption key, so replay attacks are not as much of a problem.

From there, you can call:

X = SecureRequest("ClientID")

Conclusion

Everything works seamlessly, using familiar syntax.

Over the last several months I've also adapted this code to work with edge cases, such as hyperlinks that trigger a download - sometimes you need to generate a hyperlink on the client that has a secure QueryString. That works really well.

Let me know if you would like to see this code and I will put it up somewhere.

One last thought: it's weird to accept my own answer over some of the very thoughtful posts other people put on here, but this really does seem to be the ultimate answer to my problem. Thanks to everyone who helped get me there.

How to pass data between pages without sessions in ASP.net MVC

You can get the query string using the

Request.Url.Query

and on your links to the other page you can send it.

Here is an idea of how you can find and change your page:

public abstract class BasePage : System.Web.UI.Page
{
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
System.IO.StringWriter stringWriter = new System.IO.StringWriter();

HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

// now you render the page on this buffer
base.Render(htmlWriter);

// get the buffer on a string
string html = stringWriter.ToString();

// manipulate your string html, and search all your links (hope full find only the links)
// this is a simple example of replace, THAT PROBABLY not work and need fix
html = html.Replace(".aspx", ".aspx?" + Request.Url.Query);

writer.Write(html);
}
}

I do not suggest it how ever, and I think that you must find some other way to avoid to manipulate all your links...

How to pass variable from page to another page in ASP.NET

You can pass the value as a querystring parameter.

So if you are using Response.Redirect you could do something like

protected void Button1_Click(object sender, EventArgs e){
Response.Redirect("Page2.aspx?value=" + taxtbox1.text);
}

On Page 2 you can get the value using Request["value"].ToString()

Notice that the querystring parameter name is what you request. So if you have ?something=else you will Request["something"]

Passing Variable from page to page using ASP.NET (C#) without using QueryString

You could look at using the ASP.NET Routing (which can be done without MVC) - then you can have a path with data but without a query-string; like the paths here (which are actually MVC, but the same logic applies).

Re not being able to use forms - you could simply use jQuery or similar to create the forms on the fly.

Passing values across pages ASP.net

If multiple users are using the same functionality and the id's are the same for each user, then use Application state, or a Singleton pattern.

If each user needs to have their own list of id's then you can use Session state which is unique for each user. In classic asp you shouldn't use an object eg collection but in ASP.NET you could place these in a list, array etc.

In ASP.NET you can share session state between servers if you enable the session service, or place session state in a database.

You could also consider cookies if the id's are numeric you could comma separate them and reload them with relatively low overhead, but with the caveat that a smart user may be able to alter the values.

Finally, you could store these values in a database table between calls.



Related Topics



Leave a reply



Submit