send object from page to page in asp.net - asp.net

how can i send object from page to page in asp.net?

Have you thought about using a Session variable? You can save your object to a session varible and then retrieve it from the called page.
Here's an example:
Set from one page
Session["myobject"] = MyObject;
Retrieve from another
MyObject o = Session["myobject"]

You can use the Session to store a user-specific object between pages, or send data via a form GET or POST. There is also PreviousPage for a cross-page postback.

Session
Database
Context Cache
XML/Text File
Post/Get
Azure
Querystring
Pure willpower ;)

Related

Previous page title in asp.net

I am trying to get previous page title in my asp.net application.
My attempt :
string previousPageTitle = this.Page.PreviousPage.Title;
I am getting "NullReferenceException" as Object reference not set to an instance of an object.
I do not want to use session or query string to get previous page title.
See this SO question.
Is the reference to this.Page returning null? You could try accessing it through the HttpContext object:
((Page)HttpContext.Current.Handler).PreviousPage.Title
I believe you are going to the page directly, thus resulting in PreviousPage being null.
See Page.PreviousPage property
When you use the Transfer method or use cross-page posting to transfer
processing from one ASP.NET page to another, the originating page
contains request information that might be required for the
destination page. You can use the PreviousPage property to access that
information. If the current page is being rendered as a result of a
direct request (not a transfer or cross-post from another page), the
PreviousPage property contains null.

ASP.NET session alternative to pass object between pages

I want to pass an object between pages of my website. I know I can use session object for this.
Personally, I don't want to use any session or application or caching for this.
Is there any other alternative?
Please help.
Thanks,
Mahesh
Another alternative would be to serialize the object into a hidden field and when you post to the other page deserialize the value back to an object instance. Or yet another possibility would be to persist the object to some data store and then use an id to fetch it back.

Is there a way to return a value in the Response?

The problem is I redirect to a sub page from the main page by passing a few values in Request.
I need to get back the value I got from manipulating values I sent in request and I don't need to store value in Session.
You can use the PreviousPage property, depending on how you got to the new page.
There are a number of things you can do:
Page.Request["ID_Of_Control_You_Are_Interested_In"] will get you a value that you need.
If you Server.Transfer to the page you can call Context.Handler, cast it to the page you come from and traverse the controls collection to get your value.... (similar to PreviousPage property)
You can also inspect the Query string parameters sent in the request too:
Page.Request.QueryString("Param_To_Inspect")
you can return your values as JSON as the response and then using a jquery to get the data
You have three options: Query String Parameters, Cookies and Session State
Query String Parameters: http://www.aspnet101.com/2007/11/using-the-querystringparameter/
This is a good way to pass data between pages. It's the most commonly used and is limited to the max length of the url.
Cookies : http://msdn.microsoft.com/en-us/library/ms178194.aspx
This is a good method. It also has a size limitation. The advantage of cookies is you can persist data that can span visits to your site.
Session State : http://msdn.microsoft.com/en-us/library/ms178581.aspx
This an ok method, I'd use the two methods above before I would use session state. In my experience you can get into trouble very fast with Session state.
To answer your question: You can take the value from the request and load it into a TextBox. Then on the web page, the user edits the value and clicks a save button, which posts the changed value back to the server.
Or you could use javascript to post the value back to the server.

ASP.NET: How to redirect, prefilling form data?

i want a handler to redirect to a web-forms page, pre-filling in the values of some controls on the form.
i tried setting my current Request.Form data:
if (theyWantToDoSomething)
{
//pre-fill form values
context.Request.Form["TextBox1"] = "test";
context.Request.Form["ComboBox1"] = "test 2";
context.Request.Form["TextBox2"] = GetTheTextForTheThing();
//tell the client to go there
context.Response.Redirect("~/SomeWebForm.aspx");
return;
}
But i get an exception that Form values are read only.
What would be a way to send the client to another page, pre-filling form data?
Answer
i used the Session state to store values. It's important to note that by default a Handler doesn't have access to Session (the Session object will be null). You have to tell IIS to give you the Session object by adding the IRequiresSessionState marker interface to your handler class:
public class Handler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
...
if (theyWantToDoSomething)
{
//pre-fill form values
context.Session["thing1"] = "test";
context.Session["thing2"] = "test 2";
context.Session["thing3"] = GetTheTextForTheThing();
//tell the client to go there
context.Response.Redirect("~/SomeWebForm.aspx");
return; //not strictly needed, since Redirect ends processing
}
...
}
}
You can only populate your Response, the Request is input data and is indeed read-only.
If you are using ASP.NET, there are a variety of ways you could accomplish what you need:
The best way would probably be to pass the data you need to be pre-populated to SomeWebForm.aspx via the Session object, and on that pages Load method, populate your form. Keep in mind that when you do Response.Redirect, a 302 response is sent to the client with the URL the client should redirect to. The process is transparent to the user...but there is a full round trip involved.
Another alternative to populating the users Session would be to add GET parameters via a query string to the redirect to SomeWebForm.aspx.
If you need to transfer processing to the SomeWebForm.aspx page without round tripping, you could use Server.Transfer. This will transfer execution from the current page to the page you choose...however, this can cause some odd behavior on the client end because the URL does not update. As far as the user is concerned, it will still appear as though they are on the same page they started on.
A few ideas that might get you started:
Pass the values in the query string
Store the values in the session state or in a seperate cookie
Store the values in HttpContext.Items and use Server.Transfer instead of Response.Redirect
Another approach that hasn't been mentioned yet is using Server.Transfer makes it possible to use the Page.PreviousPage property, allowing access to the controls on the page that transferred control.
As jrista mentioned though, using Transfer doesn't update the URL shown to the user, which may or may not be an issue. For example, a user can't precisely bookmark a page they got transferred to since the URL will be that of the original transferring page.

Passing an object from one page to another in ASP.NET

On page1.aspx I have a button tied to a buttonOnClick event in my server side.
Clicking the button runs buttonOnClick method which generates an object (local to buttonOnclick method) with data I would like to use in the construction of a new page.
Basically, I want to open a new (different) page (page2.aspx) in a new tab, but as this page loads, I want it to render based on the contents of the object generated during buttonOnClick.
How one might go about this properly? Up until now I have been passing URL arguments to the new popup (page2.aspx) and I have it build the object, but I would rather do this properly and generate it buttonOnclick and then load page2.aspx in a popup based on what is the object built in the buttonOnclick method.
Store the object in your Session on the first page and then retrieve it on your second page.
On first page:
Session["myobject"] = myObject;
On second page:
MyObject object = (MyObject) Session["myobject"];
The easiest thing to do would be to store the object in the user's Session.
Session["myObject"] = myObject;
To get it out, you will need to cast it back to the type of MyObject, since the Session cache is simply a hashtable of objects.
if (Session["myObject"] != null)
{
MyObject myobj = (MyObject)Session["myObject"];
}
You can access it from both pages this way. Generally, if multiple pages are going to be accessing the same object in Session, I usually store the name of the session key in a centralized location so that it only has to be updated once should you ever decide to change it. Usually I add a resource file to my application and put it in the Properties folder, so I can use it this way:
string key = WebApplication1.Properties.MyObjectSessionKey;
Session[key] = myObject;
Use session state or store the object in a database and pass the id to the second page. The object will need to be serializable for this.

Resources