Help figure out what my options are for solving this programming dilemma. I've inherited an ASP.net project (C#) that is poorly written and even more poorly documented. Apparently the previous owners made a change that broke a page and I am stumped as to how to fix it. It has to do with events not firing for dynamically loaded user controls. If you're still interested, read on. If not, I don't blame you.
Okay, here's the nitty-gritty. I have a report page (we'll call it Report_1) that dynamically loads one of a few user controls depending on the criteria specified. This is happening late in the page life cycle (i.e. well after Page_Load()). For the purpose of this question we'll just focus on one user control (even though the problem is with all of them).
The user control contains a DataGrid and a handful of column templates. Each column template contains a LinkButton control with an OnCommand attribute. The idea is that when the LinkButton is clicked the user is redirected to another more specific report (we'll call this Report_2). So when the LinkButton is clicked the specified event handler should be called, the criteria assembled in a particular format for Report_2 and stored in session, and the user redirected to the Report_2 page which grabs the criteria and runs the report. If you're still with me you may already know the problem. Dynamically loading user controls after Page_Load() causes any events in the control not to fire (see HERE
I need some way to get to a method within the user control code behind when one of the LinkButtons is clicked and that's where it gets complicated. I'll list the options that I can see and why they don't seem to work.
1. Explicitly add ID to LinkButton controls
. This is the recommended fix I see the most, and it makes sense if you're explicitly creating the controls, but how do you do this when you're dealing with controls in a template? There could be 1-n controls created depending on the data. I tried grabbing the control using the ID specified in the template and then programmatically assigning an ID in ItemDataBound and ItemCreated, but it didn't seem to have any effect.
2. Recreate the controls in the Page_Load() event
. The first link above contains a link to an example
of how to do this. Again, the problem is that I'm dealing with a template so I don't have the ability to recreate each of the LinkButtons (that I know of). I don't even think this makes sense in my situation.
3. Force postback by populating OnClientClick with ClientScript.GetPostBackEventReference()
. I can use this to pass the info I need but I have to capture it in the Page_Load() event of the hosting page (Report_1) which is really ugly for a number of reasons. The Page_Load() event in the user control apparently doesn't get called during postback. At least not when postback is done this way. Another problem is that the functionality used to build the criteria for Report_2 is contained in a class that is inherited by the user control, not the hosting Report_1 page, so I cannot properly assemble the criteria in the format expected by Report_2 in Report_1's code behind. It has to be done in the user control code behind.
So the dilemma is that I can postback to the hosting page (Report_1) and not get to Report_2 or I can get to Report_2 from the user control but I can't get to an event handler in the user control... if that makes any sense. I'll be happy to clarify anything that needs clarifying.
My brain is in a bit of a fog and I'm hoping I'm missing an elegant and simple solution. If anyone can help me with this I will be forever in your debt!