[原]动态创建Web控件制做计算器

       最近参加了Web基础开发的培训,收获不少,做了一个练习,在后台动态创建控件制作了一个简单功能的计算器。程序中控件创建好以后,往往不能放在想要的位置,前台的布局非常麻烦,我用Table、TableRow、TableCell对象来帮助前台页面的布局。在创建控件前,将要创建的控件分类,按照分类,用重载的方法自己定义了CreateBtn函数。然后用循环调用的方法进行创建。整个程序的代码如下:
前台HTML代码(VS2005自动生成的,我没做任何修改);

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"  %>
None.gif
None.gif
< html  xmlns ="http://www.w3.org/1999/xhtml"   >
None.gif
< head  runat ="server" >
None.gif    
< title > 网页动态生成计算器 </ title >
None.gif
</ head >
None.gif
< body >
None.gif    
< form  id ="form1"  runat ="server" >
None.gif    
< div >
None.gif    
None.gif    
</ div >
None.gif    
</ form >
None.gif
</ body >
None.gif
</ html >
None.gif

后台Asp.net代码:

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
public  partial  class  _Default : System.Web.UI.Page 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//定义两个操作数
InBlock.gif
    static double Num1;
InBlock.gif    
static double Num2;
InBlock.gif    
//记录操作符
InBlock.gif
    static string OperatorF;
InBlock.gif    
//判断“=”是否被点击过,每次操作时清空上次操作的记录
InBlock.gif
    static bool ReOperator = false;
InBlock.gif    TextBox txtShow 
= new TextBox();
InBlock.gif   
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 在页面登录事件里面创建计算器的界面
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif    
/// <param name="e"></param>

InBlock.gif 
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//定义表格用于容纳其它创建的控件
InBlock.gif
        Table txtTB = new Table();
