Events for Controls added in code

  • Thread starter Dominique Vandensteen
  • Start date
D

Dominique Vandensteen

I have a Table to which I add a LinkButton in the PreRender event
The LinkButton is visible on the webpage but when I click it,
the LinkButton_Click method is not called and the page just "reloads"

anybody an idea what I'm missing here?



Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
Dim addRootLink As New LinkButton
AddHandler addRootLink.Click, AddressOf LinkButton_Click

... add linkbutton to table
End Sub
 
Y

Yunus Emre ALPÖZEN [MCAD.NET]

you should manually set its event handler something like this;

LinkButton lb = new LinkButton();
lb.Click+=new EventHandler(lb_click)
 
B

Brock Allen

PreRender is too late in the page lifecycle to add events such as click and
change event handlers, as that stage has already passed. You should add these
events sometime in or prior to Page_Load.

-Brock
DevelopMentor
http://staff.develop.com/ballen
 
D

Dominique Vandensteen

thx
with some gymnastics I can make this work...

is there a place where I can find the order of the events?
 
B

Brock Allen

is there a place where I can find the order of the events?

The MSDN docs of course have it buired in there somewhere. I've recently
seen a blog post with a graphic of the order. In general they are:

Page and Controls Created
Page_Init
ViewState Loaded
Post data Loaded
Page_Load
Server Side Validation
Any Change Events
Any Click Event
Page_PreRender
Render
Page_Unload
Page is discarded

-Brock
DevelopMentor
http://staff.develop.com/ballen
 
D

Dominique Vandensteen

Thanks for the info

but now, if I need to create events depending on a clicked button...
what is the right way to do it then?

this is in fact my case, but for now I worked around it by disable-ing my
buttons
 
B

Brock Allen

but now, if I need to create events depending on a clicked button...
what is the right way to do it then?

You dynamically add the control in the button click event, then upon every
postback you need to recreate the controls that were previousally dynamically
created. The trick is how to "remember" on the next postback what you had
previously created. I'd suggest using ViewState. Here's a sample I posted
a while back:

http://groups-beta.google.com/group...ic+controls+viewstate&rnum=1#a5717a97bd32c450

-Brock
DevelopMentor
http://staff.develop.com/ballen
 

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