473,387 Members | 1,440 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,387 software developers and data experts.

Repeater failed to load the viewstate when Controls.Count was called in OnInit

Hi,
We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue which is related to the repeater. In the code shown below, if I call Repeater1.Controls.Count in the OnInit (the code fragment was highlighted in yellow) , the viewstate for the repeater will be lost during the postback. You can re-produce this behavior by copying and pasting the code I provided, then you will know what is my issue.
BTW
The reason I call the Repeater.Controls.Count is because I have a generic function will recursively find the control I need and create a literal control for it. Currently I resolve this problem by excluding the repeater control, by checking the type of the control, in recursive routine.

ILN
--------------------------------------------------------------------------------

private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

if (!Page.IsPostBack)

{

ArrayList ar = new ArrayList();

for (int i=0;i< 5;i++)

{

ar.Add(i);

}

Repeater1.DataSource = ar;

this.DataBind();

}

}





override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);





// <<<demo demo demo

bool bDemoLoadViewstateFailedInRepeater = false;

bDemoLoadViewstateFailedInRepeater = true;

if (bDemoLoadViewstateFailedInRepeater)

{

// the following line will cause repeater failed to load the viewstate

int i = Repeater1.Controls.Count ;

}

// demo demo demo>>>

}


Nov 18 '05 #1
8 4255
Hi Invalidlastname,
Thank you for using Microsoft Newsgroup Service. Based on your description,
you're dealing with an ASP.NET web application. In one page, you add some
code to retrieve a Repeater control's "Controls.Count" member and
encountered the error which indicates the ViewState isn't avaliable. Please
correct me if my understanding of your problem is not quite exact.

As for the situation you described, I think it is because the page's
viewstate hasn't been loaded when the Page's "Init" event is fired. In
ASP.NET the Page class has serveral buildin events, they are:
"Init", "LoadViewState", "LoadPostData", "Load",
"RaisePostDataChangedEvent", "RaisePostBackEvent", "PreRender",
"SaveViewState", "Render" , "Unload",
Also, their called sequence is as above. So in the Page's init event, the
page's ViewState hasn't been loaded yet, also its whole structure(such as
sub controls hierarchy) hasn't been constructed completely. If you try
accessing the viewstate at that time, you'll find it invalid. So I suggest
that you try implementing the operation in the Page's "Load" event.
For the detailed information on the ASP.NET page's event model and object
model and lifecycle(the same as a web server control), here is some tech
articles in MSDN:

#Page Life Cycle
http://msdn.microsoft.com/library/en...msPageProcessi
ngStages.asp?frame=true

#Page Object Model
http://msdn.microsoft.com/library/en...eobjectmodel.a
sp?frame=true

#ASP.NET Server Controls' Life Cycle
http://msdn.microsoft.com/library/en...rolexecutionli
fecycle.asp?frame=true
Please check out the preceding suggestions. If you have any questions,
please feel free to post here.
Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Nov 18 '05 #2
Steven,
I think you are misunderstanding my question. I AM NOT TRYING TO ACCESS THE
VIEWSTATE in OnInit EVENT.

What I observed was if a repeater Controls.Count was called before the
loadviewstate, in my case will be in OnInit, then that repeater won't be
able to restore it's items from the viewstate. The postback page will render
an empty repeater since there is no repeater items.

In my attached example, I used a repeater to bind an array, and the
databinding was called when page is not postback. I have an asp button with
server-side click event. When the button was click, if the repeater
Controls.Count was called in the OnInit, then the page gets a empty
repeater, which means the repeater's viewstate was lost during the
loadviewstate. If you remove the line of calling repeater's Controls.Count
in the Page.OnInit, then repeater's viewstate will be restored and rendered
properly.

PLEASE run the attached example yourself, or create your own testing page,
just see the difference if you call Repeater Controls.Count in the OnInit.
This is a very simple example, which only demonstrate one thing: "the
repeater can not be rendered properly in the postback page if Controls.Count
was called in the OnInit"

As I explained in my previous post, I am not trying to access viewstate in
OnInit. The reason I called the Controls.Count is because I add certain
literal controls programmatically, and I need to reconstruct the control
tree before the loadviewstate was called. That's the reason I have my
method, which relies on the value of Controls.Count, called in the OnInit,
to make sure it occurred before the loadviewstate.

Thanks,

ILN


"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:0B**************@cpmsftngxa07.phx.gbl...
Hi Invalidlastname,
Thank you for using Microsoft Newsgroup Service. Based on your description, you're dealing with an ASP.NET web application. In one page, you add some
code to retrieve a Repeater control's "Controls.Count" member and
encountered the error which indicates the ViewState isn't avaliable. Please correct me if my understanding of your problem is not quite exact.

As for the situation you described, I think it is because the page's
viewstate hasn't been loaded when the Page's "Init" event is fired. In
ASP.NET the Page class has serveral buildin events, they are:
"Init", "LoadViewState", "LoadPostData", "Load",
"RaisePostDataChangedEvent", "RaisePostBackEvent", "PreRender",
"SaveViewState", "Render" , "Unload",
Also, their called sequence is as above. So in the Page's init event, the
page's ViewState hasn't been loaded yet, also its whole structure(such as
sub controls hierarchy) hasn't been constructed completely. If you try
accessing the viewstate at that time, you'll find it invalid. So I suggest
that you try implementing the operation in the Page's "Load" event.
For the detailed information on the ASP.NET page's event model and object
model and lifecycle(the same as a web server control), here is some tech
articles in MSDN:

#Page Life Cycle
http://msdn.microsoft.com/library/en...msPageProcessi ngStages.asp?frame=true

#Page Object Model
http://msdn.microsoft.com/library/en...eobjectmodel.a sp?frame=true

#ASP.NET Server Controls' Life Cycle
http://msdn.microsoft.com/library/en...rolexecutionli fecycle.asp?frame=true
Please check out the preceding suggestions. If you have any questions,
please feel free to post here.
Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



Nov 18 '05 #3
One more thing I'd like to clarify. I really don't care the value of
Repeater.Controls.Count. The function I wrote exhausting the controls tree
by traversing all the child controls recursively by a given control, and
creating literal controls programmatically. I need Controls.Count for
finding child controls inside the placeholder, panel and user control, but
not repeater. If a repeater is a part of the control tree of the control I
passed into the my function, that repeater's viewstate will be gone during
the postback, which doesn't make any sense to me.

ILN
"Invalidlastname" <in*************@toomanyvalidations.page> wrote in message
news:un**************@TK2MSFTNGP10.phx.gbl...
Steven,
I think you are misunderstanding my question. I AM NOT TRYING TO ACCESS THE VIEWSTATE in OnInit EVENT.

What I observed was if a repeater Controls.Count was called before the
loadviewstate, in my case will be in OnInit, then that repeater won't be
able to restore it's items from the viewstate. The postback page will render an empty repeater since there is no repeater items.

In my attached example, I used a repeater to bind an array, and the
databinding was called when page is not postback. I have an asp button with server-side click event. When the button was click, if the repeater
Controls.Count was called in the OnInit, then the page gets a empty
repeater, which means the repeater's viewstate was lost during the
loadviewstate. If you remove the line of calling repeater's Controls.Count
in the Page.OnInit, then repeater's viewstate will be restored and rendered properly.

PLEASE run the attached example yourself, or create your own testing page,
just see the difference if you call Repeater Controls.Count in the OnInit.
This is a very simple example, which only demonstrate one thing: "the
repeater can not be rendered properly in the postback page if Controls.Count was called in the OnInit"

As I explained in my previous post, I am not trying to access viewstate in
OnInit. The reason I called the Controls.Count is because I add certain
literal controls programmatically, and I need to reconstruct the control
tree before the loadviewstate was called. That's the reason I have my
method, which relies on the value of Controls.Count, called in the OnInit,
to make sure it occurred before the loadviewstate.

Thanks,

ILN


"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:0B**************@cpmsftngxa07.phx.gbl...
Hi Invalidlastname,
Thank you for using Microsoft Newsgroup Service. Based on your

description,
you're dealing with an ASP.NET web application. In one page, you add some code to retrieve a Repeater control's "Controls.Count" member and
encountered the error which indicates the ViewState isn't avaliable.

