0
点赞
收藏
分享

微信扫一扫

ASP.NET开发基础(Cookie的简单使用)


ASP.NET.基础入门.ASP.NET开发基础.Cookie的简单使用

  • ​​0 目录​​
  • ​​2 ASP.NET​​
  • ​​2.2 Cookie的简单使用​​
  • ​​2.2.1 题目​​
  • ​​2.2.2 源码​​
  • ​​2.2.1 目录结构​​
  • ​​2.2.2 效果展示​​

  • ​​5 下一章​​

0 目录

2 ASP.NET

2.2 Cookie的简单使用

2.2.1 题目

输入密码,用cookie进行保存方便下次登录使用

2.2.2 源码

Contact.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Contact.aspx.cs" Inherits="WebHomework.Contact1" %>

<!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="formLogin" runat="server">
<div>
<table>
<tr>
<td>
<strong>用户名</strong>
</td>
<td>
<asp:TextBox ID="name" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<strong>密码</strong>
</td>
<td>
<asp:TextBox ID="pwd" runat="server"></asp:TextBox>
</td>

</tr>
<tr>
<td>
<strong>用户身份</strong>
</td>
<td>
<asp:DropDownList ID="identity" runat="server">
<asp:ListItem Selected="True" Value="user">用户</asp:ListItem>
<asp:ListItem Value="admin">管理员</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center;">
<asp:CheckBox ID="remember" runat="server" Text="记住我" OnCheckedChanged="remember_CheckedChanged"/>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center;">
<asp:Button ID="login" runat="server" Text="登录" OnClick="login_Click" />
<asp:Button ID="cancel" runat="server" Text="取消" OnClick="cancel_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

ASP.NET开发基础(Cookie的简单使用)_简单使用

Contact.aspx.cs

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

namespace WebHomework
{
public partial class Contact1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["name"] != null)
{
name.Text = Request.Cookies["name"].Value;
}
if (Request.Cookies["pwd"] != null)
{
pwd.Attributes["value"] = Request.Cookies["pwd"].Value;
}
}

protected void login_Click(object sender, EventArgs e)
{
if (remember.Checked)
{
Response.Cookies["name"].Value = name.Text;
Response.Cookies["pwd"].Value = pwd.Text;
}

string username = name.Text.Trim();
string password = pwd.Text.Trim();
string id = identity.SelectedValue.ToString();

if (username.Equals("123")&&password.Equals("123"))
{
if (id.Equals("admin"))
{

Response.Redirect("admin.aspx");
}
else
{
Response.Redirect("user.aspx");
}
}
else
{
Response.Write("<script>alert('用户名或密码错误!')</script>");
}
}

protected void cancel_Click(object sender, EventArgs e)
{
name.Text = "";
pwd.Attributes["Value"] = "";
if (remember.Checked)
{
remember.Checked=false;
}
}

protected void remember_CheckedChanged(object sender, EventArgs e)
{

}
}
}

2.2.1 目录结构

ASP.NET开发基础(Cookie的简单使用)_web_02

2.2.2 效果展示

ASP.NET开发基础(Cookie的简单使用)_web_03
ASP.NET开发基础(Cookie的简单使用)_cookie_04
ASP.NET开发基础(Cookie的简单使用)_html_05
ASP.NET开发基础(Cookie的简单使用)_web_06
ASP.NET开发基础(Cookie的简单使用)_web_07
ASP.NET开发基础(Cookie的简单使用)_简单使用_08

5 下一章

博客地址:


举报

相关推荐

0 条评论