. NET control development How to use the code written on the first day in. NET for the next day

Source: Internet
Author: User
Tags tag name

The first day before we see all is JS code, although the framework of the BS is Java or PHP, reusability is particularly high, but it is difficult to write, how to do, we can be more simple?

Of course, we'll be using. NET custom controls at this time.

First we'll review the first day's code (we have an initial change below)

The object-oriented idea used here is to create a New button object var button = function () {              this.control = null;   Property: The button object's own            this.id = null;          The ID of the button object            this.value = null;    The value displayed by the button object            This.click = null;      Click events for Button objects            //followed by initialization, properties of all buttons            this.init = function (ClientId) {                this.id = ClientId;   This is the page passed over the ID                              var button_html = ' <div href= ' # "  id=" Button_ ' +this.id+ ' "  class=" button "> ' + This.value + ' </div> ';  This is the HTML file we want to insert                this.control.after (button_html);                 This.control = $ ("#" + ClientId);  This is done through the ID-bound control  (for which the label needs to be written to the page after the tag is loaded)//binding the fixed-point event to  $ ("#button_" +this.id+). Unbind ("click"). Bind ("click", This.click); This.control.hide (); Hides the current control  . 17 18}

  

First we create a class library control, and then build a controlbase (this class is the parent class of all the control classes)

  Public classControlbase:webcontrol//Inherit WebControl {/// <summary>        ///Loading/// </summary>        /// <param name= "E" ></param>        protected Override voidOnLoad (EventArgs e)//rewrite page load {Base.        OnLoad (e); }        /// <summary>        ///To add a custom control to the specified page, you need to place the IF (!        IsPostBack) {} is not OK. /// </summary>        /// <param name= "page" >page</param>         Public Static voidaddwebcontrolbasetopage (Page page) { for(inti =0; I < page. Controls.Count; i++)            {                if(page. Controls[i] iscontrolbase) {                    return; }} controlbase controlbase=Newcontrolbase (); Page.        Controls.Add (controlbase); }        /// <summary>        ///Registering resource Files/// </summary>        /// <param name= "Strlink" >Resource String</param>        /// <param name= "Control" >Control</param>         Public Static voidRegistersource (stringStrlink, System.Web.UI.Control Control) {            //to ensure that the resource is registered only once, the loop is compared, and there is no added            BOOLFlag =false;  for(Int32 i =0; I < control. Page.Header.Controls.Count; i++) {LiteralControl LC= control. Page.header.controls[i] asLiteralControl; if(LC! =NULL)                {                    if(LC. Text = =strlink) {Flag=true;  Break; }                }            }            if(!flag) {LiteralControl include=NewLiteralControl (Strlink); Control.            PAGE.HEADER.CONTROLS.ADD (include); }        }        /// <summary>        ///Control Add Property/// </summary>        /// <param name= "writer" ></param>        /// <param name= "name" ></param>        /// <param name= "Value" ></param>        protected voidAddattributetocontrol (HtmlTextWriter writer,stringName,stringValue) {            if(!string. IsNullOrEmpty (Value)) {writer.            AddAttribute (Name, Value); }        }    }

Then we are creating a class, called the Button class, as follows:

 public class Button:controlbase {#region Properties//<summary>//Properties Test///</summa            ry> [DefaultValue (""), Description ("Get or set Page ID")] public string FormID {get;        Set }///<summary>///Set page style sheets///</summary> public string Class//These properties will be rendered on the page            out {get;        Set #endregion #region Reload Method///<summary>//Set up loading container///</summary> PR otected override HtmlTextWriterTag Tagkey {get {return htmltextwritertag.div            ; }}///<summary>///control load other operation methods///</summary>//<param name= "E" >& lt;/param> protected override void OnLoad (EventArgs e) {base.        OnLoad (e);     }///<summary>///output control before///</summary>   <param name= "E" ></param> protected override void OnPreRender (EventArgs e) {ba Se.        OnPreRender (e); }///<summary>///output control///</summary>//<param name= "writer" ></param&        Gt protected override void Render (HtmlTextWriter writer) {base.            Render (writer); if (this. Visible) {writer.                WriteLine ("<script type= ' Text/javascript ' >"); Writer. WriteLine ("//<![                Cdata["); string query = this.                ClientID + "_buttongroup"; Writer.                WriteLine ("var" + query + "= new Button ();"); Writer. WriteLine (query + ". Init (' "+ this.                ClientID + "', ' Load Data ')"); Writer.                WriteLine ("//]]>"); Writer.            WriteLine ("</script>"); }}////<summary>///Control Add controls///</summary>//<param name= "Writer" ; </param> protected override void RenderContents (HtmlTextWriter writer) {base.        RenderContents (writer); }///<summary>///Add properties to the control///</summary>//<param name= "writer" ></par am> protected override void AddAttributesToRender (HtmlTextWriter writer) {base.            AddAttributesToRender (writer); Writer. AddAttribute ("Class", "Btn_container" + this.)            Class); if (!string. IsNullOrEmpty (this. FormID)) {writer. AddAttribute ("FormID", this.            FormID);         }} #endregion #region custom method///<summary>//Get Organization Data///</summary> <returns></returns> private void Othermethod () {} #endregion}

How do we get them to the front desk next?

First we'll add a reference to the project:

Then configure it in the configuration file:

As you can see:

namespace represents the address of the namespace where the  referenced  project is added       , such as: MyControl represents the name under the solution, and Control represents the name of the class library
Assembly  represents   the address of the reference you need to add
TagPrefix  means you add a tag name:  

<pages controlrenderingcompatibilityversion= "3.5" clientidmode= "Autoid" validaterequest= "false" >      < controls>        <add namespace= "Mycontrol.control" assembly= "Control" tagprefix= "Basecontrols"/>      < /controls>    </pages>

Then we create a new Web page, regenerate it, and open the Toolbox to see the controls you want.

or enter <basecontrols directly: You'll be able to see the custom controls you're writing.

. NET control development How to use the code written on the first day in. NET for the next day

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.