asp.net 2.0中如何动态添加一个textbox控件?
看了网上很多例子,好像都是1.1的,我的代码是这样的
======================================================================
protected void Button1_Click(object sender, EventArgs e)
{
TextBox txt = new TextBox();
txt.ID = "txt ";
txt.Text = "add a TextBox control ";
Page.Controls.Add(txt);
}
protected void Button2_Click(object sender, EventArgs e)
{
Label lbl = new Label();
lbl.ID= "lbl ";
lbl.Text = "add a Label control ";
Page.Controls.Add(lbl);
}
=================================================================
其中button2执行没有问题,label可以动态产生,可是textbox就不行了,报一个这样的错误
Server Error in '/ ' Application.
--------------------------------------------
Control 'txt ' of type 'TextBox ' 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 'txt ' of type 'TextBox ' 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.
[解决办法]
不要加在Page里
可以找一个PlaceHolder放,PlaceHolderID.Controls.Add(txt);
[解决办法]
没有做过帮你顶顶1
------解决方案--------------------
加到某个容器里先
同意 silentwins
[解决办法]
你的 .aspx 页面中要有一个 具有 runat=server 的 form 标签
最好紧跟 body 之后
对于这一点 1.x 和 2.0 都是一样的,
这是由于 ASP.NEt 服务器控件模型的内在要求