Realize the generation of Designtimehtml with Render () of the control itself

xiaoxiao2021-03-06  41

Continue

http: // http://www.cnblogs.com/piglikefly/posts/40707.aspx

In ControlDesigner, Designtimehtml's GET Process

First come to a simple Control

Using

System;

Using

System.Web.ui;

Using

System.Web.ui.design;

Using

System.Web.ui.WebControls;

Using

System.drawing;

Using

System.drawing.design;

Using

System.componentmodel;

Namespace

Flying.pig.WebControl

{[Designer ( "Flying.Pig.WebControl.SimpleControlDesigner"),] public class SimpleControl: Control {protected override void Render (HtmlTextWriter writer) {base.Render (writer); // set breakpoints process monitors}} public class SimpleControlDesigner: ControlDesigner {public override string getdesigntimehtml () {return base.getdesigntimeHtml (); // Process monitoring time setting breakpoint}}}

Obviously, this Control can't do anything. The purpose of writing it is to track system.web.ui.design.controlDesigner.getdesigntimehtml () what is done. Compile -> Put it to the toolbox -> Start another IDE-> Create a web application -> Back to the control IDE-> Startup Process Monitor -> Set the breakpoint (breakpoint location) -> Back to Web Application IDE-> Throw SimpleControl into WebForm1 and then what did you find? Yes, yes, DesignTimeHtml's Get process: First, IDE check SimpleControlDesigner of GetDesignTimeHtml (), then according to SimpleControlDesigner.GetDesignTimeHtml () instructions, call base.GetDesignTimeHtml (), which is System.Web.UI.Design.ControlDesigner of GetDesignTimeHtml ( So what did ControlDesigner getDesigntimehtml () do? It turns out that it is the same as the run (there is still some difference, I will explain in detail later), I have made a htmlTextWriter, request SimpleControl.Render () to generate HTML to display in the IDE. At this point, we can find that in addition to their own line of patchwork, we can also generate the render () of the control itself to generate DesigntimeHTML.

Abandoning getDesigntimehtml () now is now doing a getDesigntimehtml () method, or even ControlDesigner is not available, but can provide a rich and flexible WebControl of DesignTime

Using

System;

USINGSYSTEM.WEB.UI;

Using

System.Web.ui.design;

Using

System.Web.ui.WebControls;

Using

System.drawing;

Using

System.drawing.design;

Using

System.componentmodel;

Namespace

Flying.pig.webcontrols

{Public class SimpleControl: WebControl {private Label label; public SimpleControl () {Text = "SimpleControl";} public string Text {get {EnsureChildControls (); return label.Text;} set {EnsureChildControls (); label.Text = value }} Protected override void createchildControls () {Controls.clear (); label = new label (); controls.add (label); base.createchildControls ();}}}

Below is the design and running of SimpleControl,

Design:

The properties of SimpleControl in the figure are set to

<

CC1: SIMPLECONTROL

id

= "SimpleControl2"

Runat = "Server" Style

= "Z-index: 101; Left: 32px; position: absolute; top: 32px"

TEXT

= "My SimpleControl"

Font-size

= "10.5pt"

Width

= "120px"

HEIGHT

= "128px"

Backcolor

= "LINEN"

Borderwidth

= "1px"

Font-bold

= "True"

>

CC1: SIMPLECONTROL

>

running result:

The style handling of sub-controls will come to a slightly complex point control CAPTIONTEXT.

Using

System;

Using

System.Web.ui;

Using

System.Web.ui.design;

Using

System.Web.ui.WebControls;

Using

System.drawing;

Using

System.drawing.design;

Using

System.componentmodel;

Namespace

Flying.pig.webcontrols

{Public class CaptionTextBox: WebControl {private Label label; private TextBox textBox; private Table table; public CaptionTextBox () {Caption = "Caption"; Text = "Text";} public string Caption {get {EnsureChildControls (); return label. Text;} set {EnsureChildControls (); label.Text = value;}} public string Text {get {EnsureChildControls (); return textBox.Text;} set {EnsureChildControls (); textBox.Text = value;}} / ** Pre-treatment before ////

/// render. /// The pre-processing of this control is to apply the control style to the child control.

/// private void decols (); // Reset a child control style table.controlstyle.reset (); textbox.controlstyle.reset (); label.controlstyle.reset (); table. ApplyStyle (ControlStyle); // for label applications and only the control text font style textBox.Font.MergeWith (ControlStyle.Font); label.Font.MergeWith (ControlStyle.Font);} protected override void CreateChildControls () {Controls.Clear (); Label = new label (); TextBox = new textbox (); table = new table (); tablelow row = new tableerow (); row.cells.add (new Tablecell ()); row.cells.add ( New TableCell ()); rontrols.add (label); rontrols.add (need); Table.Rows.add (row); controls.Rows.add (row); controls.Rows.Add (Row); Base.createchildControls ();} protected override void onpre Render (Eventargs E) {doprender (); base.onprender (e);} protected override void render (htmlTextwriter Writer) {if (page! = Null && page.site! = Null && page.site.designmode) {// Since the render process under DesignMode does not call OnPreender, you need to process properties such as style. DOPRERENDER ();} Base.Render (Writer);}}} When designing: The properties of the controls in the figure are

<

CC1: CAPTIONTEXTBOX

id

= "CAPTIONTEXTBOX1"

Style

= "Z-index: 102; Left: 16px; position: absolute; top: 16px"

Runat

= "Server"

Width

= "320px"

CAPTION

= "Name:"

TEXT

= "Zhu Ai Fei"

Font-size

= "10.5pt"

Backcolor

= "Peachpuff"

HEIGHT

= "72px"

>

CC1: CAPTIONTEXTBOX

>

Runtime effect

CAPTIONTEXT Demo In design mode, the style handling of the nested sub-control, see the render and doprender methods in the code.

Today is the first time in blog, plus I originally thinking that it is not clear, so it is very chaotic. Forn to forgive me.

转载请注明原文地址:https://www.9cbs.com/read-116360.html

New Post(0)