Click here to Skip to main content
15,895,667 members
Articles / Web Development / HTML

Creating an abstract AJAX user control

Rate me:
Please Sign up or sign in to vote.
4.73/5 (10 votes)
26 Jul 2007CPOL7 min read 46.3K   345   70  
This article describes the steps needed to create an abstract class that already takes care of AJAX requests of derived controls.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;

[assembly: WebResource("AbstractAjaxControl.Resources.AjaxBase.js", "text/javascript")]
namespace AjaxControls
{
    [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
    public abstract class AjaxControl : WebControl
    {
        private string _scope = null;

        public AjaxControl()
        {
            AjaxRequest += new AjaxRequestEventHandler(HandleAjaxRequest);
        }

        public string Scope
        {
            get { return _scope; }
            set { _scope = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            if (!Page.IsPostBack && Page.Request.Form["scope"] == _scope)
            {
                OnAjaxRequest();
            }

            base.OnInit(e);


            Page.ClientScript.RegisterClientScriptResource(typeof(AjaxControl), "AbstractAjaxControl.Resources.AjaxBase.js");
        }

        protected abstract void HandleAjaxRequest(object sender, AjaxRequestEventArgs e);
        protected abstract override void RenderContents(HtmlTextWriter output);

        private delegate void AjaxRequestEventHandler(object sender, AjaxRequestEventArgs e);
        private event AjaxRequestEventHandler AjaxRequest;
        private void OnAjaxRequest()
        {
            if (AjaxRequest != null)
                AjaxRequest(this, new AjaxRequestEventArgs(Page.Request.Form));
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions