Web程序设计

发布于:2025-09-02 ⋅ 阅读:(14) ⋅ 点赞:(0)

一、当前在线人数设计

前端代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Application.aspx.cs" Inherits="Application" %>

<!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:Label ID="lblMsg" runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>

后端代码

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

public partial class Application : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMsg.Text = Application["VisistNubmer"].ToString();
    }
}

全局应用程序类代码

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

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        // 在应用程序启动时运行的代码
        Application["VisistNubmer"] = 0;
    }

    void Application_End(object sender, EventArgs e)
    {
        //  在应用程序关闭时运行的代码

    }

    void Application_Error(object sender, EventArgs e)
    {
        // 在出现未处理的错误时运行的代码

    }

    void Session_Start(object sender, EventArgs e)
    {
        // 在新会话启动时运行的代码
        Application.Lock();
        Application["VisistNubmer"] = (int)Application["VisistNubmer"] + 1;
        Application.UnLock();

    }

    void Session_End(object sender, EventArgs e)
    {
        // 在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
        // 或 SQLServer,则不引发该事件。
        Application.Lock();
        Application["VisistNubmer"] = (int)Application["VisistNubmer"] - 1;
        Application.UnLock();

    }

</script>

效果展示

二、页面跳转

登录界面前端代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="pageRedirect.aspx.cs" Inherits="pageRedirect" %>

<!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="txtName" runat="server"></asp:TextBox>
            <asp:RadioButtonList ID="rblUser" runat="server">
                <asp:ListItem Value="Teacher">老师</asp:ListItem>
                <asp:ListItem Value="Student">学生</asp:ListItem>
            </asp:RadioButtonList><br />
            <asp:Button ID="btnSubmit" runat="server" Text="登录" OnClick="btnSubmit_Click" />
        </div>
    </form>
</body>
</html>

登录界面后端代码

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

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

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if(rblUser.SelectedValue == "Teacher")
        {
            Response.Redirect("~/Teacher.aspx?username=" + txtName.Text);
        }else if(rblUser.SelectedValue == "Student")
        {
            Response.Redirect("~/Student.aspx?username=" + txtName.Text);
        }
        else
        {
            Response.Write("<script>alert('请选择身份!');</script>");
        }
    }
}

教师端代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Teacher.aspx.cs" Inherits="Teacher" %>

<!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:Label ID="lblTeacher" runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Teacher : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblTeacher.Text = "你好," + Request.QueryString["username"] + "老师";
    }
}

学生端代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Student.aspx.cs" Inherits="Student" %>

<!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:Label ID="lblStudent" runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Student : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblStudent.Text = "你好," + Request.QueryString["username"] + "同学";
    }
}

三、Cookie

登录界面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="userLogin.aspx.cs" Inherits="userLogin" %>

<!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="txtName" runat="server"></asp:TextBox><br />
            密码:<asp:TextBox ID="txtPwd" runat="server" TextMode="Password"></asp:TextBox><br />
            <asp:Button ID="btnLogin" runat="server" Text="登录" OnClick="btnLogin_Click" />
        </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if(txtName.Text == "user100" && txtPwd.Text == "123456")
        {
            HttpCookie cookie = new HttpCookie("Name");
            cookie.Value = txtName.Text;
            cookie.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(cookie);
            Response.Redirect("~/userIndex.aspx");
        }
        else
        {
            Response.Write("<script>alert('用户名或密码不正确!');</script>");
        }
    }
}

登录成功界面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="userIndex.aspx.cs" Inherits="userIndex" %>

<!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:Label ID="lblUser" runat="server" ></asp:Label>
            <asp:Button ID="btnOut" runat="server" Text="退出" OnClick="btnOut_Click" />
        </div>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class userIndex : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["Name"] != null)
        {
            lblUser.Text = "欢迎你," + Request.Cookies["Name"].Value;
        }
        else
        {
            Response.Redirect("~/userLogin.aspx");
        }
    }

    protected void btnOut_Click(object sender, EventArgs e)
    {
        HttpCookie cookie = new HttpCookie("Name");
        cookie.Value = null;
        cookie.Expires = DateTime.Now.AddDays(-1);
        Response.Cookies.Add(cookie);
        Response.Redirect("~/userLogin.aspx");
    }
}


网站公告

今日签到

点亮在社区的每一天
去签到