views:

2330

answers:

3

Hi,

I'm creating a file-browser in ASP.Net but I have run into a problem which I believe relates to event-wireing on dynamic controls. I'll explain what my code does here:

When my page loads (OnLoad) i call a method (createStuff()) that create a number of linkbuttons (representing folders in a current directory) which are wired to an event that changes the current directory to the one that is associated with the linkbutton.

Since a linkbutton-click-event is fired after OnLoad is called (and the current directory therefore is first changed after onload) the linkbuttons doesn't update to reflect the new current directory (they are created before the current directory is changed by the click-event). Therefore I also call createStuff() in OnLoadComplete resulting in the linkbuttons being updated and reflecting the folders in the new current directory.

HOWEVER (and this is my problem) it seems that the event wireing (Click += new EventHandler(changeDirectory)) I do in createStuff() doesn't really work when the method is called in OnLoadComplete (OnLoadComplete is called after a linkbutton-click-event has been fired). The result is that while the text-property of the linkbuttons reflect the folders in the new directory clicking the linkbuttons doesn't fire the event.

How can I fix this problem? I tried rewireing the events on the LinkButtons in OnLoad but same result.

Thank you in advance

Simon, Denmark

A: 

Set the LinkButton's ID property consistently.

Two common causes of this behavior are (1) missing event handlers and (2) missing ID properties. Since you're sure that your event handler is being attached during OnLoad then missing ID properties is the likely culprit.

When a user clicks your LinkButton on the client, a PostBack is sent to the server which names the LinkButton by ID. If you're not ensuring that your control has the same ID between PostBacks your event handler might not get raised.

Ken Browning
I had solved the problem simply by redirecting the browser to the page after the linkbutton click-event had happened (the filesystembrowser is stored in a session-variable). But I think your solution (which works) is better, thanks for helping out!
A: 

I've searched the internet and tried everything - had the same problem - and Ken's answer was it: I gave my dynamically created LinkButton an ID and now the dynamically added command is processed. Thanks Ken!

Patrick
A: 

thanks KEN i also solved my problem by attaching Ids to my dunamically generated linkbuttons

pallavis eth