Adding controls at runtime

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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?
 
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)
 
Back
Top