Programming ASP.NET components step by step

  Programming ASP.NET components step by step 

  Prior statement: This article only for the components of interest programming early, intermediate programmers, if you are programming components cattle X, you can skip here.    Welcome you guidance and made comments and suggestions. 

  It began (solely for the purpose of enhancing the interesting article) 

  I have not the film cold, the recent Internet, television, "House of Flying Daggers", the advertising and comment on day-to flutter, and in Zhang Yimou's CCTV also conducted a concert devoted to the film inevitably also ready to make trouble together.    I did not go to the cinema habits, so if there are online on the Internet to read.    QQ chat happens on the 1st, a group of members, a link that can be downloaded from the "House of Flying Daggers", I tried to, really, good speed, the speed of more than 70 K, and more than 100 M East East soon on the next end (I was not particularly easy to meet?).    But effect is not very good, basically there is no color, black-and-white film, as in the past, is still not clear, but what it is so free, and what will you. 

  Read 10 minutes, and finally Kanbuxiaqu, Dadashasha are three main characters are out and there is no hunk nor beauty, I see dizziness straight snooze.    In order to prevent see Duke of Zhou, mouse, the unrest in my hands, I use the players to see this movie just downloaded the realplay10, see this is the first time, had previously been using 8.0, the new version of the interface did not look good (although I do not feel that good-looking, Benben), I would like to see what functions Finally, I opened the following interfaces: 

  Xi, I saw that the blue background of very interesting ah, "the name" label and documents combined into a composite frame controls can also change the background color, a little creativity, the effect is good, then I have done a ah, You Sometimes an idea is the so unintentionally. 

  Basic knowledge 

  ASP.net done in a composite control is not difficult, but if just contact, it may still need some guidance direction, so here may wish to 1,10 Suo, experts have Moguai. 

  Javascript: today's most popular client scripting language, to make his effect to the most capability. 

  CSS: cascading style sheet to set the elements of style. 

  DHTML: Dynamic HTML, and interactive browser will be used. 

  System.Web.UI.WebControls.WebControl: ASP.NET Zidingxikongjian in the base class, we might be rewriting some of his methods commonly used are: 

  CreateChildControls (): controls for the control of designated to create opportunities, by default OnPreRender () method call this method will require the creation of its control of controls, it is not a situation from the FindControl () method call. 

  OnPreRender (): trigger PreRender events occurred prior to the drawing controls. 

  Render (): Drawing control. 

  AddAttributesToRender (): add HTML tags to form or attributes. 

  In addition, there is a way: EnsureChildControls (), the method used to determine whether the server controls-controls of, if not contained, then create sub-controls, play a role in the design stage. 

  Others look at you slowly, which is our number will be used in the examples. 

  Final results 

  First look at the effect of such hearts have clear objectives. 

  No mouse cursor to move the mouse out of control after Reduction 

  How?    Ting-hyun effect?    (I claim it) 

  With me step by step 

  Now, please listen to my instructions: Like this effect left the station, and do not like the results very disdain or the right station.    Well, the right friends please close your browser to your favorite on-line trip, left with a friend invited me to: 

  1, we first complete results of the javascript is prepared script.    My method is to write an HTML document, test the correctness of the script: 

<HTML>

<HEAD>

  <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"> 

  <TITLE> </ TITLE> 

  <script Language=javascript> 

  Var OldColor; 

  Function mouseover (ctrl, color) 

  ( 

  OldColor = ctrl.style.backgroundColor; 

  Ctrl.style.color = "# ffffff"; 

  Ctrl.style.backgroundColor = color; 

  ) 

  Function mouseout (ctrl) 

  ( 

  Ctrl.style.backgroundColor = OldColor; 

  Ctrl.style.color = "# 000000"; 

  ) 

  </ Script> 

  </ HEAD> 

