Adding controls at runtime

Discussion in 'ASP .Net' started by =?Utf-8?B?Y2FzaGRlc2ttYWM=?=, Aug 1, 2006.

  1. I have created a Datagrid in code, added it to the Controls collection and
    set it's DataSource. All works fine. I try the same thing with a button
    (create an instance, add it to the controls collection), it doesn't want to
    know. I just get an error stating:

    "Control '_ctl1' of type 'Button' must be placed inside a Form tag with
    runat=server".

    Can anyone suggest why it let me create and display the DataGrid but not a
    Button?
     
    =?Utf-8?B?Y2FzaGRlc2ttYWM=?=, Aug 1, 2006
    #1
    1. Advertising

  2. =?Utf-8?B?Y2FzaGRlc2ttYWM=?=

    Teemu Keiski Guest

    Hi,

    you need to add it inside the server-side form.

    If you have

    <form id="Form1" runat="server">

    </form>

    You'd need to add it to the form's Controls collection (Button requires
    such, datagrid doesn't unless it is set to contain buttons). E.g

    Dim btn As New Button()
    Page.FindControl("Form1").Controls.Add(btn)

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke


    "cashdeskmac" <> wrote in message
    news:...
    >I have created a Datagrid in code, added it to the Controls collection and
    > set it's DataSource. All works fine. I try the same thing with a button
    > (create an instance, add it to the controls collection), it doesn't want
    > to
    > know. I just get an error stating:
    >
    > "Control '_ctl1' of type 'Button' must be placed inside a Form tag with
    > runat=server".
    >
    > Can anyone suggest why it let me create and display the DataGrid but not a
    > Button?
     
    Teemu Keiski, Aug 1, 2006
    #2
    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. Uzi Baruch
    Replies:
    0
    Views:
    445
    Uzi Baruch
    Dec 17, 2003
  2. =?Utf-8?B?TGluZGEgV2llbmhvbHQ=?=

    Adding User Controls at Runtime

    =?Utf-8?B?TGluZGEgV2llbmhvbHQ=?=, Feb 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    369
    Hans Kesting
    Feb 9, 2004
  3. Richard Wilson
    Replies:
    0
    Views:
    6,254
    Richard Wilson
    Feb 26, 2004
  4. Charlie@CBFC

    Adding controls to Repeater during runtime

    Charlie@CBFC, May 11, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    3,870
    Charlie@CBFC
    May 11, 2004
  5. Mark Siffer

    adding controls dynamically at runtime

    Mark Siffer, Jun 21, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    532
    Mark Siffer
    Jun 21, 2004
Loading...

Share This Page