asp.net 2.0 making a Composite Control sample (ii) [sample code DOWNLOAD]

Source: Internet
Author: User
Tags tostring
Asp.net| Control | sample | download

(i). overview
Demonstrate the production of a composite control by making a < calculator > function Custom Composite Control
(ii). Code
1. Core control implementation code file ComputeControl.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using system.web;
5using System.Web.UI;
6using System.Web.UI.WebControls;
7
8namespace Kingcontrols
9{
Ten public class ComputeControl:System.Web.UI.WebControls.CompositeControl
11 {
12//operand
Private TextBox tb1;
Private TextBox TB2;
15
16//Display Results
Private Label lb;
18
19//Operation (+-*/)
Private Button bt1;
Private Button BT2;
Private Button BT3;
The private Button bt4;
24
protected override void CreateChildControls ()
26 {
TB1 = new TextBox ();
Tb1.id = "TextBox1";
this. Controls.Add (TB1);
30
TB2 = new TextBox ();
Tb2.id = "TextBox2";
this. Controls.Add (TB2);
34
LB = new Label ();
Lb.id = "Label1";
Notoginseng lb. BackColor = System.Drawing.Color.YellowGreen;
Lb. Text = "Result:";
this. Controls.Add (LB);
40
BT1 = new Button ();
Bt1.id = "Button1";
Bt1. Text = "+";
Bt1.commandargument = "+";
Bt1. Click + + new EventHandler (Bt_click);
this. Controls.Add (BT1);
BT2 = new Button ();
Bt2.id = "Button2";
Bt2. Text = "-";
Bt2.commandargument = "-";
Wuyi BT2. Click + + new EventHandler (Bt_click);
this. Controls.Add (BT2);
BT3 = new Button ();
Bt3.id = "Button3";
BT3. Text = "*";
bt3.commandargument = "*";
BT3. Click + + new EventHandler (Bt_click);
this. Controls.Add (BT3);
BT4 = new Button ();
Bt4.id = "Button4";
Bt4. Text = "/";
Bt4.commandargument = "/";
Bt4. Click + + new EventHandler (Bt_click);
The This. Controls.Add (BT4);
65}
66
Bt_click void (object sender, EventArgs e)
68 {
The Try
70 {
The IF ("Result:"!= lb. Text)
72 {
Lb. Text = lb. Text + ",";
74}
Switch ((Button) sender). CommandArgument)
76 {
Case "+": lb. Text = lb. Text + convert.tostring (Convert.ToInt32 (this.tb1.Text) + Convert.ToInt32 (this.tb2.Text)); Break
The case "-": lb. Text = lb. Text + convert.tostring (Convert.ToInt32 (This.tb1.Text)-Convert.ToInt32 (This.tb2.Text)); Break
The case "*": lb. Text = lb. Text + convert.tostring (Convert.ToInt32 (this.tb1.Text) * Convert.ToInt32 (This.tb2.Text)); Break
Case "/": lb. Text = lb. Text + convert.tostring (Convert.ToInt32 (this.tb1.Text)/Convert.ToInt32 (This.tb2.Text)); Break
81}
82}
The catch
84 {
The LB. Text = "It ' s" isn't right format, please input again. "
86}
87}
88
protected override void RenderContents (HtmlTextWriter output)
90 {
Output. Write ("a");
92
93}
94
protected override void Render (HtmlTextWriter writer)
96 {
The writer. RenderBeginTag (htmltextwritertag.table);
98
//operating Item 1
Writer. RenderBeginTag (htmltextwritertag.tr);
Writer. RenderBeginTag (HTMLTEXTWRITERTAG.TD);
102 TB1. RenderControl (writer);
The writer. RenderEndTag ();
Writer. RenderEndTag ();
105
//<br>
Writer. Writebreak ();
108
109//operating Symbol
The writer. RenderBeginTag (htmltextwritertag.tr);
Writer. RenderBeginTag (HTMLTEXTWRITERTAG.TD);
112 writer. Writeencodedtext ("+-*/");
113 writer. RenderEndTag ();
114 writer. RenderEndTag ();
115
116//operating item2
117 writer. RenderBeginTag (htmltextwritertag.tr);
118 writer. RenderBeginTag (HTMLTEXTWRITERTAG.TD);
119 TB2. RenderControl (writer);
Writer. RenderEndTag ();
121 writer. RenderEndTag ();
122
123//operating Symbol
124 writer. RenderBeginTag (htmltextwritertag.tr);
The writer. RenderBeginTag (HTMLTEXTWRITERTAG.TD);
126 writer. Writeencodedtext ("EQUAL");
127 writer. RenderEndTag ();
128 writer. RenderEndTag ();
129
130//the relust Label
131 writer. RenderBeginTag (htmltextwritertag.tr);
132 writer. RenderBeginTag (HTMLTEXTWRITERTAG.TD);
to the LB. RenderControl (writer);
134 writer. RenderEndTag ();
135 writer. RenderEndTag ();
136
137//button1
138 writer. RenderBeginTag (htmltextwritertag.tr);
139 writer. RenderBeginTag (HTMLTEXTWRITERTAG.NOBR);
140 writer. RenderBeginTag (HTMLTEXTWRITERTAG.TD);
The BT1. RenderControl (writer);
Bt2. RenderControl (writer);
143 BT3. RenderControl (writer);
144 BT4. RenderControl (writer);
145 writer. RenderEndTag ();
146 writer. RenderEndTag ();
147
148 writer. RenderEndTag ();
149}
150}
151}
152

2. Front page code usingcompositecontrol.aspx (use method)

1<%@ Page language= "C #" autoeventwireup= true "codefile=" UsingCompositeControl.aspx.cs "inherits=" Usingcompositecontrol "%>
2<%@ Register assembly= "Kingcontrols" namespace= "Kingcontrols" tagprefix= "KSP"%>
3. ...
4<ksp:computecontrol id= "compute1" runat= "Server"/>
5. ...
6

(iii). Sample code Download
Http://www.cnblogs.com/Files/ChengKing/KingControls.rar



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.