Last post Mar 02, 2007 03:52 AM by XOR_
Mar 01, 2007 09:10 AM|mycwcgr|LINK
In the following code, I hope to click btnSet button to set MyTicket, and click btnGet button to get the result of MyTicket.
I know the result in the following code is not right, because Http is no state, but I can't store a struct to HiddenField control, how can I do? many thanks!
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
private struct Ticket
public string Name;
public int count;
private Ticket MyTicket;
protected void btnSet_Click(object sender, EventArgs e)
MyTicket = new Ticket();
MyTicket.Name = "Paul";
protected void btnGet_Click(object sender, EventArgs e)
Label1.Text = MyTicket.Name + MyTicket.count.ToString();
<html xmlns="http://www.w3.org/1999/xhtml" >
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<asp:Button ID="btnSet" runat="server" Text="Set" OnClick="btnSet_Click" />
<asp:Button ID="btnGet" runat="server" Text="Get" OnClick="btnGet_Click" /></div>
Mar 01, 2007 08:32 PM|SGWellens|LINK
The easiest way is to use two hidden fields, one for each member of the struct.
Or, you can write ToString and FromString functions to convert the struct to and from a string.
Mar 01, 2007 11:15 PM|mycwcgr|LINK
Mar 02, 2007 01:48 AM|XOR_|LINK
i think you should put the value into the view state.
structtype s = (structType) ViewState["Struct"]; //to retrieve
Mar 02, 2007 03:36 AM|mycwcgr|LINK
but I get the following error, could you give me the complete code?
Type 'ASP.default_aspx+Ticket' in Assembly 'App_Web_pzyempuh, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Runtime.Serialization.SerializationException: Type 'ASP.default_aspx+Ticket' in Assembly 'App_Web_pzyempuh, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Mar 02, 2007 03:52 AM|XOR_|LINK
ok then. u need to add the Serializable attributes to the strcuttype class.