Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass objects, not values, between pages in ASP.net c#?

Tags:

c#

asp.net

At the moment I pass values from one page to another. I need to pass objects between pages, how can I do this.

Any help is appreciated.

like image 632
Tassisto Avatar asked Mar 08 '11 08:03

Tassisto


People also ask

How do you pass a class object from one page to another in asp net?

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.

Can you pass object by value in C#?

By default object types are passed by value in C#. But when you pass a object reference to a method, modifications in the object are persisted. If you want your object to be inmutable, you need to clone it.


3 Answers

save object in Session or Cache and then redirect to other page? lets say you have a.aspx in a.aspx you add item to Session.

Session["Item"] = myObjectInstance; 

in b.aspx you will get object;

var myObjectInstance = (MyObjectInstance) Session["Item"];

but you should check if anyvalue is set in Session before using it.

like image 193
adt Avatar answered Sep 28 '22 07:09

adt


you could serialize the object into an input field in HTML and submit it via form. Then deserialize it back to the object on the page the form submits to with Request['paramName'].

/// <summary>
/// Serialize an object
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string Serialize<T>(T data)
{
    string functionReturnValue = string.Empty;

    using (var memoryStream = new MemoryStream())
    {
        var serializer = new DataContractSerializer(typeof(T));
        serializer.WriteObject(memoryStream, data);

        memoryStream.Seek(0, SeekOrigin.Begin);

        var reader = new StreamReader(memoryStream);
        functionReturnValue = reader.ReadToEnd();
    }

    return functionReturnValue;
}

/// <summary>
/// Deserialize object
/// </summary>
/// <param name="xml"></param>
/// <returns>Object<T></returns>
public static T Deserialize<T>(string xml)
{
    using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
    {
        var serializer = new DataContractSerializer(typeof(T));
        T theObject = (T)serializer.ReadObject(stream);
        return theObject;
    }
}

Just don't forget to HTML encode the data, when you pass it via URL.

like image 41
Rumplin Avatar answered Sep 28 '22 09:09

Rumplin


You can serialize an object in ASP very easily, here are 3 approaches suited for different types of requirements:

1- Using Session to pass objects on Asp:

//In A.aspx
//Serialize.
Object obj = MyObject;
Session["Passing Object"] = obj;

//In B.aspx
//DeSerialize.
MyObject obj1 = (MyObject)Session["Passing Object"];

Returntype of the method xyx = obj.Method;//Using the deserialized data.

2- To save on your Asp project solution itself:

//Create a folder in your asp project solution with name "MyFile.bin"
//In A.aspx
//Serialize.
IFormatter formatterSerialize = new BinaryFormatter();
Stream streamSerialize = new FileStream(Server.MapPath("MyFile.bin/MyFiles.xml"), FileMode.Create, FileAccess.Write, FileShare.None);
formatterSerialize.Serialize(streamSerialize, MyObject);
streamSerialize.Close();

//In B.aspx
//DeSerialize.
IFormatter formatterDeSerialize = new BinaryFormatter();
Stream streamDeSerialize = new FileStream(Server.MapPath("MyFile.bin/MyFiles.xml"), FileMode.Open, FileAccess.Read, FileShare.Read);
MyObject obj = (MyObject)formatterDeSerialize.Deserialize(streamDeSerialize);
streamDeSerialize.Close();

Returntype of the method xyx = obj.Method;//Using the deserialized data.

3- To save on client machine................

String fileName = @"C:\MyFiles.xml";//you can keep any extension like .xyz or .yourname, anything, not an issue.
//In A.aspx
//Serialize.
IFormatter formatterSerialize = new BinaryFormatter();
Stream streamSerialize = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
formatterSerialize.Serialize(streamSerialize, MyObject);
streamSerialize.Close();

//In B.aspx
//DeSerialize.
IFormatter formatterDeSerialize = new BinaryFormatter();
Stream stream1 = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
MyObject obj = (MyObject)formatterDeSerialize.Deserialize(stream1);
stream1.Close();

Returntype of the method xyx = obj.Method;//Using the deserialized data.
like image 27
Hari Kiran Avatar answered Sep 28 '22 07:09

Hari Kiran