Thursday, March 4, 2010

Run-time Error: Control 'controlname' of type 'Button' 'TextBox' 'GridView' 'CheckBox' must be placed inside a form tag with runat=server




Please visit my new Web Site WWW.Codedisplay.com



I found this error in Asp.Net when i am trying to add a control dynamically in the page. The error is Control 'controlname' of type 'Button' must be placed inside a form tag with runat=server. The error comes when you trying to add the control like GridView, Button, TextBox, RadioButton, RadioButtonList, CheckBox, CheckBoxList out side the form tag of your aspx page in runtime or dynamically. There were another reason you may add those controls in your page under form tag but when you open it in other editor like dreamweaver and drag thos controls then might be a chance that the control positioned outside the form tag. This type of problems solution is just place those controls inside the form tag. The error is:

error inside form

And the solution is:

inside form solution

This was the very basic solution. In my case when i runtime or dynamically add a button then i found this error for below code:
protected void Page_Load(object sender, EventArgs e)
    {
        Button cmd = new Button();
        cmd.Text = "Click Me";
        // Error comes from this line
        this.Controls.Add(cmd);
        cmd.Click += new EventHandler(Dynamic_Method);
    }

And the solution is simple look:
protected void Page_Load(object sender, EventArgs e)
    {
        Button cmd = new Button();
        cmd.Text = "Click Me";
        this.form1.Controls.Add(cmd);
        cmd.Click += new EventHandler(Dynamic_Method);
    }

Ok thats it. If you found this error for another reason like user control rendering problem please write those as a comment.

0 comments:

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash