Can we pass object to the Viewstate?

Posted by Laghaterohan under Regular Expressions on 3/14/2011 | Points: 10 | Views : 12407 | Status : [Member] | Replies : 12
Hello,

Can we pass a object to the ViewState? If yes how? and which type of variable?


Thanks!

Best Regards,
Rohan Laghate



Responses

Posted by: T.saravanan on: 3/14/2011 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

ViewState is Generic Data Type.So we can store different types of objects in ViewState.
For ex: I have a data in DataTable...
DataTable dtData =new DataTable();

// Store value into ViewState
ViewState["Data"] = dtData;
// Get value from ViewState
DataTable dtResult = (DataTable)ViewState["Data"];

Even we can store class object also in ViewState (Using Properties).

Note: We cannot store maximum size of data in ViewState object.

For more info...http://msdn.microsoft.com/en-us/library/ms972976.aspx


Thanks,
T.Saravanan

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Laghaterohan on: 3/14/2011 [Member] Starter | Points: 25

Up
0
Down
Ok! thanks but ...i heard something like we can save object if it is marked as Serializable...? what is this excatly? am confused...when is the object serialized?

Best Regards,
Rohan Laghate

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: T.saravanan on: 3/14/2011 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,

Know about Serialization in ViewState...
http://www.beansoftware.com/ASP.NET-Tutorials/ViewState-In-ASP.NET.aspx
http://forums.asp.net/p/1362572/2818497.aspx

I think its helpful to you..

Thanks,
T.Saravanan

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gsolvers on: 3/14/2011 [Member] Starter | Points: 25

Up
0
Down
Really Cool Article! Thanks Saravanan!

Best Regards,

VG
www.TeacherJi.com

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Laghaterohan on: 3/14/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks!

Best Regards,
Rohan Laghate

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 3/15/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Actually we can use any object as well in view state but best practice is not to use since the data size is huge... For that its better to go with session for large data.

Thanks
Karthik
www.f5Debug.net

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 3/15/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Actually we can use any object as well in view state but best practice is not to use since the data size is huge... For that its better to go with session for large data.

Thanks
Karthik
www.f5Debug.net

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 9/19/2012 [Member] Bronze | Points: 25

Up
0
Down
Yes,we can pass object to the viewstate

[Serializable]
public class Student
{
public string firstName;
public string lastName;

public Student(string fName, string lName)
{
firstName = fName;
lastName = lName;
}
}


Because the Student class is marked as serializable, it can be stored in view state:
// Storing a student in view state.
Student stud = new Student("John", "Doe");
ViewState["CurrentStudent"] = stud;
Remember, when using custom objects, you'll need to cast your data when you retrieve it from view state.
// Retrieve a student from view state.
Student stud = (Student) ViewState["CurrentStudent"];

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 9/19/2012 [Member] Bronze | Points: 25

Up
0
Down
Please mark as answer if it helpful to you.. That helps others who search the same..

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 9/20/2012 [Member] Starter | Points: 25

Up
0
Down
Good Explanation.

Thanks & Regards
Hari

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Muhsinathk on: 9/21/2012 [Member] Bronze | Points: 25

Up
0
Down
Thanks..

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hariinakoti on: 9/21/2012 [Member] Starter | Points: 25

Up
0
Down
welcome

Thanks & Regards
Hari

Laghaterohan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response