本文介绍了如何从服务器代码添加Control到ContentPlaceHolder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是,我想做类似的事情:ContentPlaceHolder1.Controls.Add(Panel1),但我不想将Panel1放在aspx文件中,因为我想从抽象页面添加控件。

I mean, I want to do something like that: ContentPlaceHolder1.Controls.Add(Panel1), but I don't want to put Panel1 in aspx file because I want to add Controls from a abstract page.

推荐答案

Panel pnl = new Panel();
pnl.Controls.Add(new TextBox()); // add textbox to panel can be whatever control you want
ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("MainContent");
cph.Controls.Add(pnl);



或者:




Or alternatively:

Panel pnl2 = new Panel();
pnl2.Controls.Add(new TextBox()); // add textbox to panel can be whatever control you want
ContentPlaceHolder cph = (ContentPlaceHolder)this.Master.FindControl("MainContent").Controls.Add(pnl2);





ASPX页码:





ASPX Page Code:

<asp:content id="HeaderContent" runat="server" contentplaceholderid="HeadContent" xmlns:asp="#unknown">
</asp:content>
<asp:content id="BodyContent" runat="server" contentplaceholderid="MainContent" xmlns:asp="#unknown">
    <p>Page content here</p>
</asp:content>


这篇关于如何从服务器代码添加Control到ContentPlaceHolder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 09:05