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!
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!