Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 包含内容的Web用户控件_C#_Asp.net_Web Controls_Webusercontrol - Fatal编程技术网

C# 包含内容的Web用户控件

C# 包含内容的Web用户控件,c#,asp.net,web-controls,webusercontrol,C#,Asp.net,Web Controls,Webusercontrol,用法: } 问题是它给了我以下错误: 对象引用未设置为对象的实例。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源 异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。 源错误: 第21行:{ 第22行:base.OnInit(e); 第23行:_ContentTemplate.instanceIn(BodyControlSpace); 第24行:} 第25行:如何从web控件中的pan

用法:

}

问题是它给了我以下错误:
对象引用未设置为对象的实例。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

源错误:

第21行:{
第22行:base.OnInit(e);
第23行:_ContentTemplate.instanceIn(BodyControlSpace);
第24行:}

第25行:

如何从web控件中的panel类派生。它会让你和div围绕你的控制,但也会让你把内容放在里面。试着这样做:

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;}
}
或者,如果不希望对html进行任何额外更改,请使用文字作为基本控件:

public WindowControl : Panel
{
   // ...
}

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

public WindowControl : Literal
{
   // ...
}

我很高兴你找到了一个解决方案我也有同样的问题,当我使用这种方法作为用户控件时,这种方法是有效的,但当我尝试将其转换为控件时。同样的问题。
public WindowControl : Panel
{
   // ...
}
public WindowControl : Literal
{
   // ...
}
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
    if (_ContentTemplate != null)
         _ContentTemplate.InstantiateIn(BodyControlSpace); 
}