How To: Use AJAX in an ASP.NET Custom Server Control

吃可爱长大的小学妹 提交于 2020-01-22 10:03:07

问题


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.


回答1:


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.




回答2:


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.




回答3:


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. :\



来源:https://stackoverflow.com/questions/592614/how-to-use-ajax-in-an-asp-net-custom-server-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!