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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With