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