2011-03-08 56 views
3

此刻我將值從一個頁面傳遞到另一個頁面。我需要在頁面之間傳遞對象,我該怎麼做。如何在ASP.net c#中的頁面之間傳遞對象,而不是值?

任何幫助表示讚賞。

+1

參見:[傳遞asp.net頁面之間的數據(http://stackoverflow.com/q/2639965/102112) – Alex 2011-03-08 08:57:15

+0

當你說 - 你從一個頁面傳遞到另一個值,我想你引用QueryString ....您可能想要使用Session將數據從一個頁面傳遞到另一個頁面。但是 - 還有其他的選擇(緩存對象/應用程序對象)可以根據需求管理頁面之間的數據。 – sajoshi 2011-03-08 08:57:18

+0

如果我想使用會話來做它,你能提供給我們一些代碼嗎? – SamekaTV 2011-03-08 09:18:55

回答

11

保存Session或Cache中的對象然後重定向到其他頁面? 可以說你在a.aspx中有一個a.aspx,你可以在Session中添加項目。

Session["Item"] = myObjectInstance; 

在b.aspx中您將獲得對象;

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

但是您應該檢查在使用它之前是否在Session中設置了任何值。

+0

它是如何在代碼中完成的? – SamekaTV 2011-03-08 09:14:55

+1

@Massimo編輯。 – adt 2011-03-08 09:22:53

+0

會話是邪惡的。 http://www.4guysfromrolla.com/webtech/faq/Advanced/faq4.shtml – 2014-10-30 21:23:54

0

一般的方法是將這些值放入會話中。然而,會話變量的一個經驗法則是它們應該保持在最低限度。

我們所做的就是將狀態保持在'狀態服務器'中。這只是數據庫中的一個表格,用於存儲用戶的值。

此表有一個xml列,其中包含來自我們州的對象xmlserialized。缺點是你依賴於xmlserializer,它是limitations。 Plus性能明智......每個請求上需要執行查詢,反序列化狀態,處理請求,再次序列化狀態並將更改更新回數據庫。當您擁有高流量的網站時,這不是最佳選擇。

如果硬件不是問題,更好的選擇是使用real stateserver並只使用會話。

4

您可以將對象序列化爲HTML中的input字段,並通過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; 
    } 
} 

當您通過URL傳遞數據時,請不要忘記對數據進行HTML編碼。

2

你可以很容易的序列化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. 
1

序列化的多個對象到列表中的元素以XML文件和反序列化他們

按鈕點擊或序列化點..

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);