GridView problems with viewstate events overriden.

G

Guest

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"
 
T

Teemu Keiski

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)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top