tags:

views:

86

answers:

3

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

+4  A: 

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.

womp
Crud, that what I tried (and failed) to do the first time around. /me goes back to reading stuff...
BCS
Just post back here if you need some help with it :)
womp
Several issues: Your link reference adding a `<%@ Register...` to the .aspx file, but VS barfs if I have that and the `<%@ Page...` so I've got to be missing something here. That same link implies the `Register` tag need an `Assembly` attribute but I've only got one assembly... that doesn't seem right. Am I missing something else?
BCS
<%@ Register TagPrefix="MyControls" Assembly="WebApplication1" Namespace="WebApplication1" %> and then you should be able to do <MyControls:MyCalendar> </MyControls:MyCalendar> in the aspx file.
womp
When I add the Register tag VS barfs of the Page tag. If I drop the Page tag, how will it find my code? (BTW it seems VS is FUBAR'ed so I'm reinstalling... :(
BCS
Yeah, you should be able to have your @Page directive as well as multiple @Register directives. Did you try starting from a fresh .aspx file?
womp
And @Register is after @Page?
s_hewitt
After reinstalling a pile of crud, that works.
BCS
A: 

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" />
s_hewitt
where do you add the `<%@ Register`? After the `<%@ Page`? In place of it? After the `<html>`? In some other file?
BCS
The <%@ Register comes after the Page directive and before the HTML on the page.
Cyberherbalist
Yes, after the Page directive on an aspx page. If you're working with a control (.ascx) it would be after the Control directive (< % Control.... %>
s_hewitt
Well it would seem something is messed up then as I tried it there and it didn't work... I'm testing a few theories.
BCS
What errors did you get?
s_hewitt
A: 

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

John Saunders