Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.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
Asp.net 如何从另一个UserControl继承UserControl?_Asp.net_Inheritance_User Controls_Nullreferenceexception - Fatal编程技术网

Asp.net 如何从另一个UserControl继承UserControl?

Asp.net 如何从另一个UserControl继承UserControl?,asp.net,inheritance,user-controls,nullreferenceexception,Asp.net,Inheritance,User Controls,Nullreferenceexception,是否可以从另一个用户控件继承一个用户控件 我试图实现的是从另一个用户控件继承的用户控件。所以我有baseusercontrol.ascx,它只有文本“Stuff”。然后我有另一个用户控件,childusercontrol.ascx继承自baseusercontrol.ascx。如果我不更改childusercontrol.ascx中的任何内容,我希望baseusercontrol.ascx文本显示“Stuff” 而且,我应该能够在派生版本中扩展基本的用户控制功能 我也尝试过类似的方法,但对我来说

是否可以从另一个用户控件继承一个用户控件

我试图实现的是从另一个用户控件继承的用户控件。所以我有baseusercontrol.ascx,它只有文本“Stuff”。然后我有另一个用户控件,childusercontrol.ascx继承自baseusercontrol.ascx。如果我不更改childusercontrol.ascx中的任何内容,我希望baseusercontrol.ascx文本显示“Stuff”

而且,我应该能够在派生版本中扩展基本的用户控制功能

我也尝试过类似的方法,但对我来说还不够

现在我的childusercontrol.ascx看起来就在下面

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="childusercontrol.ascx.cs" Inherits="test_childusercontrol" %>
<%@ Register src="baseusercontrol.ascx" tagname="baseusercontrol" tagprefix="uc1" %>
当我浏览这个页面时,我发现错误如下

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.

有什么线索吗?

我已经亲自测试过这种情况。实际上,您不能用自己的
ASCX
代码从
UserControl
基类继承

但是,您可以在(可能是抽象的)基类中实现一些基本功能(没有
ASCX
),如下所示:

public class BaseClass:UserControl
{
    public String BaseGreeting { get { return "Welcomebase!"; }}
}
然后在具体的
UserControl
类中使用这个基类的方法和属性,这些类有自己的
ASCX
文件

public partial class childusercontrol : BaseClass
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Greeting = base.BaseGreeting; //As initial value
    }
    public String Greeting
    {
        get { return LabelGreeting.Text; }
        set { LabelGreeting.Text = value; }
    }
}

能否共享baseusercontrol.ascx中的代码?您的错误表示在特定行号处有空引用。我怀疑你有一个空引用。o_o您需要查看堆栈跟踪,就像异常消息告诉您的那样。几乎所有
NullReferenceException
的情况都是相同的。请参阅“”以获取一些提示。此外,不,用户控件不应被继承。您最好创建继承
UserControl
的基类,但不包含任何标记(no.ascx文件)。
public partial class childusercontrol : BaseClass
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Greeting = base.BaseGreeting; //As initial value
    }
    public String Greeting
    {
        get { return LabelGreeting.Text; }
        set { LabelGreeting.Text = value; }
    }
}