用法:Web用户控件的内容里面
<uc1:WindowControl ID="Window_ExpoNews" runat="server" Height="265px" Title="Expo News"
Width="100%">
<ContentTemplate>
This content will show in the center cell of this user control.
<br />
I can even add real controls.
<asp:TextBox ID="TextBox1" runat="server" />
<br />
Good times.
</ContentTemplate>
</uc1:WindowControl>
控制的来源:
<asp:Panel ID="Panel2"
runat="server">
</div>
</asp:Panel>
<asp:PlaceHolder runat="server" ID="BodyControlSpace"/>
代码背后:
public partial class Componants_WebUserControl : System.Web.UI.UserControl{
private ITemplate _ContentTemplate;
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate ContentTemplate
{
get { return _ContentTemplate; }
set { _ContentTemplate = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
_ContentTemplate.InstantiateIn(BodyControlSpace);
}
public Unit Width
{
get {return Panel1.Width;}
set {Panel1.Width = value;}
}
public Unit Height
{
get {return Panel1.Height;}
set {Panel1.Height = value;}
}
public string Title
{
get {return lblTitle.Text;}
set {lblTitle.Text = value;}
}
}
的问题是它给我跟随错误:
未将对象引用设置为对象的实例。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。
异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。
源错误:
线21:{
第22行:base.OnInit(E);
第23行:_ContentTemplate.InstantiateIn(BodyControlSpace);
第24行:}
第25行:
我有同样的问题,这个工程,当我用这种方法作为用户控件,但是当我试图将其转换为控件。一样的问题。 – 2012-04-17 13:40:30