ASP.NET学习笔记(一)编写简单网站页面


》1《 找出数组中的最大值和最小值


先设计一下页面像这个样子:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int maxNum, minNum;
        int []number = new int[8]{21, 23, 54, 63, 7, 6, 87, 333};
        maxNum = minNum = number[0];
        /*
            foreach(object in set){
         *      // TODO
         * }
         */
        foreach (int x in number) { 
            if (x > maxNum) {
                maxNum = x;
            }
            if (x < minNum) {
                minNum = x;
            }
        }
        TextBox1.Text = "最大元素是" + maxNum.ToString() + ",最小元素是" + minNum.ToString();
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
}



》2《 数组元素排序


直接在网页代码中嵌入C#代码:

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
        int[] array = new int[] {21,4,5,3,1,2,34,54,67,123};
        /*
         Array.Sort(ArrayName, StartSort, EndSort);
         */
        Array.Sort(array, 0, array.Length);
        foreach (int i in array) {
            TextBox1.Text = TextBox1.Text + i.ToString() + " ";
        }
    }
</script>

<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:Button ID="Button1" runat="server" OnClick="Button1_Click" style="height: 21px" Text="Button" />
        <asp:TextBox ID="TextBox1" runat="server" Width="480px"></asp:TextBox>
    
    </div>
    </form>
</body>
</html>

运行效果:




》3《 新建网站文件,在设计视图中输入三行文字,分别是用户登录、用户名、密码、在用户名和密码后面分别添加一个文本框TextBox1、TextBox2,继续添加两个按钮,并将其text属性改为“确定”、“取消”。


随即得到代码:

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "admin" )
        {
            Response.Write("Administrator logined");
        }else if(TextBox1.Text == "firedom"){
            Response.Write("Hello, firedom~!");
        }else{
            Response.Write("Require all denied");
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {

    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">
        .auto-style1 {
            text-align: center;
        }
        #Password1 {
            width: 130px;
        }
    </style>
</head>
<body style="text-align: center">
    <form id="form1" runat="server">
    <div class="auto-style1">
    
        <h1 class="auto-style1">
    
        <br />
        <br />
        FIREDOM网站登陆页面</h1>
        <div class="auto-style1">
            <br />
        用户名:<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="TextBox1_TextChanged" Width="130px"></asp:TextBox>
            <br />
  密码:<input id="Password1" type="password" /><br />
        
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="确定" />
  
        <asp:Button ID="Button2" runat="server" Text="取消" OnClick="Button2_Click" />
    
        </div>
    
    </div>
    </form>
</body>
</html>

执行效果:





》4《 随机点名系统


设计以下代码:

<%@ Page Language="C#" %>
<%@ Import Namespace ="System.Threading" %>

<!DOCTYPE html>

<script runat="server">


    String[] className = new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "11", "12", "13", "14", "15", "16", "17",  "18", "19", "20",
            "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
            "31", "32" };
    Random rnd = new Random();
    protected void Button1_Click(object sender, EventArgs e)
    {
       
            int n = rnd.Next(0, 31);
            Label2.Text = className[n];
           // System.Threading.Thread.Sleep(50);
            
        
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">
        .auto-style1 {
            text-align: center;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <h1 class="auto-style1">
            <asp:Label ID="Label1" runat="server" style="font-size: 80pt" Text="计科12点名系统"></asp:Label>
        </h1>
        <p class="auto-style1">
            <asp:Label ID="Label2" runat="server" style="font-size: 80pt; font-weight: 700;" Text="press start"></asp:Label>
        </p>
        <p class="auto-style1">
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="开始抽奖" />
        </p>
    
    </div>
    </form>
</body>
</html>

执行效果:



OVER

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值