Why it doesn't work?

Discussion in 'ASP .Net Web Controls' started by Ufit, Jul 1, 2006.

  1. Ufit

    Ufit Guest

    Dim img As New ImageButton
    Page.Controls.Add(img)

    It works with Image but ImageButton gives this error:
    Control '1' of type 'ImageButton' must be placed inside a form tag with runat=server.

    What's wrong???


    Thanks

    Ufi
    Ufit, Jul 1, 2006
    #1
    1. Advertising

  2. Ufit

    Guest

    ASP.NET follows the single form model therefore your imagebutton would
    need to be added to a form which in turn is added to a page.
    Normally the form is statically declared on a page (normally called
    "form1") and the dynamically created controls would be added to the
    statically declared form.

    'Assume a form of ID "form1" is already statically decalred on a page.
    Dim img As New ImageButton
    form1.Controls.Add(img)

    Ufit wrote:
    > Dim img As New ImageButton
    > Page.Controls.Add(img)
    >
    > It works with Image but ImageButton gives this error:
    > Control '1' of type 'ImageButton' must be placed inside a form tag with runat=server.
    >
    > What's wrong???
    >
    >
    > Thanks
    >
    > Ufi
    , Jul 2, 2006
    #2
    1. Advertising

  3. Ufit

    Ufit Guest

    Ok I got it but still when I add controls to form and f.ex. click on some button
    my controls (UserControl) get destroyed. Why is that?
    form1 is getting destroyed and reloaded as a new instance
    It looks like it's not static so how to make it static?
    Thanks

    ufi

    <> wrote in message news:...
    > ASP.NET follows the single form model therefore your imagebutton would
    > need to be added to a form which in turn is added to a page.
    > Normally the form is statically declared on a page (normally called
    > "form1") and the dynamically created controls would be added to the
    > statically declared form.
    >
    > 'Assume a form of ID "form1" is already statically decalred on a page.
    > Dim img As New ImageButton
    > form1.Controls.Add(img)
    >
    >
    Ufit, Jul 2, 2006
    #3
  4. Ufit

    Guest

    Yes, all controls gets destroyed after every response because of the
    stateless nature of http.

    The trick is to recreate it again on the next request. If you've
    declaratively added your controls to the page then the ASP.NET
    framework would automatically recreate your controls on each postback
    and make it look like the controls were never destroyed.
    Else if you are programmatically adding your controls then you have to
    programmatically add the controls back on each postback.

    To handle an event (eg.click) from a dynamically created control, you
    need to first recreate the control (such as during page_load) otherwise
    the event handler would never be called.

    Andy

    Ufit wrote:
    > Ok I got it but still when I add controls to form and f.ex. click on some button
    > my controls (UserControl) get destroyed. Why is that?
    > form1 is getting destroyed and reloaded as a new instance
    > It looks like it's not static so how to make it static?
    > Thanks
    >
    > ufi
    >
    > <> wrote in message news:...
    > > ASP.NET follows the single form model therefore your imagebutton would
    > > need to be added to a form which in turn is added to a page.
    > > Normally the form is statically declared on a page (normally called
    > > "form1") and the dynamically created controls would be added to the
    > > statically declared form.
    > >
    > > 'Assume a form of ID "form1" is already statically decalred on a page.
    > > Dim img As New ImageButton
    > > form1.Controls.Add(img)
    > >
    > >
    , Jul 2, 2006
    #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. David Prowak

    Why oh why doesn't my data view work?

    David Prowak, Jan 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    724
    Alvin Bruney [MVP]
    Jan 30, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    862
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
  4. Sara
    Replies:
    6
    Views:
    249
    John W. Krahn
    Apr 12, 2004
  5. PerlFAQ Server
    Replies:
    0
    Views:
    246
    PerlFAQ Server
    Apr 26, 2011
Loading...

Share This Page