Help! Control '_ctl0__ctl0' of type 'Button' must be placed inside a form tag with runat=server
异常的全文如下:
System.Web.HttpException: Control '_ctl0__ctl0' of type 'Button' must be placed inside a form tag with runat=server.
调用堆栈为
[HttpException (0x80004005): Control '_ctl0__ctl0' of type 'Button' must be placed inside a form tag with runat=server.]
System.Web.UI.Page.VerifyRenderingInServerForm(Control control)
我在msdn里查了VerifyRenderingInServerForm这个方法,说“This method will throw an exception if the page is not currently in the render phase of page processing, and inside the <form runat=server> tags”。
这个异常是在加载一个包含web用户控件的页面时发生的。控件是运行期时new出来的,然而我不确定这样的做法是否有错误,请高手赐教。
相关代码如下
Transfer.aspx.cs
---------------------------------------
uclTransfer = new uclTransfer1();
this.Controls.Add(uclTransfer);
---------------------------------------
uclTransfer1.ascx.cs
---------------------------------------------
//控件的构造函数
public uclTransfer1()
{
this.btn_Ok = new Button();
this.tb_Rate = new TextBox();
this.tb_Amount = new TextBox();
this.dl_In = new DropDownList();
this.dl_Out = new DropDownList();
this.Controls.Add(btn_Ok);
this.Controls.Add(tb_Rate);
this.Controls.Add(tb_Amount);
this.Controls.Add(dl_In);
this.Controls.Add(dl_Out);
}
--------------------------------------------------