InBlock.gif        form1.Controls.Add(txtTB);
InBlock.gif        txtShow.Width 
= 110;
InBlock.gif        TableRow TR 
= new TableRow();
InBlock.gif        TableCell TC 
= new TableCell();
InBlock.gif        TC.Controls.Add(txtShow);
InBlock.gif        TR.Controls.Add(TC);
InBlock.gif        txtTB.Controls.Add(TR);
InBlock.gif        Table TB 
= new Table();
InBlock.gif        form1.Controls.Add(TB);
InBlock.gif        
//循环创建有规律分布的数字控件,和操作符控件
InBlock.gif
        for (int i = 0,I = 1; i < 3; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//用两个数组记录操作符控件的Text和ID值
ExpandedSubBlockStart.gifContractedSubBlock.gif
            string[] Op = dot.gif{"+","-","*"};
ExpandedSubBlockStart.gifContractedSubBlock.gif            
string[] CharOp = dot.gif{"jia","jian","cheng"};
InBlock.gif            TableRow TRows 
= new TableRow();
InBlock.gif            TB.Controls.Add(TRows);
InBlock.gif            
for (int j = 0; j < 3; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                TableCell Tcell 
= new TableCell(); 
InBlock.gif                Tcell.Controls.Add(CreateBtn(I.ToString()));
InBlock.gif                I
++;
InBlock.gif                TRows.Controls.Add(Tcell);
ExpandedSubBlockEnd.gif            }

InBlock.gif            TableCell TcellO 
= new TableCell();
InBlock.gif            Button OperBtn 
= new Button();
InBlock.gif            OperBtn.Text 
= Op[i];
InBlock.gif            OperBtn.ID 
= CharOp[i];
InBlock.gif            OperBtn.Width 
= 25;
InBlock.gif            OperBtn.Click 
+= new EventHandler(Btn_Click);
InBlock.gif            TcellO.Controls.Add(OperBtn);
InBlock.gif            TRows.Controls.Add(TcellO);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        TableRow TRow 
= new TableRow();
InBlock.gif        TB.Controls.Add(TRow);
InBlock.gif        TableCell Tc2 
= new TableCell();
InBlock.gif        Tc2.Controls.Add(CreateBtn(
"Backspace","B"));
InBlock.gif        TRow.Controls.Add(Tc2);
InBlock.gif
InBlock.gif        TableCell Tc3 
= new TableCell();
InBlock.gif        Tc3.Controls.Add(CreateBtn(
"Clear","C"));
InBlock.gif        TRow.Controls.Add(Tc3);
InBlock.gif
InBlock.gif        TableCell Tc1 
= new TableCell();
InBlock.gif        Tc1.Controls.Add(CreateBtn(
"deng""="));
InBlock.gif        TRow.Controls.Add(Tc1);
InBlock.gif
InBlock.gif        TableCell Tc 
= new TableCell();
InBlock.gif        Tc.Controls.Add(CreateBtn(
"chu""/"));
InBlock.gif        TRow.Controls.Add(Tc);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 创建操作数按钮的函数
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="Num">操作数</param>
ExpandedSubBlockEnd.gif    
/// <returns>返回一个按钮</returns>

InBlock.gif    protected Button CreateBtn(string Num)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Button NumBtn 
= new Button();
InBlock.gif        NumBtn.Text 
= Num;
InBlock.gif        NumBtn.ID 
= "ID" + Num;
InBlock.gif        NumBtn.Width 
= 25;
InBlock.gif        NumBtn.Click 
+= new EventHandler(Btn_Click);
InBlock.gif        
return NumBtn;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 创建操作符按钮的函数
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="ID">按钮的ID号</param>
InBlock.gif    
/// <param name="Operator">操作符</param>
ExpandedSubBlockEnd.gif    
/// <returns>返回一个按钮</returns>

InBlock.gif    private Button CreateBtn(string ID, string Operator)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Button OperBtn 
= new Button();
InBlock.gif        OperBtn.Text 
= Operator;
InBlock.gif        OperBtn.ID 
= ID;
InBlock.gif        OperBtn.Width 
= 25;
InBlock.gif        OperBtn.Click 
+= new EventHandler(Btn_Click);
InBlock.gif        
return OperBtn;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 定义按钮响应的事件,所有按钮通用一个事件,用按钮的Text属性区分要执行的操作
InBlock.gif    
/// </summary>
InBlock.gif    
/// <param name="sender"></param>
ExpandedSubBlockEnd.gif    
/// <param name="e"></param>

InBlock.gif    private void Btn_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
string BtnText = ((Button)sender).Text;
InBlock.gif        
switch(BtnText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
case "+":
InBlock.gif                Num1 
= int.Parse(txtShow.Text);
InBlock.gif                OperatorF 
= BtnText;
InBlock.gif                txtShow.Text 
= "";
InBlock.gif                
break;
InBlock.gif            
case "-":
InBlock.gif                Num1 
= int.Parse(txtShow.Text);
InBlock.gif                OperatorF 
= BtnText;
InBlock.gif                txtShow.Text 
= "";
InBlock.gif                
break;
InBlock.gif            
case "*":
InBlock.gif                Num1 
= int.Parse(txtShow.Text);
InBlock.gif                OperatorF 
= BtnText;
InBlock.gif                txtShow.Text 
= "";
InBlock.gif                
break;
InBlock.gif            
case "/":
InBlock.gif                Num1 
= int.Parse(txtShow.Text);
InBlock.gif                OperatorF 
= BtnText;
InBlock.gif                txtShow.Text 
= "";
InBlock.gif                
break;
InBlock.gif            
case "=":
InBlock.gif                
if (OperatorF == "+")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Num2 
= Num1 + double.Parse(txtShow.Text);
InBlock.gif                    txtShow.Text 
= Num2.ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (OperatorF == "-")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Num2 
= Num1 - double.Parse(txtShow.Text);
InBlock.gif                    txtShow.Text 
= Num2.ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (OperatorF == "*")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Num2 
= Num1 * double.Parse(txtShow.Text);
InBlock.gif                    txtShow.Text 
= Num2.ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (OperatorF == "/")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Num2 
= Num1 / double.Parse(txtShow.Text);
InBlock.gif                    txtShow.Text 
= Num2.ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                ReOperator 
= true;
InBlock.gif                
break;
InBlock.gif            
case "C":
InBlock.gif                txtShow.Text 
= "";
InBlock.gif                Num1 
= Num2 = 0;
InBlock.gif                
break;
InBlock.gif            
case "B":
InBlock.gif                
if (txtShow.Text.Length >=1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    txtShow.Text 
= txtShow.Text.Substring(0, txtShow.Text.Length - 1);
InBlock.gif                    
if (txtShow.Text != "")
InBlock.gif                        Num1 
= int.Parse(txtShow.Text);
InBlock.gif                    
else
InBlock.gif                        Num1 
= 0;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
break;
InBlock.gif            
default:
InBlock.gif                
if (ReOperator)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    txtShow.Text 
= "";
InBlock.gif                    Num1 
= Num2 = 0;
InBlock.gif                    ReOperator 
= false;
ExpandedSubBlockEnd.gif                }

InBlock.gif                txtShow.Text 
+= BtnText;
InBlock.gif                
break;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }
 
ExpandedBlockEnd.gif}

None.gif


整个程序的运行结果如下:
                        

可以将这个计算器做成一个自定义Web控件,代码的改动不大,以后有机会了再讲,程序中做的计算器的功能还不完整,有的地方控制还不够,广大网友有兴趣还可以自己完善扩展。

转载于:https://www.cnblogs.com/salonliudong/archive/2007/05/19/752322.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值