![]() |
|
|
|||||||
![]() |
ASP Net - GridView problems with viewstate events overriden. |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
Hi,
I posted this problem some days ago. I repeat it because it was no answered. If description is confused, please make me know and i'll try to explain the case better. Thanks a lot, Marcelo "I’m implementing SavePageStateToPersistenceMedium and LoadPageStateFromPersistenceMedium in asp.net 2.0 to increment performance avoiding sending viewstate of pages to browser. The code I use is: protected override void SavePageStateToPersistenceMedium(object state) { string str = "VIEWSTATE_" + Request.UserHostAddress + "_" + DateTime.Now.Ticks.ToString(); Cache.Add(str, state, null, DateTime.Now.AddMinutes(Session.Timeout), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null); ClientScript.RegisterHiddenField("__VIEWSTATE_KEY" , str); // this hidden indetifies the unique id which viewstate is saved in server. ClientScript.RegisterHiddenField("__VIEWSTATE", "") } And LoadPageStateFromPersistenceMedium: protected override object LoadPageStateFromPersistenceMedium() { string str = Request.Form["__VIEWSTATE_KEY"]; if (!str.StartsWith("VIEWSTATE_")) { return null; } return Cache[str]; } I have a problem in one page with a usercontrol that contains a gridview bounded to a dataview. This gridview has asigned a DataKeyName in the onInit event of the usercontrol: gvVista.DataSource = mView; string[] sKeyFields = { “keyFieldName” }; gvVista.DataKeyNames = sKeyFields; The problem is that in the SelectedIndexChanged event I can’t catch de ID Selected by User. protected void gvVista_SelectedIndexChanged(object sender, EventArgs e) { int index = Convert.ToInt32(gvVista.SelectedIndex); int iID = Convert.ToInt32(gvVista.DataKeys[index].Value); } This code allways throws the exception “Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index” That because DataKeys is empty. If I comment the viewstate events overriden in the page, the page works fine. Why does the overriden viewstate events “delete” de datakeys value or what I’m doing bad? Thanks a lot. Marcelo" =?Utf-8?B?Y2hlbG9tYW4xMg==?= |
|
|
|
|
#2 |
|
Posts: n/a
|
Hi,
instead of overriding SavePageStatexxx and LoadPageStateFromxxx use this on the Page to override PageStatePersister property protected override PageStatePersister PageStatePersister { get { return new SessionPageStatePersister(this); } } It's built-in ASP.NET and saves the state into Session instead hidden form field (HiddenFieldPageStatePersister, the default) -- Teemu Keiski ASP.NET MVP, AspInsider Finland, EU http://blogs.aspadvice.com/joteke "cheloman12" <> wrote in message news:A5C939FB-79D0-410C-BDF1-... > Hi, > > I posted this problem some days ago. I repeat it because it was no > answered. > If description is confused, please make me know and i'll try to explain > the > case better. > > Thanks a lot, Marcelo > > "I'm implementing SavePageStateToPersistenceMedium and > LoadPageStateFromPersistenceMedium in asp.net 2.0 to increment performance > avoiding sending viewstate of pages to browser. > The code I use is: > protected override void SavePageStateToPersistenceMedium(object state) > { > string str = "VIEWSTATE_" + Request.UserHostAddress + "_" + > DateTime.Now.Ticks.ToString(); > Cache.Add(str, state, null, > DateTime.Now.AddMinutes(Session.Timeout), TimeSpan.Zero, > System.Web.Caching.CacheItemPriority.Normal, null); > ClientScript.RegisterHiddenField("__VIEWSTATE_KEY" , str); > // this hidden indetifies the unique id which viewstate is saved in > server. > ClientScript.RegisterHiddenField("__VIEWSTATE", "") > } > > And LoadPageStateFromPersistenceMedium: > > protected override object LoadPageStateFromPersistenceMedium() > { > string str = Request.Form["__VIEWSTATE_KEY"]; > if (!str.StartsWith("VIEWSTATE_")) > { > return null; > } > return Cache[str]; > } > > I have a problem in one page with a usercontrol that contains a gridview > bounded to a dataview. This gridview has asigned a DataKeyName in the > onInit > event of the usercontrol: > > gvVista.DataSource = mView; > string[] sKeyFields = { "keyFieldName" }; > gvVista.DataKeyNames = sKeyFields; > > The problem is that in the SelectedIndexChanged event I can't catch de ID > Selected by User. > > protected void gvVista_SelectedIndexChanged(object sender, EventArgs e) > { > int index = Convert.ToInt32(gvVista.SelectedIndex); > int iID = Convert.ToInt32(gvVista.DataKeys[index].Value); > } > > This code allways throws the exception > "Index was out of range. Must be non-negative and less than the size of > the > collection. Parameter name: index" > > That because DataKeys is empty. > > If I comment the viewstate events overriden in the page, the page works > fine. > > Why does the overriden viewstate events "delete" de datakeys value or what > I'm doing bad? > > Thanks a lot. > > Marcelo" > > |
|