您的位置:首页 > 编程语言 > C#

jetway_loves_programming之c#初学——登录界面程序

2011-04-15 21:11 465 查看
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="height: 251px" id="Label1">

<asp:Label ID="Label7" runat="server" Font-Size="XX-Large" ForeColor="Maroon"

Height="35px" style="margin-left: 186px" Text="请登录" Width="103px"></asp:Label>

<br />

<br />

<asp:Label ID="Label2" runat="server" Font-Size="X-Large" ForeColor="#CC00CC"

Height="27px" style="margin-left: 90px" Text="用户:" Width="90px"></asp:Label>

<asp:TextBox ID="TextBox1" runat="server" Height="21px"></asp:TextBox>

<asp:RequiredFieldValidator ID = "RequiredFieldValidator1"

ControlToValidate = "TextBox1"

Display = "Dynamic"

ErrorMessage = "不能为空"

runat = "server" />

<br />

<br />

<asp:Label ID="Label3" runat="server" Font-Size="X-Large" ForeColor="#CC3399"

Height="27px" style="margin-left: 90px" Text="密码:" Width="87px"></asp:Label>

<asp:TextBox ID="TextBox2" runat="server" style="margin-left: 2px"></asp:TextBox>

<asp:RequiredFieldValidator ID = "RequiredFieldValidator2"

ControlToValidate = "TextBox2"

ErrorMessage = "密码不能为空"

runat = "server" />

<br />

<br />

<asp:CheckBox ID="Persist" runat="server" style="margin-left: 172px"

Text="记住密码" Width="120px" oncheckedchanged="Persist_CheckedChanged" />

<br />

<asp:Button ID="Submit1" OnClick="Login_Click" runat="server" Text="登陆"

Font-Names="黑体" Font-Size="X-Large" Height="42px" style="margin-left: 83px"

Width="76px" />

<asp:Label ID="Label4" runat="server" style="margin-left: 35px" Width="200px"></asp:Label>

</div>

</form>

</body>

</html>

Login.aspx.cs文件中,代码如下:

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

public partial class Login : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Login_Click(object sender, EventArgs e)

{

if ((TextBox1.Text == "sa") && (TextBox2.Text == "sa"))

{

FormsAuthentication.RedirectFromLoginPage(TextBox1.Text, Persist.Checked);

}

else{

Label4.Text = "验证不成功,请再试一次";

}

}

}

代码参考如上,测试过应该没什么问题,当然了,之前还要在web.config中写入如下代码:

<authentication mode="Forms">

<forms loginUrl="Login.aspx" name=".aspsafetest">

</forms>

</authentication>

<authorization>

<deny users="?"/>

</authorization>

然后把更改页面的form id,如上文中已经加上了[b]form id="form1"的字样,个人认为这表示的是程序执行时的登录顺序。



[/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: