473,703 Members | 2,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Contr ols.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.Contro ls.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(objec t sender, System.EventArg s e)

{

// Put user code to initialize the page here

if (!Page.IsPostBa ck)

{

ArrayList ar = new ArrayList();

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

{

ar.Add(i);

}

Repeater1.DataS ource = ar;

this.DataBind() ;

}

}





override protected void OnInit(EventArg s e)

{

//

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

//

InitializeCompo nent();

base.OnInit(e);





// <<<demo demo demo

bool bDemoLoadViewst ateFailedInRepe ater = false;

bDemoLoadViewst ateFailedInRepe ater = true;

if (bDemoLoadViews tateFailedInRep eater)

{

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

int i = Repeater1.Contr ols.Count ;

}

// demo demo demo>>>

}


Nov 18 '05 #1
8 4278
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" , "LoadPostDa ta", "Load",
"RaisePostDataC hangedEvent", "RaisePostBackE vent", "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?fr ame=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?fra me=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 misunderstandin g 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 programmaticall y, 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.m icrosoft.com> wrote in message
news:0B******** ******@cpmsftng xa07.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" , "LoadPostDa ta", "Load",
"RaisePostDataC hangedEvent", "RaisePostBackE vent", "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?fr ame=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?fra me=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.Contro ls.Count. The function I wrote exhausting the controls tree
by traversing all the child controls recursively by a given control, and
creating literal controls programmaticall y. 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
"Invalidlastnam e" <in************ *@toomanyvalida tions.page> wrote in message
news:un******** ******@TK2MSFTN GP10.phx.gbl...
Steven,
I think you are misunderstandin g 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 programmaticall y, 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.m icrosoft.com> wrote in message
news:0B******** ******@cpmsftng xa07.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" , "LoadPostDa ta", "Load",
"RaisePostDataC hangedEvent", "RaisePostBackE vent", "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?fr ame=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?fra me=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.Co unt" 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.Co unt" 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 programmaticall y 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
programmaticall y, 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.m icrosoft.com> wrote in message
news:5J******** ******@cpmsftng xa07.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.Co unt" 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.Co unt" 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
21644
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 the results in the grid. Say you filter the grid for records that have a certain condition set to "NO" (in this case a checkbox). In this scenario the search returns one result. If I then check the checkbox ("YES") and save it, I now get my message...
0
2112
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
1509
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 Postback. Please try the following test, and let me know if you can reproduce this: - Create a custom DataGrid or Repeater class that does nothing but derive from one of the above and gives a place to put breakpoints in overriden handlers - Create...
0
6167
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 dynamically (something I need to do for my application). The problem is that when I click on the button in the repeater the ItemCommand event does not seem to be fired. Can anyone tell me what I am doing wrong? //////////////WebForm1.aspx: <%@ Page...
3
3312
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 with the Products that were ordered on the ItemCreated event of the repeater i set a property of an options control within the repeater. The Options control sets a DropDownList and then sets a SelectedValue. The Selected Value prop is correct...
1
4345
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 objects and wish to display the user control multiple times. On this web page I simply bind the repeater to the data source and in the ItemDataBound event dynamically load the user control via "LoadControl()". The user control contains an auto post...
0
2565
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 link (1,2,3 so on). The code can be found in SubscriptionCart.aspx.cs. Default.aspx ------------
0
1168
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 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">
2
11001
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 they can be different user controls for each timesheet type.
0
9251
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6588
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5922
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4432
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3123
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2450
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2069
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.