overriden viewstate events and gridview

G

Guest

Hi,
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top