Handling events on automatically generated web controls

Discussion in 'ASP .Net' started by JezB, Jun 24, 2004.

  1. JezB

    JezB Guest

    I am dynamically creating WebControls and creating them on my page. Some of
    these are buttons, and I attach events to these buttons. The problem is
    these force a post-back, so I must regenerate all the webcontrols in the
    same way (I am assuming - am I right?).

    It was all working fine but now I get the error:

    "An error has occurred because a control with auto-generated id '_ctl129'
    could not be located to raise a postback event. To avoid this error,
    explicitly set the ID property of controls that raise postback events."

    occurring between Page_Load and actually firing the event. I can basically
    understand why but I dont know why it was working and now it isnt (I just
    added some code to evaluate a dataset in generating the webcontrols - if I
    take this out it works again).

    What can I do about it ?
    JezB, Jun 24, 2004
    #1
    1. Advertising

  2. JezB

    JezB Guest

    Incidentally, I have tried generating IDs for my buttons as the error
    suggests but this doesnt help. Same thing happens.

    "JezB" <> wrote in message
    news:...
    > I am dynamically creating WebControls and creating them on my page. Some

    of
    > these are buttons, and I attach events to these buttons. The problem is
    > these force a post-back, so I must regenerate all the webcontrols in the
    > same way (I am assuming - am I right?).
    >
    > It was all working fine but now I get the error:
    >
    > "An error has occurred because a control with auto-generated id '_ctl129'
    > could not be located to raise a postback event. To avoid this error,
    > explicitly set the ID property of controls that raise postback events."
    >
    > occurring between Page_Load and actually firing the event. I can basically
    > understand why but I dont know why it was working and now it isnt (I just
    > added some code to evaluate a dataset in generating the webcontrols - if I
    > take this out it works again).
    >
    > What can I do about it ?
    >
    >
    JezB, Jun 24, 2004
    #2
    1. Advertising

  3. JezB wrote:

    > Incidentally, I have tried generating IDs for my buttons as the error
    > suggests but this doesnt help. Same thing happens.
    >
    > "JezB" <> wrote in message
    > news:...
    >
    >>I am dynamically creating WebControls and creating them on my page. Some

    >
    > of
    >
    >>these are buttons, and I attach events to these buttons. The problem is
    >>these force a post-back, so I must regenerate all the webcontrols in the
    >>same way (I am assuming - am I right?).
    >>
    >>It was all working fine but now I get the error:
    >>
    >>"An error has occurred because a control with auto-generated id '_ctl129'
    >>could not be located to raise a postback event. To avoid this error,
    >>explicitly set the ID property of controls that raise postback events."
    >>
    >>occurring between Page_Load and actually firing the event. I can basically
    >>understand why but I dont know why it was working and now it isnt (I just
    >>added some code to evaluate a dataset in generating the webcontrols - if I
    >>take this out it works again).
    >>
    >>What can I do about it ?
    >>
    >>

    >
    >
    >

    What dynamic method are you using to create those web controls? Adding
    them to a repeater maybe? Loading from a path? When you say
    WebControls, I'm assuming you mean the intrinsic controls, such as
    asp:textbox or are you maybe talking about Customr Web Controls you
    created, or Custome User Controls? Too many pieces missing to give you
    a definite answer ... a code sample would help.

    ib.
    Ireney Berezniak, Jun 25, 2004
    #3
  4. JezB

    JezB Guest

    I got round it by saving my HtmlTable object in session, then on postpack
    reinstating it and adding it again to the placeholder (rather than going
    through the loop again).

    "JezB" <> wrote in message
    news:%...
    > I create an HtmlTable object then in a loop populate the rows and

    columns -
    > some of the columns contain ImageButton controls, to which I attach the
    > events. Then I add the HtmlTable to a placeholder that I have embedded

    into
    > the design surface. ie (pseudo-code) :-
    >
    > HtmlTable t = new HtmlTable();
    > foreach (...)
    > {
    > HtmlTableRow rw = new HtmlTableRow();
    >
    > // add a button column
    > HtmlTableCell tb1 = new HtmlTableCell();
    > ImageButton b1 = new ImageButton();
    > b1.Click += new ImageClickEventHandler(b1_Click);
    > tb1.Controls.Add(b1);
    > rw.Cells.Add(tb1);
    >
    > // add some other columns
    > ...
    >
    > // add the row to the table
    > t.Rows.Add(rw);
    > }
    > // add the table to the placeholder
    > PlaceHolder1.Controls.Add(t);
    >
    > Clicking on the generated imagebutton forces a postback before the event

    is
    > called (is there any way to prevent this?) so the same loop is executed
    > before the event fires. Problem being that the event does NOT then fire, I
    > get the error.
    >
    > "Ireney Berezniak" <> wrote in message
    > news:8HMCc.4241$l6.763@clgrps12...
    > > JezB wrote:
    > >
    > > > Incidentally, I have tried generating IDs for my buttons as the error
    > > > suggests but this doesnt help. Same thing happens.
    > > >
    > > > "JezB" <> wrote in message
    > > > news:...
    > > >
    > > >>I am dynamically creating WebControls and creating them on my page.

    Some
    > > >
    > > > of
    > > >
    > > >>these are buttons, and I attach events to these buttons. The problem

    is
    > > >>these force a post-back, so I must regenerate all the webcontrols in

    the
    > > >>same way (I am assuming - am I right?).
    > > >>
    > > >>It was all working fine but now I get the error:
    > > >>
    > > >>"An error has occurred because a control with auto-generated id

    > '_ctl129'
    > > >>could not be located to raise a postback event. To avoid this error,
    > > >>explicitly set the ID property of controls that raise postback

    events."
    > > >>
    > > >>occurring between Page_Load and actually firing the event. I can

    > basically
    > > >>understand why but I dont know why it was working and now it isnt (I

    > just
    > > >>added some code to evaluate a dataset in generating the webcontrols -

    if
    > I
    > > >>take this out it works again).
    > > >>
    > > >>What can I do about it ?
    > > >>
    > > >>
    > > >
    > > >
    > > >

    > > What dynamic method are you using to create those web controls? Adding
    > > them to a repeater maybe? Loading from a path? When you say
    > > WebControls, I'm assuming you mean the intrinsic controls, such as
    > > asp:textbox or are you maybe talking about Customr Web Controls you
    > > created, or Custome User Controls? Too many pieces missing to give you
    > > a definite answer ... a code sample would help.
    > >
    > > ib.

    >
    >
    JezB, Jun 25, 2004
    #4
    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. =?Utf-8?B?RmVkZXJpY28=?=
    Replies:
    5
    Views:
    4,959
    Chilá
    Feb 1, 2006
  2. DC
    Replies:
    2
    Views:
    647
  3. Replies:
    2
    Views:
    878
    Arne Vajhøj
    Oct 21, 2010
  4. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    353
    Chris
    Aug 30, 2005
  5. Replies:
    1
    Views:
    155
    Peter Michaux
    Apr 27, 2007
Loading...

Share This Page