Save a state of a Custom Object
-
i really need help in that, i developed a custom control that display data from a datasource, its a data-bound control, the problem is how can i save the dataset that i provide in the the DataSource property so i can use it when the user ask for DataBind( ) function, the dataset i use is a custom object that have a complex structure from inside and can't save the values inside independintly, i should save the whole dataset. please if any have a suggestion tell me. thx
-
i really need help in that, i developed a custom control that display data from a datasource, its a data-bound control, the problem is how can i save the dataset that i provide in the the DataSource property so i can use it when the user ask for DataBind( ) function, the dataset i use is a custom object that have a complex structure from inside and can't save the values inside independintly, i should save the whole dataset. please if any have a suggestion tell me. thx
I'm a little confused when you say "I should save the whole dataset". A control with a
DataSource
property can be bound to a number of types of objects, with aDataSet
being one. You mention you are binding to your own custom object, which is fine too. It sounds to me that you want to re-bind your custom object to theDataSource
property of the control, perhaps on a postback, and are looking for how you may persist your custom object across page requests? Is that right? If so - here are some thoughts. If you don't mind using the applicationCache
or theSession
object, you can persist your custom object in either. Here's what it could look like to use the Session object:Session["MyCustomObject"] = (object) myCustomObject
and to retrieve the value...
myCustomObject = (myCustomType) Session["MyCustomObject"]
If you are interested in more permanent persistence, you could use serialization to output your custom object's properties to an XML file. Try a google search for "XML serialization C#" -- or something like that. There are lots of good examples and ideas on how to do this out there. I apologize if I've missed the point of your question.
-
I'm a little confused when you say "I should save the whole dataset". A control with a
DataSource
property can be bound to a number of types of objects, with aDataSet
being one. You mention you are binding to your own custom object, which is fine too. It sounds to me that you want to re-bind your custom object to theDataSource
property of the control, perhaps on a postback, and are looking for how you may persist your custom object across page requests? Is that right? If so - here are some thoughts. If you don't mind using the applicationCache
or theSession
object, you can persist your custom object in either. Here's what it could look like to use the Session object:Session["MyCustomObject"] = (object) myCustomObject
and to retrieve the value...
myCustomObject = (myCustomType) Session["MyCustomObject"]
If you are interested in more permanent persistence, you could use serialization to output your custom object's properties to an XML file. Try a google search for "XML serialization C#" -- or something like that. There are lots of good examples and ideas on how to do this out there. I apologize if I've missed the point of your question.
thanks for your help, but this way is only available if i want to store the dataset in the page, but what i want is to store it inside the control that i developed, where there is no Session only ViewState and i can't transform my datasource to a strings to save it in the ViewState, thats what i mean: myCustomControl.DataSource = CustomDataSet ; this Custom DataSet has a complex structure and the CustomControl should know how ti fill it self from this Dataset. thats what i mean thanks for any suggestion.:(
-
thanks for your help, but this way is only available if i want to store the dataset in the page, but what i want is to store it inside the control that i developed, where there is no Session only ViewState and i can't transform my datasource to a strings to save it in the ViewState, thats what i mean: myCustomControl.DataSource = CustomDataSet ; this Custom DataSet has a complex structure and the CustomControl should know how ti fill it self from this Dataset. thats what i mean thanks for any suggestion.:(
Hi there. I think you may have answered your own question in a way. The coding that would persist from postback to postback a value from a custom control would exist in the custom control itself. Another way of putting it: it is the responsibility of the custom control author to persist his/her custom control's properties/values. Often in a data-bindable control, the custom control will override the CreateChildControls() method with one that re-creates child controls and sets their values according to what the control has saved in ViewState. There are some good examples you can google (or look here on CodeProject) that shows how to do this and how to use ViewState within a custom control to handle the saving and loading of a control's state. Something else to consider: a data-bindable control's DataSource property will often be coded to accept a generic object type provided the object implements a specific interface (like the IList or IEnumerable interface). If your custom control's DataSource is only meant to work with your CustomDataSet object, then you may help yourself by marking the custom object [Serializable] or by developing a custom TypeConverter for it (to convert its values to/from a string type). That could simplify the custom control's serialization of its data source to its ViewState.
-
Hi there. I think you may have answered your own question in a way. The coding that would persist from postback to postback a value from a custom control would exist in the custom control itself. Another way of putting it: it is the responsibility of the custom control author to persist his/her custom control's properties/values. Often in a data-bindable control, the custom control will override the CreateChildControls() method with one that re-creates child controls and sets their values according to what the control has saved in ViewState. There are some good examples you can google (or look here on CodeProject) that shows how to do this and how to use ViewState within a custom control to handle the saving and loading of a control's state. Something else to consider: a data-bindable control's DataSource property will often be coded to accept a generic object type provided the object implements a specific interface (like the IList or IEnumerable interface). If your custom control's DataSource is only meant to work with your CustomDataSet object, then you may help yourself by marking the custom object [Serializable] or by developing a custom TypeConverter for it (to convert its values to/from a string type). That could simplify the custom control's serialization of its data source to its ViewState.
thanks for this, i wanted to check if there is another way to save the Custom DataSet, the problem is that my DataSet is comes form unmanaged code, anyway thanks again i'll try some other methodology to do this.
-
thanks for this, i wanted to check if there is another way to save the Custom DataSet, the problem is that my DataSet is comes form unmanaged code, anyway thanks again i'll try some other methodology to do this.
Hi there. There may be one more aspect to this to think about... is your CustomDataSet object a subclass of
DataSet
? If so, you may want to consider using theDataSet
.WriteXml
() method... perhaps writing out to aStringWriter
object; the resulting string might then be persisted in the custom control'sViewState
. Just a thought. -
Hi there. There may be one more aspect to this to think about... is your CustomDataSet object a subclass of
DataSet
? If so, you may want to consider using theDataSet
.WriteXml
() method... perhaps writing out to aStringWriter
object; the resulting string might then be persisted in the custom control'sViewState
. Just a thought.even this one doesn't work, since its not, its from unmanaged code that is wraped to work with managed code, anyway i think its ok if save it in the Session outside the object and whenever the Control need it again i'll give it to it, isn't this the way that the Normal Datagrid works or i'm wrong? i mean don't u have to assign the Dataset to the Datagrid when u want to handle the sorting event or anyother? if that is true then its normal to save my Custom Dataset outside the Control. thanks again