包含内容的 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 行:
Usage :
<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>
Control's source :
<asp:Panel ID="Panel2"
runat="server">
</div>
</asp:Panel>
<asp:PlaceHolder runat="server" ID="BodyControlSpace"/>
Code Behind :
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;}
}
}
The problem is it gives me following error :
Object reference not set to an instance of an object.
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.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 21: {
Line 22: base.OnInit(e);
Line 23: _ContentTemplate.InstantiateIn(BodyControlSpace);
Line 24: }
Line 25:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后我发现没人说我可以在那里添加“如果”:
finally I found it nobody said me I can add "if" over there :
从 Web 控件中的面板类派生怎么样?它会给你和 div 围绕你的控制,但也将允许你将内容放入其中。尝试这样:
或者,如果您不想在 html 中进行任何额外的更改,请使用 Literal 作为基本控件:
What about deriving from panel class in your web control. It will give you and div around your control but also will avaible you to put content inside. Try like this:
Alternatively if you don't want any extra changes in your html use Literal as a base control: