By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,534 Members | 1,807 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,534 IT Pros & Developers. It's quick & easy.

Wiring up an Event To Dynamically Added Control

P: n/a
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.MyImageB utton_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!
Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
you have to initialize MyImageButton to the control after you dynamically
create it.

-- bruce (sqlwork.com)
"Jordan" <A@B.com> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
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.MyImageB utton_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!

Nov 19 '05 #2

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

Thanks!
"Bruce Barker" <br******************@safeco.com> wrote in message
news:us**************@TK2MSFTNGP14.phx.gbl...
you have to initialize MyImageButton to the control after you dynamically
create it.

-- bruce (sqlwork.com)
"Jordan" <A@B.com> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
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.MyImageB utton_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!


Nov 19 '05 #3

P: n/a
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.MyImageB utton_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.

"Jordan" <A@B.com> wrote in message
news:%2*******************@TK2MSFTNGP14.phx.gbl...
Hi Bruce - I don't know what this means:
"initialize MyImageButton to the control"
Can you clarify?

Thanks!
"Bruce Barker" <br******************@safeco.com> wrote in message
news:us**************@TK2MSFTNGP14.phx.gbl...
you have to initialize MyImageButton to the control after you dynamically
create it.

-- bruce (sqlwork.com)
"Jordan" <A@B.com> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
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.MyImageB utton_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!



Nov 19 '05 #4

P: n/a
that correct, the control has to be recreated on postback and rewired.

-- bruce (sqlwork.com)
"Jordan" <A@B.com> wrote in message
news:uQ**************@TK2MSFTNGP14.phx.gbl...
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.MyImageB utton_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.

"Jordan" <A@B.com> wrote in message
news:%2*******************@TK2MSFTNGP14.phx.gbl...
Hi Bruce - I don't know what this means:
"initialize MyImageButton to the control"
Can you clarify?

Thanks!
"Bruce Barker" <br******************@safeco.com> wrote in message
news:us**************@TK2MSFTNGP14.phx.gbl...
you have to initialize MyImageButton to the control after you
dynamically create it.

-- bruce (sqlwork.com)
"Jordan" <A@B.com> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
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.MyImageB utton_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!



Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.