zoukankan      html  css  js  c++  java
  • 步步为营-72-asp.net简单练习(通过webForm实现一些简单实例)

    WebForm成功之处在于:实现的代码后置,和asp相比实现了html代码和C#代码分离.但 aspx和aspx.cs之间的强耦合和性能方面(特别是服务器控件)做的不是很好.

    参照步步为营-68完成相同功能的小例子

    1 实现自增

    1.1 通过客户端控件

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="01-自增.aspx.cs" Inherits="_01_小实例._01_自增" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" action="" method="post">
            <input  type="text" name="num" value="<%=Num %> "/>
            <input type="submit" value="自增" />
        </form>
    </body>
    </html>
    aspx
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace _01_小实例
    {
        public partial class _01_自增 : System.Web.UI.Page
        {
            public int Num { get; set; }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (Request["num"]!= null)
                {
                    int num = int.Parse(Request["num"]);
                    num++;
                    Num = num;
                }
            }
        }
    }
    aspx.cs

    1.2 通过服务端控件实现

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="01-自增(服务端控件).aspx.cs" Inherits="_01_小实例._01_自增_服务端控件_" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:TextBox ID="txtNum" runat="server">0</asp:TextBox>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="自增" />
        
        </div>
        </form>
    </body>
    </html>
    aspx
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace _01_小实例
    {
        public partial class _01_自增_服务端控件_ : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                txtNum.Text = (Convert.ToInt32(txtNum.Text) + 1).ToString();
            }
        }
    }
    aspx.cs

    2 实现加法计算器

    2.1 通过客户端控件

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="02-加法计算器.aspx.cs" Inherits="_01_小实例._02_加法计算器" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" method="post" action="">
            <input type="text" name="num1" value="<%=Num1 %>" />
            +
            <input type="text" name="num2" value="<%=Num2 %>" />
            <input type="submit" value="="/>
            <input type="text" name="result" value="<%=Result %>" />
        
        </form>
    </body>
    </html>
    aspx
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace _01_小实例
    {
        public partial class _02_加法计算器 : System.Web.UI.Page
        {
            public int Num1 { get; set; }
            public int Num2 { get; set; }
            public int Result { get; set; }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (String.IsNullOrEmpty(Request["num1"]) || String.IsNullOrEmpty(Request["num2"]))
                {
                    return;
                }
                int num1 = int.Parse(Request["num1"]);
                int num2 = int.Parse(Request["num2"]);
                int result = num1 + num2;
    
                Num1 = num1;
                Num2 = num2;
                Result = result;
            }
        }
    }
    aspx.cs

    2.2 通过服务端控件实现

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="02-加法计算器(服务端控件).aspx.cs" Inherits="_01_小实例._02_加法计算器_服务端控件_" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:TextBox ID="txtNum1" runat="server">0</asp:TextBox>
            +<asp:TextBox ID="txtNum2" runat="server">0</asp:TextBox>
    &nbsp;<asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="=" />
            <asp:TextBox ID="txtResult" runat="server">0</asp:TextBox>
        </form>
    </body>
    </html>
    aspx
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace _01_小实例
    {
        public partial class _02_加法计算器_服务端控件_ : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void btnAdd_Click(object sender, EventArgs e)
            {
                txtResult.Text = (int.Parse(txtNum1.Text) + int.Parse(txtNum2.Text)).ToString();
            }
        }
    }
    aspx.cs

    3 div的自增长

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="03-div的增长.aspx.cs" Inherits="_01_小实例._03_div的增长" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
    </head>
    <body>
    
        <div style="border:solid 1px red; <%=Len%>px;height:<%=Len%>px"">
            <form id="form1" method="post" action="">
                <input type="hidden" name="len" value="<%=Len%>"/>
                <input type="submit" value="" />
            </form>
        </div>
    
    </body>
    </html>
    aspx
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace _01_小实例
    {
        public partial class _03_div的增长 : System.Web.UI.Page
        {
            public int Len { get; set; }
            protected void Page_Load(object sender, EventArgs e)
            {
                int len ;
                if (!string.IsNullOrEmpty(Request["len"]))
                {
                    len  = Convert.ToInt32(Request["len"]) +10;
                }else{
                    len = 50;
                }
                Len = len;
            }
        }
    }
    aspx.cs

  • 相关阅读:
    Html禁止粘贴 复制 剪切
    表单标签
    自构BeanHandler(用BeansUtils)
    spring配置中引入properties
    How Subcontracting Cockpit ME2ON creates SD delivery?
    cascadia code一款很好看的微软字体
    How condition value calculated in sap
    Code in SAP query
    SO Pricing not updated for partial billing items
    Javascript learning
  • 原文地址:https://www.cnblogs.com/YK2012/p/7017526.html
Copyright © 2011-2022 走看看