Article 2: rendering content _ Section 2: WebControl rendering; Article 2: webcontrol

Source: Internet
Author: User

Article 2: rendering content _ Section 2: WebControl rendering; Article 2: webcontrol
1. Presentation process of WebControl

WebControl is derived from the Control class. Therefore, the rendering function of WebControl is based on the Control rendering logic, but it has a large extension.

First, WebControl overrides the Render (HtmlTextWriter writer) method and divides the rendering logic into three types: RenderBeginTag (), RenderContents (), and RenderEndTag (). This design of WebControl is based on the assumption that each WebControl finally generates an HTML control (of course, This HTML control may also contain other HTML child controls). Therefore, the presentation process of WebControl can be divided: presents the start tag, content in the tag, and end tag.

protected internal override void Render(HtmlTextWriter writer)
{
       this.RenderBeginTag(writer);
       this.RenderContents(writer);
       this.RenderEndTag(writer);
}

① Present the start tag:

public virtual void RenderBeginTag(HtmlTextWriter writer)
{
    this.AddAttributesToRender(writer);
    HtmlTextWriterTag htmlTextWriterTag = this.TagKey;
    if (htmlTextWriterTag != HtmlTextWriterTag.Unknown)
    {
        writer.RenderBeginTag(htmlTextWriterTag);
        return;
    }
    writer.RenderBeginTag(this.TagName);
}

The preceding Implementation Code shows that the HTML Tag generated by the RenderBeginTag () method is determined by the WebControl. TagKey or WebControl. TagName attribute.

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
protected virtual HtmlTextWriterTag TagKey
{
    [TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
    get
    {
        return this.tagKey;
    }
}

The TagKey type is HtmlTextWriterTag enumeration. If your tag is not in this enumeration, set the TagKey attribute to Unkown and set the TagName attribute to the String type, this means that you can set this attribute to any value.

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
protected virtual string TagName
{
    get
    {
         if (this.tagName == null && this.TagKey != HtmlTextWriterTag.Unknown)
         {
              this.tagName = Enum.Format(typeof(HtmlTextWriterTag), this.TagKey, "G").ToLower(CultureInfo.InvariantCulture);
         }
         return this.tagName;
     }
}
  • Note: Normally, we do not rewriteRenderBeginTag ()Method, but by rewritingTagKeyOrTagNameAttribute to control the generated peripheral HTML tags.

② Present the content in the tag:

protected internal virtual void RenderContents(HtmlTextWriter writer)
{
    base.Render(writer);
}

③ Display the end tag:

public virtual void RenderEndTag(HtmlTextWriter writer)
{
    writer.RenderEndTag();
}

Here, the presentation process of WebControl is finished, but there is a question: how does the peripheral HTML Tag add attributes and style attributes?

I believe that careful students will surely find the first line of code in the WebControl class RenderBeginTag () method:

this.AddAttributesToRender(writer);

The attribute is added to the rendering content. Let's take a look at the implementation of this method:

protected virtual void AddAttributesToRender(HtmlTextWriter writer)
{
    if (this.ID != null)
    {
        writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
    }
    if (this._webControlFlags[4])
    {
        string accessKey = this.AccessKey;
        if (accessKey.Length > 0)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, accessKey);
        }
    }
    if (!this.Enabled)
    {
        if (this.SupportsDisabledAttribute)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
        }
        if (this.RenderingCompatibility >= VersionUtil.Framework40 && !string.IsNullOrEmpty(WebControl.DisabledCssClass))
        {
            if (string.IsNullOrEmpty(this.CssClass))
            {
                this.ControlStyle.CssClass = WebControl.DisabledCssClass;
            }
            else
            {
                this.ControlStyle.CssClass = WebControl.DisabledCssClass + " " + this.CssClass;
            }
        }
    }
    if (this._webControlFlags[16])
    {
        int tabIndex = (int)this.TabIndex;
        if (tabIndex != 0)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, tabIndex.ToString(NumberFormatInfo.InvariantInfo));
        }
    }
    if (this._webControlFlags[8])
    {
        string toolTip = this.ToolTip;
        if (toolTip.Length > 0)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Title, toolTip);
        }
    }
    if (this.TagKey == HtmlTextWriterTag.Span || this.TagKey == HtmlTextWriterTag.A)
    {
        this.AddDisplayInlineBlockIfNeeded(writer);
    }
    if (this.ControlStyleCreated && !this.ControlStyle.IsEmpty)
    {
        this.ControlStyle.AddAttributesToRender(writer, this);
    }
    if (this.attrState != null)
    {
        AttributeCollection attributes = this.Attributes;
        IEnumerator enumerator = attributes.Keys.GetEnumerator();
        while (enumerator.MoveNext())
        {
            string text = (string)enumerator.Current;
            writer.AddAttribute(text, attributes[text]);
        }
    }
}

First, AddAttributesToRender () is a virtual method, which can be rewritten because it is called at the beginning of RenderBeginTag, obviously, we can implement the logic of adding attributes and style attributes to the most peripheral HTML tags here. Secondly, we noticed that the WebControl class has completed basic functions (such as Enabled or not, different rendering) when implementing the RenderBeginTag () method. Therefore, when we rewrite this method to add custom attributes and style attributes for the peripheral tag, in order to obtain the basic functions provided by WebControl. You must call the method of the parent class in the derived control class:

protected override void AddAttributesToRender(HtmlTextWriter writer)
{
     base.AddAttributesToRender(writer);
// ...... Add the code for the required attributes and styles
}
2. derive the "album" control from the WebControl class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CustomServerControls
{
    public class Albumn : WebControl
    {
// (1): the default TagKey of WebControl is span, which is rewritten as Div.
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }
// (2) Add some style attributes to the main tag (Div)
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "center");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "194px");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "194px");
            writer.AddStyleAttribute("background", "url(Images/background.gif) no-repeat left");
        }
// (3): The content of the master label is an Img label.
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, "Images/nature.jpg");
            writer.AddAttribute(HtmlTextWriterAttribute.Width, "160px");
            writer.AddAttribute(HtmlTextWriterAttribute.Height,"160px");
            writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "none");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Padding, "0px");
            writer.AddStyleAttribute(HtmlTextWriterStyle.MarginTop, "16px");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
        }
    }
}

Webcontrol

Else {
// Use reflection to obtain the Text attribute of the control
System. Reflection. BindingFlags flags =
BindingFlags. GetProperty |
BindingFlags. Public |
BindingFlags. Instance;
System. Reflection. PropertyInfo info =
Ddl. GetType (). GetProperty ("Text", flags );
If (info! = Null ){
Ddl. ToolTip = Convert. ToString (info. GetValue (ddl ));
}
}

What is web control?

Web control was invented by Microsoft and requires constant interaction with Web servers.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.