Wednesday, August 18, 2010

How to build a base control WebControl in asp.net

For the ResourceManager please refer this link
Base Control:
public class WebControlBase : WebControl
{
    #region Properties
 
    public bool RenderDefaultStyles { getset; }
    public string Domain
    {
        get
        {
            Uri requestUri = Context.Request.Url;
            HttpRequest request = Context.Request;
            string rootUrl = string.Format("{0}{1}{2}{3}",
                requestUri.Scheme,
                Uri.SchemeDelimiter,
                requestUri.Host,
                requestUri.IsDefaultPort ? string.Empty : string.Format(":{0}", requestUri.Port));
            return rootUrl.EndsWith("/") ? rootUrl : string.Format("{0}/", rootUrl);
        }
    }
    public string RootUrl
    {
        get
        {
            Uri requestUri = Context.Request.Url;
            HttpRequest request = Context.Request;
            string rootUrl = string.Format("{0}{1}{2}{3}{4}",
                requestUri.Scheme,
                Uri.SchemeDelimiter,
                requestUri.Host,
                requestUri.IsDefaultPort ? string.Empty : string.Format(":{0}", requestUri.Port),
                request.ApplicationPath);
            return rootUrl.EndsWith("/") ? rootUrl : string.Format("{0}/", rootUrl);
        }
    }
 
    #endregion
 
    #region Constructors
 
    public WebControlBase()
    {
        this.Initialize();
    }
 
    #endregion
 
    #region Methods
 
    private void Initialize()
    {
        this.RenderDefaultStyles = true;
    }
    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        ResourceManager.RegisterResource(this.Page, this.GetType(), string.Format("{0}.{0}.js", 
            this.GetType().Name), ResourceType.JavaScript, this.Domain);
        if (this.RenderDefaultStyles)
            ResourceManager.RegisterResource(this.Page, this.GetType(), string.Format("{0}.{0}.css", 
                this.GetType().Name), ResourceType.StyleSheet, this.Domain);
 
    }
 
    #endregion
}

Inherited Control:
public abstract class WebField : WebControlBase
{
    #region Properties
 
    public string Label { getset; }
 
    #endregion
 
    #region Constructors
 
    public WebField()
    {
        this.Initalize();
    }
 
    #endregion
 
    #region Methods
 
    private void Initalize()
    {
    }
    public override void RenderBeginTag(HtmlTextWriter writer)
    {
        writer.Write("<div class=\"{0} {1}\">"this.GetType().Name, 
            Properties.Resources.FieldCssClass);
        base.RenderBeginTag(writer);
    }
    protected override void RenderContents(HtmlTextWriter writer)
    {
        this.RenderFieldLabel(writer);
        this.RenderFieldControl(writer);
        this.RenderFieldValidator(writer);
        writer.Write("<div class\"ClearBoth\"></div>");
        base.RenderContents(writer);
    }
    public virtual void RenderFieldLabel(HtmlTextWriter writer)
    {
        if (!string.IsNullOrEmpty(this.Label))
            writer.Write("<div class=\"{0}\"><span>{1}</span></div>", 
                Properties.Resources.FieldLabelCssClass, this.Label);
    }
    public virtual void RenderFieldControl(HtmlTextWriter writer) 
    {
        this.RenderFieldControl(writer, this.RenderFieldContent());
    }
    public virtual void RenderFieldControl(HtmlTextWriter writer, string content)
    {
        writer.Write("<div class=\"{0}\">{1}</div>", Properties.Resources.FleldControlCssCass, content);
    }
    public virtual void RenderFieldValidator(HtmlTextWriter writer) 
    { 
    }
    public virtual string RenderFieldContent()
    {
        return string.Empty;
    }
    public override void RenderEndTag(HtmlTextWriter writer)
    {
        base.RenderEndTag(writer);
        writer.Write("</div>");
    }
    public new string ResolveUrl(string url)
    {
        if (url.StartsWith("http://")) return url;
        if (url.StartsWith("~"))
            return string.Concat(this.RootUrl, url.Replace("~/"string.Empty));
        else if (url.Contains("WebResource.axd"))
            return string.Concat(this.Domain, url.Remove(0, 1));
        return url;
    }
    protected override void OnPreRender(EventArgs e)
    {
        ResourceManager.RegisterResource(this.Page, this.GetType(), 
            "WebField.js"ResourceType.JavaScript, this.Domain);
        if (this.RenderDefaultStyles)
            ResourceManager.RegisterResource(this.Page, this.GetType(), 
                "WebField.css"ResourceType.StyleSheet, this.Domain);
        base.OnPreRender(e);
    }
 
    #endregion
}

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...