BCS
BCS

Reputation: 78605

How do I make a custom ASP.NET control based/subclassed on an existing one?

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.

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.


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.)

Upvotes: 2

Views: 156

Answers (3)

John Saunders
John Saunders

Reputation: 161783

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

Upvotes: 0

s_hewitt
s_hewitt

Reputation: 4302

In a new class file you need to inherit from System.Web.UI.WebControls.Calendar instead of 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):

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

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

Upvotes: 0

womp
womp

Reputation: 116977

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.

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.

Upvotes: 4

Related Questions