<BODY>

  <table Border="0" style="font-size:9pt" onmouseover="mouseover(this,'#ff0044')" onmouseout="mouseout(this)"> 

  <tr> 

  <td> <span> Name: </ span> </ td> <td> <input name="_ctl2" type="text" /> </ td> 

  </ Tr> 

  </ Table> 

  </ BODY> 

  </ HTML> 

  Create a text file, copy this code will be the last, and then changed the name of the expansion of the paper. Html, opened with IE is not seen the desired effect? 

  2, ready to complete the preparatory work, the next open VS.NET IDE, a new project, select Visual C # (Do not ask me to VB.net code, I will not), on the right list, select "Control WEB the "importation Project Name: LabelTextBox. 

  3, the automatic generation of code deleted, leaving only one kind of framework, such as the following (from the namespace): 

  Using System; 

  Using System.Web.UI; 

  Using System.Web.UI.WebControls; 

  Using System.ComponentModel; 

  Using System.Drawing; 

  Namespace LzhTextBpx 

  ( 

  Public class LabelTextbox: System.Web.UI.WebControls.WebControl 

  ( 

  ) 

  ) 

  4, see above is a fact, the effect of his two forms, we have forms of the border attribute set to 0, so do not come out, which left the cell label release, the right cells release a text box, so we have to code generation using this form.    Use the class: 

  A) Table: that forms 

  B) TableRow: that forms trip 

  C) TableCell: that forms in the cells 

  NOTE: a table can have multiple forms line, a line forms can be placed in multiple cell 

  Generated code as follows: 

  / / Definition of a table object 

  Table t = new Table (); 

  / / Add the line 

  TableRow tr = new TableRow (); 

  / / Add two cells 

  TableCell tc1 = new TableCell (); 

  TableCell tc2 = new TableCell (); 

  / / Will be added to the controls on Controls. 

  Tc1.Controls.Add (label); 

  Tc2.Controls.Add (textBox); 

  Tr.Controls.Add (tc1); 

  Tr.Controls.Add (tc2); 

  T.Controls.Add (tr); 

  This.Controls.Add (t); 

  We would also like to respond to the mouse moved out of form, the background color change is the trigger here, the following code completion of the matter: 

  / / Add the mouse incident 

  T.Attributes.Add ( "onmouseover", "mouseover (this, '" + "#" + R + G + B + "')"); 

  T.Attributes.Add ( "onmouseout", "mouseout (this)"); 

  Attributes that forms the attribute set, Add () method used to add a new attribute. 

  Incidentally the form of the font are installed to: 

  / / Add form, used to control fonts 

  T.Style.Add ( "font-size", "10pt"); 

  See above for R, G, B three variables?    These three variables is a color that hexadecimal string.    Use the following method to color decomposition: 

  / / The following will be color into hexadecimal 

  String R, G, B; 

  R = (Convert.ToInt32 (this._backgroundColor.R)). ToString ( "X"); 

  G = (Convert.ToInt32 (this._backgroundColor.G)). ToString ( "X"); 

  B = (Convert.ToInt32 (this._backgroundColor.B)). ToString ( "X"); 

  _ BackgroundColor which is the custom attributes. 

  This step all the code below: 

  Private void CreateControls () / / create controls and the creation of the relevant attribute controls 

  ( 

  / / The following will be color into hexadecimal 

  String R, G, B; 

  R = (Convert.ToInt32 (this._backgroundColor.R)). ToString ( "X"); 

  G = (Convert.ToInt32 (this._backgroundColor.G)). ToString ( "X"); 

  B = (Convert.ToInt32 (this._backgroundColor.B)). ToString ( "X"); 

  / / Definition of a table object 

  Table t = new Table (); 

  / / Add the mouse incident 

  T.Attributes.Add ( "onmouseover", "ltmouseover (this, '" + "#" + R + G + B + "')"); 

  T.Attributes.Add ( "onmouseout", "ltmouseout (this)"); 

  / / Add form, used to control fonts 

  T.Style.Add ( "font-size", "10pt"); 

  / / Add the line 

  TableRow tr = new TableRow (); 

  / / Add two cells 

  TableCell tc1 = new TableCell (); 

  TableCell tc2 = new TableCell (); 

  / / Will be added to the controls on Controls. 

  Tc1.Controls.Add (label); 

  Tc2.Controls.Add (textBox); 

  Tr.Controls.Add (tc1); 

  Tr.Controls.Add (tc2); 

  T.Controls.Add (tr); 

  This.Controls.Add (t); 

  ) 

  5, outside the control provides three attributes, backgroundColor that controls the mouse into the background color, text labels for labelString, textString content for the text box.    Defined as follows: 

  Private Label label = new Label ();// create a label 

  Private TextBox textBox = new TextBox ();// create a text box 

  Private Color _backgroundColor; / / mouse into the background color 

  Public Color backgroundColor 

  ( 

  Get 

  ( 

  If (_backgroundColor == Color.Empty) 

  Return Color.Blue; 

  Return _backgroundColor; 

  ) 

  Set 

  ( 

  _backgroundColor = Value; 

  ) 

  ) 

  Public string labelString 

  ( 

  Get 

  ( 

  Return label.Text; 

  ) 

  Set 

  ( 

  Label.Text = value; 

  ) 

  ) 

  Public string textString 

  ( 

  Get 

  ( 

  Return textBox.Text; 

  ) 

  Set 

  ( 

  TextBox.Text = value; 

  ) 

  ) 

  6, this step will need to rewrite CreateChildControls () method, the method will be automatically called, used to generate sub-control. 

  Protected override void CreateChildControls () 

  ( 

  This.EnsureChildControls ();// of control if not create, create 

  Base.CreateChildControls ();// call methods 

  CreateControls (); 

  ) 

  7, the first step would be to keep a constant script: 

  Private const string MOUSE_SCRIPT = / / mouse events to trigger scripts 

  "<script Language=javascript> \ n" + 

  "Var OldColor; \ n" + 

  "Function ltmouseover (ctrl, color) \ n" + / / When the mouse moved, called the method, ctrl to form for the color to change the color value 

  "(\ N" + 

  "OldColor = ctrl.style.backgroundColor; \ n" + / / record of the original document background color 

  "Ctrl.style.color = '# ffffff'; \ n" + / / font color to white 

  "Ctrl.style.backgroundColor = color; \ n" + / / background color changes form 

  ") \ N" + 

  "Function ltmouseout (ctrl) \ n" + / / called when the mouse moved out, the parameters Ibid. 

  "(\ N" + 

  "Ctrl.style.backgroundColor = OldColor; \ n" + / / restore background color 

  "Ctrl.style.color = '# 000000';" + / / font color will be reduced to black 

  ") \ N" + 

  "</ Script> \ n"; 

  Rewriting OnPreRender () method of the script output to the browser, taking into account the same page may be the control over the situation at this time does not need controls are generated for each section of the script, but all share control This script, which is why we use Page.IsClientScriptBlockRegistered () to determine whether the output of the script, if the output does not need to export. 

  Protected override void OnPreRender (EventArgs e) 

  ( 

  / / Output to the script page. 

  If (! Page.IsClientScriptBlockRegistered ( "mousescript")) / / output to prevent duplication. 

  ( 

  Page.RegisterClientScriptBlock ( "mousescript" MOUSE_SCRIPT); 

  ) 

  Base.OnPreRender (e); 

  ) 

  The use of controls 

  Use the controls obviously much simpler than creating controls, the controls below to talk about the use of (for beginners): 

  After compiling the project will generate a procedure called LabelTextbox.dll set. 

  1, the creation of a testing program, open toolbox, right click and select "Add / Remove items", as the following diagram: 

  2, the pop-up "custom toolbox," choice. "Net FramWork components" tab, click "view" in the project directory to find LabelTextBox "LabelTextbox.dll" procedures set Figure: 

  3, determined, in which the controls in the toolbox icon, as the following diagram: 

  4, the direct control WEB form until ready to use. 

  This procedure debugging environment: Windows2000 Server, MS.NET2003. 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Tags: ,

Releated Articles


0 Comments to “Programming ASP.NET components step by step”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.