如何:使用Ajax在ASP.NET自定义服务器控件 [英] How To: Use AJAX in an ASP.NET Custom Server Control

查看:102
本文介绍了如何:使用Ajax在ASP.NET自定义服务器控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道,演示如何使用现有的AJAX控件扩展的自定义ASP.NET服务器控件一个好的教程?

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

我不希望建立一个自定义AJAX服务器控制。我想建立一个使用现有的AJAX控件扩展自定义服务器控件。

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.

我想一个asp结合:文本框,ASP:ImageButton的,ASP:的CustomValidator(从嵌入的资源客户端的JavaScript),和一个Ajax:CalendarExtender到一个自定义服务器控件。或者有这已经创造的呢?

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?

任何帮助将是很大的AP preciated。谢谢你。

Any help would be greatly appreciated. Thanks.

更新:基本上,我想创建一个CompositeControl的,有一个Ajax:CalendarExtender作为一个子控件

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

推荐答案

听起来像你是一个复合控件后。他们是pretty的多少完全一样,用户只控制,而不是使用ASCX文件创建编程方式创建他们所有的控件。这样在使用的用户控件是你最终的东西,你可以把一个程序集,在不同的项目中使用的一大优势。

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.

一个复合控件可以从Control或WebControl的继承。我个人通常会发现控制更有助于从因为我平时并不需要很多你自WebControl获得额外的东西,继承,如样式属性,因为通过一个单一的CssClass属性我通常只是风格。

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.

你还需要确保你的类实现INamingContainer接口。这将确保每个子控件将自动获得一个唯一的名称,如果控制在同一父容器多次使用。

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.

最重要的事情,当创建一个复合控件是重写控件的CreateChildControls方法。所有的逻辑实际创建的控制应该在这里。该框架将自动确保这被称为在正确的时间在页面生命周期。

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.

下面是一个小例子:

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";
        }
    }
}

我不认为ASP.NET AJAX应该复杂这么多。我唯一​​能想到的IST,你需要确保你创建的任何网页复合控件将被添加到一个ScriptManager。

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.

有一个完整的例子这在MSDN网站。有一个关于这个博客的另一个很好的例子。

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

这篇关于如何:使用Ajax在ASP.NET自定义服务器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