PlaceHodler动态添加控件在Web窗体上起到占位置的作用,可向动态地添加需要的控件。
如页面呈现的“确定”按钮和文本框都是在页面载入时动态生成的。
asp界面
<div>
<asp:PlaceHolder ID="plhTest" runat="server" ></asp:PlaceHolder><br />
<asp:Button ID="btnAcquire_Click" runat="server" Text="获取"
Onclick="btnAcquire_Clcik" />
</div>
protected void Page_Load(object sender, EventArgs e)
{
Button btnSubmit = new Button(); //定义btnSubmit控件按钮
btnSubmit.ID = "btnSubmit"; //设置btnSubmit控件的ID属性
btnSubmit.Text = "确定"; //设置btn按钮控件的Text属性
btnSubmit.Click += new EventHandler(btnSubmit_Click); //注册Clcik时间
plhTest.Controls.Add(btnSubmit); //将btnSubmit按钮控件添加到plhTest
TextBox txtInput = new TextBox(); //定义txtInput文本框控件
txtInput.ID = "txtInput";
plhTest.Controls.Add(txtInput);
}
protected void btnAcquire_Clcik(object sender, EventArgs e)
{
Response.Write("出发了”确定“按钮 的Click事件!");
}
protected void btnSubmit_Click(object sender,EventArgs e)
{
//查找txtInput文本框控件
TextBox txtInput = (TextBox)plhTest.FindControl("txtInput");
Response.Write(txtInput.Text);
}