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

前端 未结 3 1813
栀梦
栀梦 2021-02-06 15:55

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

相关标签:
3条回答
  • 2021-02-06 16:13

    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.

    0 讨论(0)
  • 2021-02-06 16:17

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

    0 讨论(0)
  • 2021-02-06 16:23

    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.

    0 讨论(0)
提交回复
热议问题