Wire up a click event within another click event doesn't work...

C

Chu

Thanks everyone for taking a moment to read this.

I've got a page where I use a LinkButton and I wire up a dynamic event
to the button. When the user clicks the button, the event is fired as
expected. In the event code for that button, a new LinkButton is added
to the page and is wired up to yet a different event, however when
clicked, the page is posted back but the event is not triggered.

I'm assuming it has something to do with the order of events that are
being fired (i.e, it appears page_load gets hit then the first
LinkButton's click event is fired) and so the wire up isn't working out
as expected.

My problem is that I have no idea where to go from here :) Is it even
possible to do what I'm wanting, i.e, wire up a 2nd button on the same
page that the 1st button was pushed?

Thanks for any help.
 
K

Karl Seguin [MVP]

It isn't an easy problem to solve.

Basically, for an event to fire, it needs to be hooked up on postback during
or before page_load.

you already have 2 problems.

(a) you aren't hooking it up during postback (well, you are during 1
postback, but not the 2nd one which is the important one)
(b) even if you were, it'd be too late

this is what you are doing:

Button 1 clicked
Page_Load
Setup event for button1 (you might be doing this somewhere else, you
didn't specify
end load
LoadPostback
Create 2nd button
Setup eventhandler for button2
end loadPostback

Button2 Clicked
Page_Load
Nothing's happening
end load
No events to raise


What needs to happen in Button2 click is:
Page_Load
Recreate button2
Hook up the event
end load
Here your event will fire.


As I said, it isn't fun to solve. If your pages don't need viewstate, I'd
personally use straight links and pass vlaues in the querystring <a
href="samepage.aspx?action=button2">blah</a>

If viewstate is important, you need to do somethin glike:

Button1 click:
create2nd button
ViewState.Add("Load2ndButton", true);
end click

Postback:
Page_Load
IF Page.IsPostBack then
If ViewState["Load2ndButton"] = true then
Load button 2
Setup event handler
end if
end if
End page load


all pseudo code of course...hopefully that gives you some ideas..

Karl
 
W

Winista

Its the dynamically added link button that is causing the problem for you.
By the time page_load finishes the control hierarchy should be recreated so
that the events can be wired properly to the control that caused it. Since
you are not adding that link button in Page_Load or before, that event is
lost.
What you may have to do is, look at __EVENTTARGET value in FORM's collection
in page_load to check if the event was caused by your dynamically added
control and then do your stuff.

http://www.netomatix.com
 

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