zoukankan      html  css  js  c++  java
  • 十天学会ASP.Net——(4)

    4.容器与控件动态添加:(只讲了实例:网络版计算器的实现)

    演示效果:

    clip_image002

    页面设计:

    <asp:Panel ID="Panel1" runat="server">

    <asp:Literal ID="Literal1" runat="server"></asp:Literal>

    </asp:Panel>

    后台代码:

    protected void Page_Load(object sender, EventArgs e)
        {
            TextBox tb1 = new TextBox();		//先实例化一个新控件对象
            tb1.ID = "myTextBox";				//控件是通过ID来唯一标志的。
            tb1.Text = "我是动态添加上来的";
            Panel1.Controls.Add(tb1);			//往Panel容器的控件列表中塞入新控件
    		//至此,新控件已经添加成功了,只需要以上三行重点代码。
            Literal l1 = new Literal();
            l1.ID = "literal";
            l1.Text = "<br/>";
            Panel1.Controls.Add(l1);
    		/*上面添加的这个Literal很重要!,它用来在网页上保留显示文本的位置。如果想要创建可以编程方式操纵的文本元素,可以将Literal控件的Text属性设置为要发送到Web页面的Html内容,以创建你想要的HTML内容。使用Label亦可,不过Label控件背包装在<span>标记内,Literal不添加<span>使代码更简单,但是意味着你无法将样式应用于Literal控件的内容。这意味着在Web窗体设计器处于网络模式使,Literal控件无法定位,因此Literal不适合创建标题*/

    然后网页面上添加一些按钮

    Button[] numBtn = new Button[11];
            for (int i = 0; i < 11; i++)
            {
                numBtn[i] = new Button();
                numBtn[i].ID = "numBtn" + i;
                numBtn[i].Text = i.ToString();
                numBtn[i].Height = 50;
                numBtn[i].Width = 50;
                numBtn[i].OnClientClick = "fun1";
                numBtn[i].Click += numBtn_Click;
                Panel1.Controls.Add(numBtn[i]);
            }

    其它添加的按钮步骤略。。

    设置左值和右值为静态变量:

        private static Double left = double.MinValue;
        private static Double right = double.MinValue;

    点击数字按钮的触发动作

        protected void numBtn_Click(object sender, EventArgs e)
    {
    	//注意怎样找到动态添加的文本框这个出发事件的按钮。
            TextBox tb = (TextBox)FindControl("myTextBox");
            if (tb!=null)
            {
    			//sender(事件发送者)就是这个出发事件的按钮。
    			//通过强制类型转换获得。
                Button numBtn = (Button)sender;
                tb.Text += numBtn.Text;
            }
        }

    点击运算符+:

        protected void optBtn1_Click(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)FindControl("myTextBox");
            if (left == double.MinValue)
                left = Double.Parse(tb.Text);
            tb.Text = "";
    		//这里我用1代表加法,在后面会用到
            Application["opt"] = "1";
        }

    单击OK后的动作

        protected void optBtn0_Click(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)FindControl("myTextBox");
            right = Int32.Parse(tb.Text);
            if (left == Int32.MinValue)
                Response.Write("左操作数错误");
            switch (Int32.Parse(Application["opt"].ToString()))
            {
                case 1:
                    left += right;
                    break;
                case 2:
                    left -= right;
                    break;
                case 3:
                    left *= right;
                    break;
                case 4:
                    left /= right;
                    break;
                default:
                    break;
            }
            tb.Text = left.ToString();
        }
  • 相关阅读:
    poj 1328 Radar Installation (贪心)
    hdu 2037 今年暑假不AC (贪心)
    poj 2965 The Pilots Brothers' refrigerator (dfs)
    poj 1753 Flip Game (dfs)
    hdu 2838 Cow Sorting (树状数组)
    hdu 1058 Humble Numbers (DP)
    hdu 1069 Monkey and Banana (DP)
    hdu 1087 Super Jumping! Jumping! Jumping! (DP)
    必须知道的.NET FrameWork
    使用记事本+CSC编译程序
  • 原文地址:https://www.cnblogs.com/shenerguang/p/2511051.html
Copyright © 2011-2022 走看看