Wiring up an Event To Dynamically Added Control

J

Jordan

I need to dynamically add an ImageButton control to a user control and and
do some server-side processing when the user clicks it. While I the
ImageButton is added to the user control at runtime, as needed, I'm having
trouble wiring up its click event procedure. The problem is that when I go
to subscribe the ImageButton to the delegate, the ImageButton is <undefined>
(i.e., throws the "object not found" exception).

Note that I'm using ParseControl() to get the server-side control
declarations from a string (including the ImageButton's
definition/declaration)..

Here is what I have done. I'd appreciate any suggestions for what must be
changed in order to get the event to fire.
1. defined the ImageButton in the code-behind of the user control like this:
protected System.Web.UI.WebControls.ImageButton MyImageButton;

2. defined the ImageButton to be added at runtime in a string (that is later
fed to ParseControl()), like this:
string imageButtonControl = "<asp:ImageButton id=\"MyImageButton\"
runat=\"server\"
ImageUrl=\"file:///C:\\_\\testImage.jpg\"></asp:ImageButton>";

Note this string is concatenated with another string that contains more
html - prior to the single ParseControl call.

3. I call ParseControl AND add the resulting controls to the PlaceHolder
during the OnInit event procedure.

4. Then I try to wire up the delegate, like this:
MyImageButton.Click += new
System.Web.UI.ImageClickEventHandler(this.MyImageButton_Click);

And yes, there is a MyImageButton_Click event procedure.

No matter where I put that line (in step 4), MyImageButton is <undefined>
(and therefore the event doesn't get wired up).
I have tried that line in the OnInit event procedure after the
ParseControl() call, and I have tried it in the Page_Load event (because an
online article I found said it should go there - although that doesn't make
much sense to me). I have also tried placing it in the page's
InitializeComponent event procedure (but of course it wouldn't work there
because the ParseControl method hasn't added it to the page yet.

Any suggestions? Thanks!
 
B

Bruce Barker

you have to initialize MyImageButton to the control after you dynamically
create it.

-- bruce (sqlwork.com)
 
J

Jordan

Hi Bruce - I don't know what this means:
"initialize MyImageButton to the control"
Can you clarify?

Thanks!
 
J

Jordan

Okay, after that last post it dawned on me what you were talking about. I
now have a followup question.

This is what I did.

System.Web.UI.Control c = ParseControl(controlString);
c.ID = "DynamicallyAddedControls";
ProdDetailPlaceHolder.Controls.Add(c);

// Wire up the event for the ImageButton:
System.Web.UI.Control ctlTest;
ctlTest = ProdDetailPlaceHolder.FindControl("MyImageButton");
if (ctlTest != null) {
MyImageButton = (System.Web.UI.WebControls.ImageButton) ctlTest;
MyImageButton.Click += new
System.Web.UI.ImageClickEventHandler(this.MyImageButton_Click);
}


It works.

Is this the way I should be doing it? This code is called on every postback
during OnInit. Is this the way it should be done?

Thanks.
 
B

Bruce Barker

that correct, the control has to be recreated on postback and rewired.

-- bruce (sqlwork.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

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top