asp.net 统计登录的用户数

操作题设计包含一个包含Login.aspxWelcome.aspx两个页面的网站。要求用户登录界面如下图,输入制定的用户名和密码后才能打开Welcome.aspx页面,此时页面中显示用户名、欢迎信息以及是第几位登录用户。


asp.net 统计登录的用户数_第1张图片asp.net 统计登录的用户数_第2张图片

方法一:使用image控件来显示图片:

1.Login.aspx.cs

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

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["username"] = "mr";
        Session["password"] = "mrsoft";
        Application["count"] = 0;
    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        //Session["flag"] = "true";
        //接收来自文本框的用户名和密码
        String name = TextBox1.Text;
        String pw = TextBox2.Text;
        if (name == "mr" && pw == "mrsoft")
        {            
            Response.Redirect("Welcome.aspx");
        }
        else
        {
            Response.Write("<script>alert('用户名或密码有误');</script>");
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
    }
}
2.Welcome.aspx.cs

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

public partial class Welcome : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        Application["count"] = Convert.ToInt32(Application["count"]) + 1; 
<span style="white-space:pre">	int s=Convert.ToInt32(Application["count"]);</span>    
            switch (s)
            {
                case 1:
                    Image1.ImageUrl = "~/picture/1.jpg";
                    Image2.Visible = false;//隐藏第二张图片
                    break;
                case 2:
                    Image1.ImageUrl = "~/picture/2.jpg";
                    Image2.Visible = false;
                    break;
                case 3:
                    Image1.ImageUrl = "~/picture/3.jpg";
                    Image2.Visible = false;
                    break;
                case 4:
                    Image1.ImageUrl = "~/picture/4.jpg";
                    Image2.Visible = false;
                    break;
                case 5:
                    Image1.ImageUrl = "~/picture/5.jpg";
                    Image2.Visible = false;
                    break;
                case 6:
                    Image1.ImageUrl = "~/picture/6.jpg";
                    Image2.Visible = false;
                    break;
                case 7:
                    Image1.ImageUrl = "~/picture/7.jpg";
                    Image2.Visible = false;
                    break;
                case 8:
                    Image1.ImageUrl = "~/picture/8.jpg";
                    Image2.Visible = false;
                    break;
                case 9:
                    Image1.ImageUrl = "~/picture/9.jpg";
                    Image2.Visible = false;
                    break;
                case 10:
                    Image1.ImageUrl = "~/picture/1.jpg";
                    Image2.ImageUrl = "~/picture/0.jpg";
                    break;
            }           
        }     
 }    

3.Login.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="color: #008000">
    
           用户登录<br />
        用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>(<%=Session["username"]%>)
        <br />
        密码:  
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>(<%=Session["password"]%>)
        <br />    
        <asp:Button ID="Button1" runat="server" Text="登录" OnClick="Button1_Click1" />
        <asp:Button ID="Button2" runat="server" Text="取消" OnClick="Button2_Click" />    
    </div>
    </form>
</body>
</html>

4.Welcome.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        欢迎用户<%=Session["username"]%>登录本系统<br/>
        您是本网站第<asp:Image ID="Image1" runat="server" />位登录的用户</div>       
    </form>
</body>
</html>


方法二:使用代码显示图片

在Welcome.aspx.cs中加入如下的方法,并在前台页面调用。

public void showPic (){
        String s = Convert.ToString(Application["count"]);
        String img = " ";
        for (int i = 0; i < s.Length; i++) {
            img += "<img src='"+"picture/"+s[i]+".jpg'>";
        }
        Response.Write(img);
    }        
调用方式

<%showPic();%>


注意:本实验中使用application创建的变量模拟用户登录,注意image属性的应用,注意显示图片的方法。






你可能感兴趣的:(asp.net)