Last post Feb 03, 2009 08:23 PM by ambic
Feb 01, 2009 09:08 PM|ambic|LINK
I have a control that I have written that dynamically builds a few LinkButtons that send back an onclick event. I wire these up during the OnInit and toggle the visibility to make them accessible by the users. I originally attempted to create the objects
on the fly, but on the reload of the control the event call would vanish.
I now have to add a new piece of functionality to my control. Its basically a "Search" control, that will bring back a list of items. Each item needs a button to the side saying "Select this item" and on selecting it needs to pass back a value that I
can react to. I cannot create the event handler in the onInit section because I have no idea how many I will need, so I will need to create it afterwards. But these events don't do anything because on postback the controls they were associated with wont
Feb 01, 2009 09:38 PM|ambic|LINK
Just a bit more info about the type of thing I'm doing: This is from the microsoft ASP.Net books online
If you create controls by declaring them in markup, you can bind events to methods using an attribute (for example,
onclick) or in Visual Basic, by using the
Handles keyword. If you create controls dynamically (in code), you cannot use either of these methods, because the compiler does not have a reference to the control at compilation time.
In that case, you must use explicit event binding.
Button b = new Button;
b.Text = "Click";
b.Click += new System.EventHandler(ButtonClick);
The new Button B (declared to the class) appears fine. But on clicking reposts the whole page, and the event handler ButtonClick is never called. I need to be able to dynamically add about 20 buttons on average, but could have to do quite a few more, and
predeclaring them all in the OnInit is messy.
[EDIT: Work Around: I have a workaround. I am currently setting PostBackUrl to another page which handles parameters set inside the URL at the point of construction. This works
okay but doesn't really solve the issue. I am posting this here in case it helps someone else]
Feb 03, 2009 08:23 PM|ambic|LINK
This took a lot of achieving. I read internet pages for more than a day before coming up with the following:
Contrary to what a LOT of forums say, where you must recreate all controls on the OnInit override, you dont have to. What you must do is this:
1. Set OnInit to do the following:
This forces the Control State memory bank to be turned on.
2. Go to the routine where you are creating the controls, and specify the ID of the buttons. If you do not, you will not be able to complete stage 3.
3. Create/Edit the Page Load to reconstruct your controls which will no longer exist. They must have the SAME IDs, so I call the same routine that generated them in the first place. The page_load does work because it occurs in the page lifecycle
BEFORE the events are fired. If the IDs are inconsistent your events will vanish. This will cause an unintended side effect where your routine for generation is called OnLoad and then again with the page events. If you do need data reads etc, put them into
properties in a seperate routine, and have the object generation called seperately.