如何从另一个UserControl继承一个UserControl? [英] How to inherit a UserControl from another UserControl?

查看:127
本文介绍了如何从另一个UserControl继承一个UserControl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to inherit a User control from another user control?

我要实现的目的是从另一个用户控件继承的用户控件.所以我有baseusercontrol.ascx,它只有文本"Stuff".然后,我有另一个用户控件childusercontrol.ascx继承了baseusercontrol.ascx.如果我不更改childusercontrol.ascx中的任何内容,则希望baseusercontrol.ascx文本显示为"Stuff".

The thing I'm trying to achieve is a user control inheriting from another user control. So I have baseusercontrol.ascx, and this just has text "Stuff". Then I have another user control, childusercontrol.ascx inheriting off of baseusercontrol.ascx. If I don't change anything in childusercontrol.ascx, I would expect the baseusercontrol.ascx text to display of "Stuff".

而且我还应该能够在派生类中扩展基本的用户控件功能.

And also I should be able to extend the base user control functionalities in derived one.

我也尝试过类似> ,但对我而言还不够.

I have also tried something like this, but its not enough in my case.

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

Now my childusercontrol.ascx looks lie below

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="childusercontrol.ascx.cs" Inherits="test_childusercontrol" %>
<%@ Register src="baseusercontrol.ascx" tagname="baseusercontrol" tagprefix="uc1" %>

childusercontrol.ascx.cs如下

childusercontrol.ascx.cs as below

public partial class test_childusercontrol : baseusercontrol
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }   
}

当我浏览此页面时,我收到错误消息

When I browse this page Im getting error as

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基类继承.

I have tested this situation for myself. Actually you can not inherit from a base class that is a UserControl with it's own ASCX code.

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

What you can do however, is implementing some basic functionality in a (possibly abstract) base class (without ASCX) like this:

public class BaseClass:UserControl
{
    public String BaseGreeting { get { return "Welcomebase!"; }}
}

,然后在具有自己的ASCX文件的具体UserControl类中使用此基类的方法和属性.

And then use the methods and properties of this base class in concrete UserControl classes that have their own ASCX file.

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; }
    }
}

这篇关于如何从另一个UserControl继承一个UserControl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