Please
correct me if my understanding of your problem is not quite exact.

As for the situation you described, I think it is because the page's
viewstate hasn't been loaded when the Page's "Init" event is fired. In
ASP.NET the Page class has serveral buildin events, they are:
"Init", "LoadViewState", "LoadPostData", "Load",
"RaisePostDataChangedEvent", "RaisePostBackEvent", "PreRender",
"SaveViewState", "Render" , "Unload",
Also, their called sequence is as above. So in the Page's init event, the page's ViewState hasn't been loaded yet, also its whole structure(such as sub controls hierarchy) hasn't been constructed completely. If you try
accessing the viewstate at that time, you'll find it invalid. So I suggest that you try implementing the operation in the Page's "Load" event.
For the detailed information on the ASP.NET page's event model and object model and lifecycle(the same as a web server control), here is some tech
articles in MSDN:

#Page Life Cycle

http://msdn.microsoft.com/library/en...msPageProcessi
ngStages.asp?frame=true

#Page Object Model

http://msdn.microsoft.com/library/en...eobjectmodel.a
sp?frame=true

#ASP.NET Server Controls' Life Cycle

http://msdn.microsoft.com/library/en...rolexecutionli
fecycle.asp?frame=true
Please check out the preceding suggestions. If you have any questions,
please feel free to post here.
Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Nov 18 '05 #4
Hi Invalidlastname,
Thanks for your response and correcting my mistakes on understanding your
problem. This time I'd like to confirm my current understanding of your
problem:

You've an ASP.NET web application, in one of the page, you're to loop
through a container control(such as placeholder or UserControl) and its sub
controls so as to do some programmatical operations on them. You put the
loop operation in the Page's OnInit event handler now. When loop through
the control's sub control, you need to access their "Controls.Count"
member, however, you found that if some certain control(such as Repeater)'s
"Control.Count" is accessed in the OnInit, their ViewState can't be loaded
correctly then. Right?

I've run the page sample you provided and also done some further tests.
Yes, I did encountered the problem you mentioned. The repeater seems will
lose its viewstate if ever accessed its "Control.Count" member. In fact I
found that not only the Repeater control but those Template-Databind
controls have this issue. I also found it on the DataList. I think it
maybe due to some internal mechanism of the Template-Databind controls when
they creating the Control hierarchy from the viewstate. But currently, I
haven't found any document which clearly point out this. BTW, do you think
it possible we use some other means to workaround it? For example, since
you loop through the Controls collection, then when you get a certain
control in the collection, you may first check its Class Type, and access
its Controls.Count member only if it's the control you want. Or on the
contrary, not to access those certain controls(such as Repeater or
DataList) 's "Controls.Count" member.
Also, since in the page's init event, the entire Page control structure
hasn't been completely constructed. Some of the child controls or their
child controls haven't been constructed via viewstate. Would you consider
change the operation of looping through the control hierarchy to another
place such as Page's Load event or..? If you don't think it suitable to do
that, would you tell me the certain reason or some more detailed
information on the problem you'll encounter? Please check out the above
suggestions and feel free to post here if you need any assistant.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #5
Hi Steven,
I really appreciate you spending time to help me resolve this issue. I have
always found you very insightful and educational =)

The workaround you provided by excluding certain types of control for
accessing the count has already been implemented and works very well. A
quick explanation of why I need a such function which loops through the sub
controls. In our application, each data entry form will have a editable
version and read-only version. These two versions share the same page
format, page layout, but in the read-only mode, each field will be shown as
static text instead of an editable form field object. We don't want the
developers to spend time create two versions of the same data entry form, so
we created a framework to programmatically render a read-only version form
based on editable data entry form at run-time.

One of the functions, which recursively loop through the sub controls, is to
add literal controls for every editable form object, e.g. textbox, radio
button controls. Since I added the literal controls into control tree
programmatically, so if I move the function to Load event, the ASP.NET will
complain about the mismatches between the viewstate and control tree during
the page post back. For this reason, I have to call my function before the
Loadviewstate event, and best place I found is OnInit.

