views:

1917

answers:

3

Does anyone know of a good tutorial that demonstrates using an existing AJAX control extender in a Custom ASP.NET Server Control?

I do not want to build a "Custom AJAX Server Control". I want to build a Custom Server Control that uses an existing AJAX control extender.

I would like to combine an asp:TextBox, asp:ImageButton, asp:CustomValidator (with client side javascript from an embedded resource), and an ajax:CalendarExtender into one custom server control. Or has this already been created?

Any help would be greatly appreciated. Thanks.

UPDATE: Basically, I would like to create a CompositeControl that has an ajax:CalendarExtender as a child control.

A: 

What you want is to build a user control and not a custom control most probably. A user control is a composite control whereas a custom control is a control built either from the ground up either derived from a basic control.

Andrei Rinea
Actually, I already have this working as a User Control, but YES that is easier (if you are looking for a quick fix - DO THIS). But, I would like to reuse a control like this without having to copy all of the individual pieces of the user control around.
Aaron Hoffman
A: 

I would suggest you search on MSDN. I have seen several good articles about that topic in their magazines over the last year or two, that have been fairly thorough. But I don't have links to them and I'm too lazy to Google for you. :\

Mufasa
I have done lots of Searching for this and have found nothing useful. That is why I posted the question here. If you find time, please post the links. Thank You.
Aaron Hoffman
A: 

Sounds like what you're after is a composite control. They are pretty much exactly like a user control only instead of using the ascx file to create the controls you create them all programmatically. The big advantage of doing this over using a user control is you end up with something you can put in an assembly and use in different projects.

A composite control can inherit from either Control or WebControl. I personally usually find Control more useful to inherit from because I usually don't need a lot of the extra stuff you get from WebControl such as the styling properties since I usually just style through a single CssClass property.

You'll also need to make sure your class implements the INamingContainer interface. This will make sure that each child control will automatically get a unique name if the control is used multiple times in the same parent container.

The most important thing to do when creating a composite control is to override Control's CreateChildControls method. All the logic for actually creating the controls should go in here. The framework will automatically make sure that this gets called at the right time in the page lifecycle.

Here's a little example:

public class MyCompositeControl : Control, INamingContainer
{
    protected override void CreateChildControls()
    {
        Controls.Clear();

        var textbox = new TextBox();
        textbox.ID = "TextBox1";
        Controls.Add(textbox);
        if (!Page.IsPostBack || !IsTrackingViewState)
        {
            // make sure you set the properties after
            // you add it to the parent or the viewstate
            // won't save properly
            textbox.MaxLength = 30;
        }

        var button = new Button();
        button.ID = "Button1";
        Controls.Add(button);
        if (!Page.IsPostBack || !IsTrackingViewState)
        {
            button.Text = "Save";
        }
    }
}

I don't think ASP.NET AJAX should complicate this much. The only thing I can think of ist you'll need to make sure that you create a ScriptManager on whatever page the composite control will be added to.

There's a full example of this on the MSDN site. There's another nice example on this blog.

Helephant
I would like to use the Composite Control method when creating my server control. But just fyi, ASP 2.0 and above have a new method for creating these http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.compositecontrol.aspx. Either way, AJAX ControlExtenders do not work well in them
Aaron Hoffman
Basically, I am looking for a tutorial that has an AJAX Control Extender as a child control in a CompositeControl
Aaron Hoffman
Nifty! I didn't know about the composite control class. I don't know a good tutorial specifically about ControlExtenders. Hope you find an answer.
Helephant