Wiring up an Event To Dynamically Added Control

Discussion in 'ASP .Net' started by Jordan, Sep 1, 2005.

  1. Jordan

    Jordan Guest

    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!
    Jordan, Sep 1, 2005
    #1
    1. Advertising

  2. Jordan

    Bruce Barker Guest

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

    -- bruce (sqlwork.com)


    "Jordan" <> wrote in message
    news:...
    >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!
    >
    Bruce Barker, Sep 1, 2005
    #2
    1. Advertising

  3. Jordan

    Jordan Guest

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

    Thanks!


    "Bruce Barker" <> wrote in message
    news:usvFP$...
    > you have to initialize MyImageButton to the control after you dynamically
    > create it.
    >
    > -- bruce (sqlwork.com)
    >
    >
    > "Jordan" <> wrote in message
    > news:...
    >>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!
    >>

    >
    >
    Jordan, Sep 1, 2005
    #3
  4. Jordan

    Jordan Guest

    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.



    "Jordan" <> wrote in message
    news:%23l%23%...
    > Hi Bruce - I don't know what this means:
    > "initialize MyImageButton to the control"
    > Can you clarify?
    >
    > Thanks!
    >
    >
    > "Bruce Barker" <> wrote in message
    > news:usvFP$...
    >> you have to initialize MyImageButton to the control after you dynamically
    >> create it.
    >>
    >> -- bruce (sqlwork.com)
    >>
    >>
    >> "Jordan" <> wrote in message
    >> news:...
    >>>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!
    >>>

    >>
    >>

    >
    >
    Jordan, Sep 1, 2005
    #4
  5. Jordan

    Bruce Barker Guest

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

    -- bruce (sqlwork.com)


    "Jordan" <> wrote in message
    news:...
    > 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.
    >
    >
    >
    > "Jordan" <> wrote in message
    > news:%23l%23%...
    >> Hi Bruce - I don't know what this means:
    >> "initialize MyImageButton to the control"
    >> Can you clarify?
    >>
    >> Thanks!
    >>
    >>
    >> "Bruce Barker" <> wrote in message
    >> news:usvFP$...
    >>> you have to initialize MyImageButton to the control after you
    >>> dynamically create it.
    >>>
    >>> -- bruce (sqlwork.com)
    >>>
    >>>
    >>> "Jordan" <> wrote in message
    >>> news:...
    >>>>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!
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    Bruce Barker, Sep 1, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oisin Grehan
    Replies:
    3
    Views:
    479
    Natty Gur
    Jul 15, 2003
  2. Sosh
    Replies:
    5
    Views:
    4,113
    =?Utf-8?B?V296emE=?=
    Nov 16, 2005
  3. bill
    Replies:
    1
    Views:
    397
    Ken Cox - Microsoft MVP
    Apr 9, 2006
  4. =?Utf-8?B?amVzc2UgamFtZXMgMjAyMA==?=

    Custom Web Control - viewstate and event wiring questions

    =?Utf-8?B?amVzc2UgamFtZXMgMjAyMA==?=, Aug 18, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    406
    =?Utf-8?B?amVzc2UgamFtZXMgMjAyMA==?=
    Aug 18, 2006
  5. Andrew Robinson

    Dynamically Loaded User Control Event Wiring

    Andrew Robinson, May 23, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    783
    Steven Cheng[MSFT]
    May 28, 2007
Loading...

Share This Page