包含内容的 Web 用户控件

发布于 2024-09-14 07:23:16 字数 1739 浏览 6 评论 0原文

用法:

    <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流年里的时光 2024-09-21 07:23:16

最后我发现没人说我可以在那里添加“如果”:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    if (_ContentTemplate != null)
         _ContentTemplate.InstantiateIn(BodyControlSpace); 
} 

finally I found it nobody said me I can add "if" over there :

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    if (_ContentTemplate != null)
         _ContentTemplate.InstantiateIn(BodyControlSpace); 
} 
握住你手 2024-09-21 07:23:16

从 Web 控件中的面板类派生怎么样?它会给你和 div 围绕你的控制,但也将允许你将内容放入其中。尝试这样:

public WindowControl : Panel
{
   // ...
}

或者,如果您不想在 html 中进行任何额外的更改,请使用 Literal 作为基本控件:

public WindowControl : 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:

public WindowControl : Panel
{
   // ...
}

Alternatively if you don't want any extra changes in your html use Literal as a base control:

public WindowControl : Literal
{
   // ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文