ASP.NET Component Programming Step By Step
Prior to statement: This article is only suitable for the initial and intermediate programmers of component programming. If you are programmed by the component, you can skip this. Welcome to guide and express comments and suggestions.
The cause of things (purely in order to enhance the fun of articles)
I have never had a cold, recently online, "10-sided ambush" advertisement and comment on the TV, plus Zhang Yimou also got a party in CCTV, specializing in promoting the film, can inevitably move. I didn't go to the cinema to watch the cinema, so if I went online to watch online. When I happened to chat with QQ, a group of members sent a link, saying that I can download "10-sided ambush", I tried it, really, there is still more than 70 K, more than 100 M East is so fast (is it particularly easy to satisfy?). However, the effect is not very good, the color is basically no, like the black and white movie in the past, it is not clear, but the free thing is like this, it will be.
I have seen it for ten minutes, I finally can't see it, I am playing killing, three major characters are playing, no handsome guy is not beautiful, seeing my dizziness straight snoring. In order to prevent it from seeing Zhou Gong, the mouse is uneasy in my hand, I use the player to see this movie just downloaded RealPlay10, this is a first time, I used to use 8.0, the interface has no new version (although I I don't feel good, stupid), so I want to see what function, in the end, I opened the interface below:
Hey, I look at the blue background, "Name" tag and file box combine into a composite control, the background color can also change, a bit creative, the effect is not bad, then I also do it, haha, Sometimes an idea is what is not intentionally generated.
Basic knowledge
Being a composite control in ASP.NET is not very difficult, but if you just get in touch, you may still need to point to the direction, so you may wish to be here, the master is not a misery.
JavaScript: Today's most popular client scripting language, if you want to make special effects, he is most resistant.
CSS: Cascade style sheet for setting up elements.
DHTML: Dynamic HTML, and browser interactions.
System.Web.ui.WebControls.WebControl: The base class of the control in ASP.NET, we may rewrite some of his methods, commonly available:
CreateChildControls (): Give the control to create an opportunity to create an internal stator control, the preset situation onprender () method calls this method to request the control to create its sub-control, if a situation is called by the FindControl () method.
OnPrender (): Triggered the prerender event, happening before drawing the control.
Render (): Draw control.
AddAttributeStorender (): Add HTML style or properties to the tag.
In addition, there is also a method: EnSureChildControls (), which is used to determine if the server control control contains sub-controls. If not included, create sub-controls and work in design phases.
Other slowly look, these are used in our example. final effect
Let's take a look at the effect, so you have a clear goal.
No mouse action mouse moves to the control, the mouse is removed and restored
how about it? What is the effect? (I am boast)
Step by step by step
Now, please listen to my instruction: I like this effect of the left side, I don't like this effect or the right side of the very disdainful station. Ok, please close your browser on the right, please continue your favorite online journey, please come with me on the left:
1. Let's complete the effect section first, that is, write JavaScript scripts. My method is to write an HTML file, the correctness of the test script:
Var Oldcolor;
Function Mouseover (Ctrl, Color)
{
OldColor = Ctrl.style.BackgroundColor;
Ctrl.style.color = "#fffff";
Ctrl.style.backgroundcolor = color;
}
Function Mouseout (Ctrl)
{
Ctrl.style.BackgroundColor = OldColor;
Ctrl.style.color = "# 000000";
}
script>
HEAD>
Name: span> td> | td>
TR> table> Body> Html> Create this text file, copy this code past, then change the extension of the file to .html, open with IE, is there a desired effect? 2, the previous work is ready, then open vs.net IDE, create a new project, select the Visual C # item (Don't ask me to vb.net, I will not), select "Web Control Library" in the list , Enter the project name: LabelTextBox. 3, delete the automatically generated code, leaving only one class frame, like this (namespace self-qualified): 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, the effect of the above is actually a line of two columns, we set the Border property of the table into 0, so you can't see it, where the left cell is put on the label, the right cell is sent a text box, so we want Generate this form with code. The class used is: a) Table: Represents the form b) Tablelow: Represents the table c) TableCell: Indicates cells in the table Note: A table can have multiple tables, and multiple cells can be placed in a table row. The generated code is as follows: / / Define a table object Table t = new table (); // Add a line TableRow Tr = New TableRow (); // Add two cells Tablecell TC1 = New TableCell (); Tablecell TC2 = New TableCell (); // Add the control to the Controls set. 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 also have to respond to the form of mouse to move out of the event, the background color change is triggered here, the following code completes this: // Add mouse event T.attributes.add ("OnMouseover", "Mouseover (this, '" "#" R G B ""); T.attributes.add ("onmouseout", "mouseout (this)"); Attribute indicates the property set of the table, and the add () method is used to add a new property. By the way, put the fonts in the form also set: // Add a style to control the font T.Style.Add ("Font-size", "10PT"); Did you see the three variables above R, G, and B? These three variables are a hexadecimal string representation of a color. Use the following method to decompose the color: // The following will transform the color value into hexadecimal representation 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"); Where _backgroundcolor is a custom property. All code in this step is as follows: Private void createControls () // Create controls and setting controls related properties { // The following will transform the color value into hexadecimal representation String r, g, b; R = (convert.Toint32 (this._backgroundcolor.r)). Tostring ("x"); G = (convert.Toint32 (this._backroundcolor.g)). TOSTRING ("X"); b = (convert.Toint32 (this._backroundcolor.b)). Tostring ("x"); / / Define a table object Table t = new table (); // Add mouse event T.attributes.add ("OnMouseover", "LTMOUSEOVER (this, '" "#" R G B ""); T.attributes.add ("OnMouseout", "LTMOUSEOUT (THIS)"); // Add a style to control the font T.Style.Add ("Font-size", "10PT"); // Add a line TableRow Tr = New TableRow (); // Add two cells Tablecell TC1 = New TableCell (); Tablecell TC2 = New TableCell (); // Add the control to the Controls set. 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, this control provides three properties, and BackgroundColor means that the mouse is moved into the background color of the control, Labelstring is the text of the tag, TextString is 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 moving 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 needs to overwrite the CreateChildControls () method, which is automatically called to generate a child control. Protected Override Void CreateChildControls () { This.ensureChildControls (); // is created if the child control is not created Base.createChildControls (); // Call method CreateControls (); } 7. Save the script of the first step to a constant: Private const string mouse_script = // Used to trigger a script for mouse events |