ViewState got confused between two user controls in same parent ct

G

Guest

I use Visual Basic .NET Framework 1.1, Visual Studio. I have a Web page
(aspx) referencing a control (ascx) that has a placeholder that is assigned
one of 2 child controls through buttons. The ViewState of the second child
control gets confused with the first child control - the first Text property
in the 2nd child gets updated with the first Text property of the 1st child
when the 2nd button is clicked after the 1st.

For instance, click 1st button (display 1st child control), first Text
property is "123456". Then click the 2nd button (display 2nd child control),
first Text property is "View Information" (a button control). Click the 1st
button again, Text property again is "123456" as it should be. Click the 2nd
button, Text property is no longer "View Information", but "123456". This
incorrect assignment occurs between the end of the <button>_Click subroutine
for the 2nd button and before the Page_Load of the 2nd child control.

I fixed the problem by changing the EnableViewState property from True to
False on the "View Information" button control.

I figure .NET is having trouble assigning the values of the two child
controls to the ViewStates when they are both within the same placeholder.

Interesting issue.
 
T

Trevor Benedict R

If you are dynamically adding controls then make sure that they are added in
the exact same sequence. Apart from the I do not understand anything about
how you have implemented it. Some could would be more helpful.

Regards,

Trevor Benedict R
MCSD
 
B

Bruce Barker

on the postback, you have to create a child control of the same type of the
original render.

-- bruce (sqlwork.com)
 
G

Guest

I am dynamically adding controls, but, each control is added depending on
which button is clicked. The buttons can be clicked in any order meaning the
controls are added in any order.

One placeholder is used to house one control and either of the controls can
be added to the placeholder.

Does this information help? Or do you need more?
 
G

Guest

I'm not sure what you mean by that statement.

I declare two different child user controls that are called and loaded in a
placeholder depending on the button that is clicked. Only one control is
loaded based on the button. Also, neither child control is loaded on the
page when the page is first rendered.

Let me know if you need more information.
 
T

Trevor Benedict R

Louise,
Let's assume that you have a place holder called X
you add control A & B.
User types value 1 in A and value 2 in B.
Form gets posted back to the Server
Before you can access these control's values, you must add them to the Place
Holder once again.
Let's assume that you add Control B and then Control A
The values will be swapped in this case because of the names that are
automatically assigned to the control.

Could you do the same test by naming the Controls with specific names and
then adding them in the exact same sequence.

aleternately you could have both the controls in the placeholder and ideally
show only one. See if that works for you.

Regards,

Trevor Benedict R
 
J

jhcorey

This sounds like a timing issue.
If you debug this, you'll notice that the code in page_load for the
page gets executed before the code for the button.
If you start with control 1 loaded, you actually have to load it again
in page_load, and then let button 2 load the other control in order to
keep maintain the proper viewstate.
What you'll end up doing is figuring a strategy to keep track of which
control is loaded so you can re-load it in page_load.

Perhaps another way to say this is that you want everything on the page
to be the same between the time you press a button and the time the
button's code actually executes.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top