We have spent many hours to figure out why the repeater was not rendered
properly after the postback. The finding surprised everyone of us. I hope
this undocumented internal mechanism can be fixed in the future service pack
or release of ASP.NET

Rgds,

ILN

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:5J**************@cpmsftngxa07.phx.gbl...
Hi Invalidlastname,
Thanks for your response and correcting my mistakes on understanding your
problem. This time I'd like to confirm my current understanding of your
problem:

You've an ASP.NET web application, in one of the page, you're to loop
through a container control(such as placeholder or UserControl) and its sub controls so as to do some programmatical operations on them. You put the
loop operation in the Page's OnInit event handler now. When loop through
the control's sub control, you need to access their "Controls.Count"
member, however, you found that if some certain control(such as Repeater)'s "Control.Count" is accessed in the OnInit, their ViewState can't be loaded
correctly then. Right?

I've run the page sample you provided and also done some further tests.
Yes, I did encountered the problem you mentioned. The repeater seems will
lose its viewstate if ever accessed its "Control.Count" member. In fact I
found that not only the Repeater control but those Template-Databind
controls have this issue. I also found it on the DataList. I think it
maybe due to some internal mechanism of the Template-Databind controls when they creating the Control hierarchy from the viewstate. But currently, I
haven't found any document which clearly point out this. BTW, do you think
it possible we use some other means to workaround it? For example, since
you loop through the Controls collection, then when you get a certain
control in the collection, you may first check its Class Type, and access
its Controls.Count member only if it's the control you want. Or on the
contrary, not to access those certain controls(such as Repeater or
DataList) 's "Controls.Count" member.
Also, since in the page's init event, the entire Page control structure
hasn't been completely constructed. Some of the child controls or their
child controls haven't been constructed via viewstate. Would you consider
change the operation of looping through the control hierarchy to another
place such as Page's Load event or..? If you don't think it suitable to do that, would you tell me the certain reason or some more detailed
information on the problem you'll encounter? Please check out the above
suggestions and feel free to post here if you need any assistant.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #6
Hi Invalidlastname,

I'm glad that we've resolved this issue. Also, thank you for your patience
and cooperation. If you have any question or concerns, please feel free to
post it in the group. I am standing by to be of assistance.

Best regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #7
Hi Invalidlastname,

I'm glad that we've resolved this issue. Also, thank you for your patience
and cooperation. If you have any question or concerns, please feel free to
post it in the group. I am standing by to be of assistance.

Best regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #8
Hi,

As for this issue, I have reported it to the production team. Hope it'll
be fixed soon. In the meantime, I'd apologize for any inconvenience it' has
brought to you. Thanks again for choosing Microsoft Tech Support Service!
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #9

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

Similar topics

9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
0
by: John Crowley | last post by:
I'm having an odd problem with viewstate and a dynamically created control inside a repeater template. Basically, I have a repeater setup like this in the aspx:
0
by: Ferret | last post by:
I've found what seems to be a nasty bug in the DataGrid and Repeater classes. If CreateChildControls gets called before LoadViewState, ViewState fails to map and you end up with nothing on a...
0
by: Michael | last post by:
I've seen several posts relating to this problem, and tried EnableViewState="False", but it does not work for me. A twist in my problem is that I am adding the templates for the repeater...
3
by: Ben Dewey | last post by:
Hey everyone, I have a wierd issue i can't seem to find out whats going on. I have a Control for a Shopping Cart Merchant Page called OrderStatus.ascx Inside that control there is a Repeater...
1
by: Dave A | last post by:
I have a problem that I have boiled down to a very simple example. I have a user control that displays a some data from a business object. On one screen I have a collection of these business...
0
by: Eugene Anthony | last post by:
The problem with my coding is that despite removing the records stored in the array list, the rptPages repeater control is still visible. The rptPages repeater control displayes the navigation...
0
by: Eugene Anthony | last post by:
BlogListingAll.aspx ------------------- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="user_BlogListingAll.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML...
2
by: Rotsey | last post by:
Hi, I have a web site that uses user controls to display different formats of timesheets. I have a browse page that has prev/next buttons to browse through a set of timesheets. As I say...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.