Last post Feb 05, 2011 12:17 PM by mbanavige
Member
20 Points
16 Posts
Feb 05, 2011 11:15 AM|rohit.barde|LINK
Hi,
I am having tough time finding out this reason. I have a web control where i add some variable in viewstate. like viewstate["test"] = "1".
when i post back this viewstate no more exists in collection. any idea? here is my code
public class mycontrol:WebControl { public mycontrol() { // // TODO: Add constructor logic here // } protected override void OnInit(EventArgs e) { Page.RegisterViewStateHandler(); //this.TrackViewState(); //Page.RegisterRequiresControlState(this); //ViewState["initstate"] = "1"; //ViewState.Add("tedt", "tt"); //this.EnableViewState = true; base.OnInit(e); } protected override void OnPreRender(EventArgs e) { ViewState["renderstate"] = "1"; //SaveViewState(); base.OnPreRender(e); } protected override object SaveControlState() { //return base.SaveControlState(); } }
All-Star
157606 Points
13143 Posts
ASPInsiders
Moderator
Feb 05, 2011 12:17 PM|mbanavige|LINK
if you override a method but dont call the base method, you can break functionality. check you override of Save ControlState for example.
But there should be no need to add all the overrides you've added, you can read/write to viewstate directly
I often like to wrap up the viewstate access in a property.
public int SomeInteger {
get {
object o = ViewState["SomeInteger"];
if (o != null) return (int)o;
return 0;
//a default
}
set { ViewState["SomeInteger"] = value; }
http://wiki.asp.net/page.aspx/58/viewstate/
Member
20 Points
16 Posts
webcontrol loosing viewstate information during postbacks
Feb 05, 2011 11:15 AM|rohit.barde|LINK
Hi,
I am having tough time finding out this reason. I have a web control where i add some variable in viewstate. like viewstate["test"] = "1".
when i post back this viewstate no more exists in collection. any idea? here is my code
public class mycontrol:WebControl { public mycontrol() { // // TODO: Add constructor logic here // } protected override void OnInit(EventArgs e) { Page.RegisterViewStateHandler(); //this.TrackViewState(); //Page.RegisterRequiresControlState(this); //ViewState["initstate"] = "1"; //ViewState.Add("tedt", "tt"); //this.EnableViewState = true; base.OnInit(e); } protected override void OnPreRender(EventArgs e) { ViewState["renderstate"] = "1"; //SaveViewState(); base.OnPreRender(e); } protected override object SaveControlState() { //return base.SaveControlState(); } }
<div></div>
All-Star
157606 Points
13143 Posts
ASPInsiders
Moderator
Re: webcontrol loosing viewstate information during postbacks
Feb 05, 2011 12:17 PM|mbanavige|LINK
if you override a method but dont call the base method, you can break functionality. check you override of Save ControlState for example.
But there should be no need to add all the overrides you've added, you can read/write to viewstate directly
I often like to wrap up the viewstate access in a property.
public int SomeInteger {
get {
object o = ViewState["SomeInteger"];
if (o != null) return (int)o;
return 0;
//a default
}
set { ViewState["SomeInteger"] = value; }
}
http://wiki.asp.net/page.aspx/58/viewstate/