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

I

Invalidlastname

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

}
 
S

Steven Cheng[MSFT]

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-us/vbcon/html/vbconWebFormsPageProcessi
ngStages.asp?frame=true

#Page Object Model
http://msdn.microsoft.com/library/en-us/dnaspp/html/aspnet-pageobjectmodel.a
sp?frame=true

#ASP.NET Server Controls' Life Cycle
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcontrolexecutionli
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.)
 
I

Invalidlastname

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
 
I

Invalidlastname

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
 
S

Steven Cheng[MSFT]

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.)
 
I

Invalidlastname

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
 
S

Steven Cheng[MSFT]

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.)
 
S

Steven Cheng[MSFT]

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.)
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top