Trouble with sequence of page build.

T

TPS

Instead of using Hyperlinks and URL / Response.QueryString() parameters to
pass state around in my app, I am using LinkButtons with a corresponding
command event and command arguments.

This page has LinkButtons in a menu down the left hand side, that when
clicked causes the main content of the page to change. The main content of
the page has linkbuttons as well. I am having trouble with the linkbuttons
in the maincontent area keeping their associated command event wired
together.

The linkbuttons in the left menu get built in the page load of the initial
load and all subsequent postbacks (these work fine).

When a left menu linkbutton is clicked it causes a postback,

Page_Load()
BuildLeftMenu() is run in page_load

I don't run BuildMainContent() in the page_load because I don't have the new
TopicID yet.

MyLinkButton_Command() fires next, where I capture the command argument
which tells me the TopicID which I set as a session var and then call
BuildMainContent()

BuildMainContent() fires which grabs the TopicID from the Session and builds
the maincontent linkbuttons .

The server returns the page to me and the maincontent linkbuttons exist, but
are not wired to their commandevents. Meaning when I click on them nothing
happens.

***************************************************

Now if I put the BuildMainContent() in the page_load so it builds in the
postback, everything works okay.

My question is, why aren't the commandevents wired to the linkbuttons, when
I have clearly wired them up in the BuildMainContent() method?

Thanks.

TPS.
 
S

Steven Cheng[MSFT]

Hi TPS,

As for the postback issue on the dynamic controls you mentioned, here are
my understandings:

The Controls added in the "BuildMainContent()" method is called in other
controls(built in BuildLeftMenu() in Page_Load)'s post back event ,yes?

That means the maincontent's Linkbutton's postback event handler are wireup
when page processing the postback event rather than before it. So of course
it can 't be fired. And when you move the BuildMainContent() to Page_Load,
their events will be fired.

In fact, as for dynamically created controls, the following two things
should be noticed:
1.Dynamic control should be created everytime page is requested (both the
intial load and the sequential ....). We recommand that we create and add
them in page's OnInit .(in Page_Load is also ok)

2. If the control need to register postback event handler, the handler
should be wireup before the page processing the postback event.
Generally in Page's Init or Load event.

And here is good tech article which has mentioned the things above. I
believe it'll help on undertanding them:

#Understanding ASP.NET View State
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html
/viewstate.asp


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

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
T

TPS

Steven, thanks for your reply.

I have read for hours and still can't seem to answer my question.

My problem is my LinkButton event handler fires "AFTER" the page has been
built. When the event handler fires, that is where I am getting "TopicID"
from the command arguement. I am using the new TopicID to populate my
LinkButtons, but it is too late because the page has already been built!

Ideas?

Thanks,
TPS
 
S

Steven Cheng[MSFT]

Hi TPS,

I've seen for new post discussing on the same problem in this group named
"Can EventHandler fire in Page_PreRender event".

I've posted my reply in the thread. I'd appreciate if you have a look
there.
In addition, if you feel it convenient that we continue to discussing in
one of the two threads, please feel free to followup in either one. Thanks.

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

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

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

Top