473,385 Members | 1,317 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

GridView problems with viewstate events overriden.

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"
May 10 '06 #1
1 2747
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" <ch********@discussions.microsoft.com> wrote in message
news:A5**********************************@microsof t.com...
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"

May 10 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John_H | last post by:
Re: ASP.NET 2.0 I would like suggestions or code examples on how to collect a variable length list of input data (item# & item quantity specifically). I thought that I could accomplish this...
9
by: J055 | last post by:
Hi I have a very simple configuration of the GridView with paging and sorting. When I do a postback the DataBinding event fires twice - in both the ProcessPostData and PreRender stages of the...
0
by: cheloman12 | last post by:
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...
1
by: Giovanni | last post by:
Dear Friends/Gurus, I have exhausted myself and have yet no solution to the following: I have an ASP.NET 2.0 Survey type application. On a page, I have placed a GridView which is bound to an...
13
by: AG | last post by:
I have a gridview that I bind to a List(of Type) at runtime. Not using a datasource control. The gridview has a template column with an imagebutton whose commandname is set to 'Delete'. The...
0
by: jobo | last post by:
Hey there, I'm having a problem getting sorting to work. Here's what the GridView looks like: "server" ID="updt1" Mode="Conditional">
3
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
I have a question that maybe somebody can help me out. I have a gridview that is bound to a sqltable, and I have created two template columns. I am having problems getting the sorting to work....
2
by: Tim Royal | last post by:
I'm using a GridView inside a Wizard Control with a textbox and two listboxes in the footer. I populate these listboxes from the database if the page isn't on postback (and the textbox is left...
3
by: Peter | last post by:
I have a GridView which is populated by List<ofObjects> Does anyone have example of how to sort the columns of this GridView? I have found examples without DataSourceControl but these use...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.