Can you use structures in asp.net?

C

COHENMARVIN

I want to have an array of structures in my application. Each
structure might hold some info, like this:
Structure MyStruct
FirstName as String
LastName as String
age as integer
End Structure
I would show a series of web pages, and each would ask the user for a
LastName, FirstName, and age. When he fills out the form, the values
go into the array.
The problem is, in asp.net, that the only way to hold this information
is in a session variable. That way the array info is not lost when
going from page to page. So if I declared the array and its Structure
type in a Page_Load procedure of my first page, the next pages in the
series would have to remember the Structure type. Is there a way to do
this?
-- Thanks,
Marvin
 
S

S. Justin Gengo

Marvin,

There are many ways to do this. You mentioned one, session variables,
another would be to store the structure directly in viewstate on the
clientmachine, which is what I typically do. To do so you need to make the
structure serializable. The easiest way to do that is declare it so in
vb.net you declare a class, structure, whay have you as serializable by
placing <Serializable()> directly in front of the object declaration.

<Serializable()> _
Structure MyStruct
FirstName As String
LastName As String
Age As int32
End Structure

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
C

COHENMARVIN

Thanks for your answer. I do have another question though, and that
is, suppose you have an array of structures. How would you store that
in viewstate?
Marvin

"Chaos and Order are not enemies, only opposites"
Richard Garriott
 
J

Juan T. Llibre

Sorry. No can do.

ViewState is the mechanism ASP.NET uses to keep track of
server control state values that don't otherwise post back as
part of the HTTP form.

An array of structures is not a server control,
so you can't track it through viewstate.




Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
 
A

Alan Silver

Sorry. No can do.
ViewState is the mechanism ASP.NET uses to keep track of
server control state values that don't otherwise post back as
part of the HTTP form.

Can't you add your own stuff to the view state? I thought it was
available for storage of info between postbacks.
An array of structures is not a server control,
so you can't track it through viewstate.

Ignoring the problem of how ASP.NET encodes the information (which may
be a separate reason why you can't do it), surely doing...

ViewState["myValue"] = myValue;

will store the contents of myValue in the view state, even when myValue
is not a server control?

Please unconfuse me ;-)
 
K

Kevin Spencer

ViewState creates a hidden HTML form field in the document sent to the
client. Therefore, any data saved to ViewState must be serializable as text,
and any data that can be serialized as text can be stored in ViewState.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Complex things are made up of
Lots of simple things.

Alan Silver said:
Sorry. No can do.

ViewState is the mechanism ASP.NET uses to keep track of
server control state values that don't otherwise post back as
part of the HTTP form.

Can't you add your own stuff to the view state? I thought it was available
for storage of info between postbacks.
An array of structures is not a server control,
so you can't track it through viewstate.

Ignoring the problem of how ASP.NET encodes the information (which may be
a separate reason why you can't do it), surely doing...

ViewState["myValue"] = myValue;

will store the contents of myValue in the view state, even when myValue is
not a server control?

Please unconfuse me ;-)
Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
 
A

Alan Silver

ViewState creates a hidden HTML form field in the document sent to the
client. Therefore, any data saved to ViewState must be serializable as text,
and any data that can be serialized as text can be stored in ViewState.

Which is exactly what I said, only more concise!!

My question was that Juan appeared to be saying that only server control
data could be stored in the view state. You have confirmed my impression
that you could store other stuff in there too.

Ta ra
 
K

Kevin Spencer

My question was that Juan appeared to be saying that only server control
data could be stored in the view state.

Well, I doubt that that is what Juan was saying, although for all I know it
might have come across that way to you. Put it down to a communications
glitch.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Complex things are made up of
Lots of simple things.
 
A

Alan Silver

My question was that Juan appeared to be saying that only server control
Well, I doubt that that is what Juan was saying, although for all I know it
might have come across that way to you. Put it down to a communications
glitch.

Well, I must admit I was surprised, but when he said "An array of
structures is not a server control, so you can't track it through
viewstate", I read it to mean that you can't add anything that isn't a
server control to the view state, which surprised me.

Anyway, I am now unconfused, so I can sleep easily (not that I would
have much trouble anyway, I'm tired enough!!).

Ta ra
 
J

Juan T. Llibre

re:
Well, I doubt that that is what Juan was saying

I wasn't saying that, and I didn't say that.



Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
Kevin Spencer said:
My question was that Juan appeared to be saying that only server control data could be
stored in the view state.

Well, I doubt that that is what Juan was saying, although for all I know it might have
come across that way to you. Put it down to a communications glitch.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Complex things are made up of
Lots of simple things.
 
J

Juan T. Llibre

re:
Well, I must admit I was surprised, but when he said "An array of structures is not a
server control, so you can't track it through viewstate", I read it to mean that you
can't add anything that isn't a server control to the view state,

I'd be very pleased to see how you can
store an "array of structures" in viewstate.




Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
 
A

Alan Silver

Well, I must admit I was surprised, but when he said "An array of
I'd be very pleased to see how you can
store an "array of structures" in viewstate.

I didn't say *I* could!! I didn't even say it could be done at all. I
was questioning the (apparently incorrect) inference that view state was
only for server controls.

I did add a caveat that my comments were subject to the practical
reality of serialising the info you wanted to put in view state. I can
well believe that an array of structures would be hard/impossible to
store in view state, but that's a different reason from the one that
your words seemed to imply.

My apologies if I misread your words, but I wanted to make sure I wasn't
under false beliefs about what view state is.
 
A

Alan Silver

Well, I doubt that that is what Juan was saying
I wasn't saying that, and I didn't say that.

Good. I just wanted clarification, as your words *seemed* to imply that
(to me at least).

Ta ra ;-)
 
S

Scott Allen

I read through this thread and was confused as to what people thought
could be done and couldn't be done.

As S. Justin Gengo pointed out, you just need a struct that is
serializable.

Take the following struct definition.

[Serializable]
public struct Point
{
public Point(int x, int y)
{
this.x = x;
this.y = y;
}

public int x;
public int y;
}

You could save and retrieve an array of Point in ViewState, e.g:

using System;

namespace OdeToCode.viewstate
{
public class ArrayTest : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
SaveArrayToViewState();
}
else
{
RetrieveArrayFromViewState();
}
}

private void SaveArrayToViewState()
{
Point[] points =
{
new Point(1,2),
new Point(3,4),
new Point(5,6)
};

ViewState["MyPointArray"] = points;
}

private void RetrieveArrayFromViewState()
{
Point[] points = ViewState["MyPointArray"] as Point[];

foreach(Point p in points)
{
Response.Write(p.x.ToString() + ", ");
Response.Write(p.y.ToString() + "<br>");
}
}

// CODEGEN Goo and a postback control here....

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top