Question : "Control <control> must be placed inside a form tag with runat=server" error

VS 2008

I'm trying to dynamically add controls to my webpage on the OnPreInit event - which is where you are supposed to be able to add the controls, however I keep getting the error below.

At first I tried dynamically adding the panel control, but after getting the error I added it on the page manually and then tried just adding the buttons dynamically, but I keep getting the same error. What am I doing wrong?

Server Error in '/chapt1and2' Application.
--------------------------------------------------------------------------------

Control 'ctl01' of type 'Button' must be placed inside a form tag with runat=server.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'ctl01' of type 'Button' must be placed inside a form tag with runat=server.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:


[HttpException (0x80004005): Control 'ctl01' of type 'Button' must be placed inside a form tag with runat=server.]
   System.Web.UI.Page.VerifyRenderingInServerForm(Control control) +8686387
   System.Web.UI.WebControls.Button.AddAttributesToRender(HtmlTextWriter writer) +54
   System.Web.UI.WebControls.WebControl.RenderBeginTag(HtmlTextWriter writer) +20
   System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +20
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +8678454
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +8678454
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266

 
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
        
        string js = @"
                function butClick(but)
                {
                    alert(but.Value + 'clicked');
                 }
            ";
        Page.RegisterClientScriptBlock("ButtonClick",js);
        Button b;
        // Panel p = new Panel();        
        Panel p = Page.FindControl("Panel1") as Panel;
        if (p != null)
        {
            // Page.Controls.Add(p);
            for (int i = 0; i < 25; i++)
            {
                b = new Button();
                b.Text = ((char)(65 + i)).ToString();
                b.OnClientClick = "butClick(this);";
                Page.Controls.Add(b);
            }
        }
 
    }
 
-----
 
 
<%@ Page Language="C#" AutoEventWireup="true" Trace="true" ClassName="Chapt1and2Class" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 

 


    


    
    
hello world


Answer : "Control <control> must be placed inside a form tag with runat=server" error

you should add the controls to Panel
so instead of:
     Page.Controls.Add(b);
try:
     p.Controls.Add(b);

Or you can do
 Panel1.Controls.Add(b) ...no need to find Panel control.
Random Solutions  
 
programming4us programming4us