How to use AJAX in ASP.NET server user control

Does anyone know a good tutorial demonstrating the use of an existing AJAX management extender in a custom ASP.NET server?

I do not want to create "Custom AJAX Server Control". I want to create user server control that uses an existing AJAX management extender.

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

Any help would be greatly appreciated. Thanks.

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

+4
source share
3 answers

It looks like you are using a composite control. They look a lot like a user control, rather than using an ascx file to create the controls you create them programmatically. The big advantage of this when using a custom control is that you can create an assembly and use it in different projects.

A composite control can inherit either Control or WebControl. Usually, I usually find Control more useful for inheritance, because usually I donโ€™t need a lot of extra things that you get from WebControl, such as style properties, since I usually just style through a single CssClass property.

You also need to make sure your class implements the INamingContainer interface. This ensures that each child control is automatically given 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 the Control CreateChildControls method. All the logic for creating controls should go here. The structure will automatically make sure that it is called at the right time in the page life cycle.

Here is a small 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 do not think ASP.NET AJAX should complicate this. The only thing I can think of, you need to make sure that you create the ScriptManager on any page to which the composite control will be added.

Here's a complete example of this on MSDN. Another nice example on this blog .

+3
source

You want to create a custom control, not a custom control. A user control is a composite control, while a user control is a control built from scratch or from a base control.

0
source

I suggest you search on MSDN. I have seen some good articles about this topic in my magazines over the past year or two, which have been quite thorough. But I have no links to them, and I'm too lazy to google for you.: \

-1
source

All Articles