Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serializing a property

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
US
I have a GridViewRow that I am trying to serialize because I am taking the data from the row to use in a couple of other rows and I want the row to be accessible on postback.

I tried this but it doesn't work:

Code:
        [Serializable]
        public GridViewRow GVR
        {
            get
            {
                return (GridViewRow)ViewState["GVR"];
            }
            set
            {
                //if (value == string.Empty) value = "7";
                ViewState["GVR"] = value;
            }
        }

But the error I get is:

Attribute 'Serializable' is not valid on this declaration type. It is only valid on 'class, struct, enum, delegate' declarations.

How can I make a GridViewRow property serializable?

Thanks,

Tom
 
I don't see any reason that you need to serialize the data. Why do you think you need to?
Also, the error is telling you that that attribute is only valid on those types of objects
 
Viewstate needs it to be serialized or you get an error.

I want to use a property and move it to and from the viewstate so it is there when I do a postback.

Thanks,

Tom
 
If I don't have the serialization line, I get the following:

System.Runtime.Serialization.SerializationException: Type 'System.Web.UI.WebControls.GridViewRow' in Assembly 'System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.
 
From the research I have done, it seems you can't serialize this type of object.
All responses seem to be to have a property for each cell in the gvr.
If you change it to a list of GVRs you may be able to serialize that, but I haven't tried that yet.

Can you explain in detail what exactly you are trying to do? there may be a better way to accomplish what you want to do than trying this
 
The reason I do this is because if I press a button on my page, an event fires that allows you to get the information from the row the button was on.

But then I use the fields in the row for the ODS to use to get data from the database and fill a detail grid. But when the ODS is setting up the call to the stored procedure (ODS_ObjectCreteated) any private variables are gone so I usually do:

public string ETA
{
get
{
return (string)ViewState["ETA"];
}
set
{
ViewState["ETA"] = value;
}
}

But a GridViewRow needs to be serialized somehow. What I did to solve the issue was to get each piece of data I needed (such as the ETA) and stored those in the ViewState. I was hoping not to have to do that by storing the Gridviewrow and just get the data I needed later.

Thanks,

Tom
 
2 things here.
First a quick fix to your problem is you can use Session instead of viewstate. Kind of overkill but it works.

2. I would not get a gridview row's data to get other data. You should be using the DataKeys property of the gridview. Here you can specify a key or keys of that datatable. So, for example, you can say
columnname "ID" is your key value. On the select of that row, you can get that datakey's value and pass it to your query. Not sure of the structure of your table(s) but I think that is the best way to go.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top