如何在现有的基础上创建自定义ASP.NET控件/子类?

时间:2022-09-02 13:20:00

I want to make a custom ASP.NET control that is a subclasse of System.Web.UI.WebControls.Calendar. I need it to add a few data members and set up a few event handlers in it's constructor.

我想创建一个自定义ASP.NET控件,它是System.Web.UI.WebControls.Calendar的子类。我需要它添加一些数据成员并在其构造函数中设置一些事件处理程序。

Is this possible? If so How?

这可能吗?如果是这样如何?

So far I have tried using add new item to add a default web user control and tried editing in Calendar in a few different places. None have worked so far.

到目前为止,我已尝试使用添加新项目添加默认Web用户控件,并尝试在日历中的几个不同位置进行编辑。迄今为止没有人工作过。


Edit: it seems I'm totally missing how this stuff is supposed to work. Does anyone known of a demo project that I can look at? Something that already exists. (Unless you are really board, don't go out and make one for me.)

编辑:似乎我完全不知道这些东西应该如何工作。有谁知道我可以看一个演示项目?已经存在的东西。 (除非你是真的登上,不要出去为我做一个。)

3 个解决方案

#1


Unless I'm misunderstanding the question, you can just create a new class file and inherit from Calendar. Add in the properties you need, and the event handlers you want to set up.

除非我误解了这个问题,否则你可以创建一个新的类文件并从Calendar继承。添加您需要的属性以及要设置的事件处理程序。

public class MyCalendar : System.Web.UI.WebControls.Calendar
{
   public MyCalendar()
   {
      // set up handlers/properties
   }
}

Then anywhere you'd like to add a Calendar to your pages, you can simply create a MyCalendar instead. If you need to do so in the designer, you can look at several good tutorials about how to make your inherited controls show their new properties in the designer, like this one.

然后,只要您想在页面中添加日历,就可以创建一个MyCalendar。如果您需要在设计器中执行此操作,您可以查看几个有关如何使继承的控件在设计器中显示其新属性的好教程,就像这样。

#2


In a new class file you need to inherit from System.Web.UI.WebControls.Calendar instead of System.Web.UI.UserControl.

在新的类文件中,您需要从System.Web.UI.WebControls.Calendar而不是System.Web.UI.UserControl继承。

namespace MyNamespace
{
  [ToolboxData("<{0}:UserCalendar runat=\"server\" />")]
  public class UserCalendar : System.Web.UI.WebControls.Calendar
  {
     private string property1;
     public UserCalendar() { }
     public string Property1 { get { return property1;} set { property1 = value; } }
  }
}

Then on your .aspx page (or in another control .ascx):

然后在.aspx页面上(或在另一个控件.ascx中):

<%@ Register TagPrefix="userControl" namespace="MyNamespace" assembly="MyProjectAssembly" %>

<userControl:UserCalendar runat="server" ID="myCalendar" property1="some value" />

#3


Stuff to read: Developing Custom ASP.NET Server Controls.

阅读内容:开发自定义ASP.NET服务器控件。

#1


Unless I'm misunderstanding the question, you can just create a new class file and inherit from Calendar. Add in the properties you need, and the event handlers you want to set up.

除非我误解了这个问题,否则你可以创建一个新的类文件并从Calendar继承。添加您需要的属性以及要设置的事件处理程序。

public class MyCalendar : System.Web.UI.WebControls.Calendar
{
   public MyCalendar()
   {
      // set up handlers/properties
   }
}

Then anywhere you'd like to add a Calendar to your pages, you can simply create a MyCalendar instead. If you need to do so in the designer, you can look at several good tutorials about how to make your inherited controls show their new properties in the designer, like this one.

然后,只要您想在页面中添加日历,就可以创建一个MyCalendar。如果您需要在设计器中执行此操作,您可以查看几个有关如何使继承的控件在设计器中显示其新属性的好教程,就像这样。

#2


In a new class file you need to inherit from System.Web.UI.WebControls.Calendar instead of System.Web.UI.UserControl.

在新的类文件中,您需要从System.Web.UI.WebControls.Calendar而不是System.Web.UI.UserControl继承。

namespace MyNamespace
{
  [ToolboxData("<{0}:UserCalendar runat=\"server\" />")]
  public class UserCalendar : System.Web.UI.WebControls.Calendar
  {
     private string property1;
     public UserCalendar() { }
     public string Property1 { get { return property1;} set { property1 = value; } }
  }
}

Then on your .aspx page (or in another control .ascx):

然后在.aspx页面上(或在另一个控件.ascx中):

<%@ Register TagPrefix="userControl" namespace="MyNamespace" assembly="MyProjectAssembly" %>

<userControl:UserCalendar runat="server" ID="myCalendar" property1="some value" />

#3


Stuff to read: Developing Custom ASP.NET Server Controls.

阅读内容:开发自定义ASP.NET服务器控件。