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

I am currently passing values ​​from one page to another. I need to pass objects between pages, how can I do this.

Any help is appreciated.

+3
source share
5 answers

save an object in a session or cache and then redirect to another page? let's say you have a.aspx in a.aspx, you add an element to Session.

Session["Item"] = myObjectInstance; 

in b.aspx you will get an object;

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

but you have to check if any value is set in the session before use.

+11
source

input HTML form. , 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;
    }
}

HTML , URL.

+4

ASP, 3 , :

1- 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- Asp:

//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- ................

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.
+2

xml

using System.Xml.Serialization;

[XmlRoot("ClassList")]
[XmlInclude(typeof(ClassElements))] //Adds class containing elentsof list
public class ClassList
{
    private String FFolderName;
    private List<ClassElements> ListOfElements;

    [XmlArray("ClassListArray")]
    [XmlArrayItem("ClassElementObjects")]
    public List<ClassElements> ListOfElements = new List<ClassElements>();

    [XmlElement("Listname")]
    public string Listname { get; set; }

    public void InitilizeClassVars() 
    {

    }

    public ClassList() 
    {
        InitilizeClassVars();
    }

    public void AddClassElementObjects aItem)
    {
        ListOfElements.Add(aItem);
    }
}

[XmlType("ClassElements")]
public class ClassElements
{
    private String str1;
    private int iInt;
    private Double dblDouble;

    [XmlAttribute("str", DataType = "string")]
    public String str
    {
        get { return str; }
    }

    [XmlElement("iInt")]
    public int iInt
    {
        get { return iInt;}
        set { iInt = value; }
    }

    [XmlElement("dblDouble")]
    public Double dblDouble
    {
        get { return dblDouble; }
        set { dblDouble = value; }
    }

    public void InitilizeClassVars()
    {

    }

    public ClassElements()
    {
        InitilizeClassVars();
    }
}

" " "".

ClassList ListOfObjs = new ClassList();
int Count = 5;

for (int i = 0; i < Count; i++)
{
    ClassElements NewObj = new ClassElements();
    NewObj.str = "Hi";
    NewObj.iInt = 500;
    NewObj.dblDouble = 5000;
    ListOfObjs.Add(NewObj);
}

// Serialize 
String fileName = @"C:\MyFiles.xml";

Type[] elements = { typeof(ClassElements) };
XmlSerializer serializer = new XmlSerializer(typeof(ClassList), elements);
FileStream fs = new FileStream(fileName, FileMode.Create);
serializer.Serialize(fs, ListOfObjs);
fs.Close();
ListOfObjs = null;

" " "".

ClassList ListOfObjs = new ClassList();

String fileName = @"C:\MyFiles.xml";

// Deserialize 
fs = new FileStream(fileName , FileMode.Open);
personen = (ListOfObjs)serializer.Deserialize(fs);
serializer.Serialize(Console.Out, ListOfObjs);
+1

, . , .

, " ". , .

xml, , xmlserialized. , xmlserializer limitations. ... , , , . , .

If hardware is not a problem, the best alternative is to use a real state server and just use a session.

0
source

All Articles