Adding controls at runtime

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?
 
T

Teemu Keiski

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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top