C#做的ASP.NET登錄篇
2024-07-10 13:00:07
供稿:網(wǎng)友
一、新建一個數(shù)據(jù)庫
新建一個access數(shù)據(jù)user.mdb。
新建一個user表,添加:userid(文本類型)及password(文本類型)兩個字段。
二、新建一個default.aspx文件。
在web form里:
加入兩個label控件,text屬性分別為“登錄名”和“密碼”;
加入兩個textbox控件,id屬性分別為“userid”和“pwd”,text屬性均為空;
加入兩個requiredfieldvalidato控件,id屬性分別為“rfvuserid”和“rfvpwd”,text屬性分別為“請輸入登錄名!”和“請輸入登錄密碼!”,controltovalidate屬性分別為"userid"和"pwd";
加入一個button控件,id屬性為“l(fā)ogbutton”,text屬性別為“登錄”;
最后加入一個label控件,id屬性為“msg”。
default.aspx源代碼如下:
<%@ page language="c#" codebehind="default.aspx.cs" autoeventwireup="false" inherits="lsj.webform1" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<meta name="generator" content="microsoft visual studio 7.0">
<meta name="code_language" content="c#">
<meta name="vs_defaultclientscript" content="javascript (ecmascript)">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="gridlayout">
<font face="宋體">
<form runat="server" id="form1">
<asp:label id="label1" runat="server" width="55px"
height="26px">登錄名</asp:label>
<asp:label id="label2" runat="server" width="63px" height="24px">密 碼</asp:label>
<asp:textbox id="userid" runat="server" width="109px" height="25px"></asp:textbox>
<asp:textbox id="pwd" runat="server" width="109px" height="22px" textmode="password"></asp:textbox>
<asp:button id="logbutton" runat="server" width="59px" height="25px" text="登 錄"></asp:button>
<asp:label id="msg" runat="server" width="117px" height="26px"></asp:label>
<asp:requiredfieldvalidator id="requiredfieldvalidator1" runat="server" width="162px" height="18px" errormessage="requiredfieldvalidator" controltovalidate="userid">請輸入登錄名!</asp:requiredfieldvalidator>
<asp:requiredfieldvalidator id="requiredfieldvalidator2" runat="server" width="175px" height="22px" errormessage="requiredfieldvalidator" controltovalidate="pwd">請輸入登錄密碼!</asp:requiredfieldvalidator>
</form>
</font>
</body>
</html>
三、編寫default.aspx.cs文件。
雙擊logbutton,
1、加入using system.data.oledb;
2、先在class中聲明:
public string strconnection;
oledbconnection myconn;
3、加入數(shù)據(jù)庫鏈接:
把下面代碼加入“page_init(object sender, eventargs e)”的“initializecomponent();”后面.
string strconnection="provider=microsoft.jet.oledb.4.0;data source="+server.mappath(".")+"..//user.mdb;";
myconn=new oledbconnection(strconnection);
4、在logbutton_click(object sender, system.eventargs e)事件中加入下面的代碼:
string userid,pwd;
userid=userid.text;
pwd=pwd.text;
string mysel="select count(*) as icount from user where userid=""+userid+""";
oledbcommand mycmd1=new oledbcommand(mysel,myconn);
mycmd1.connection.open();
oledbdatareader dr1;
dr1=mycmd1.executereader();
dr1.read();
string count=dr1["icount"].tostring();
dr1.close();
mycmd1.connection.close();
string drpwd,drroles;
if(count!="0")
{
mysel="select * from user where userid=""+userid+""";
oledbcommand mycmd=new oledbcommand(mysel,myconn);
mycmd.connection.open();
oledbdatareader dr;
dr=mycmd.executereader();
dr.read();
drpwd=dr["password"].tostring();
dr.close();
if(drpwd==pwd)
{
session["logid"]=userid;
response.redirect("main.aspx");
}
else
msg.text="登錄密碼錯.";
}
else
msg.text="沒有這個用戶.";
好了,全部工作已經(jīng)完成,default.aspx.cs源代碼如下:
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.data.oledb;
namespace lsj
{
/// <summary>
/// summary description for webform1.
/// </summary>
public class webform1 : system.web.ui.page
{
protected system.web.ui.webcontrols.label label1;
protected system.web.ui.webcontrols.label label2;
protected system.web.ui.webcontrols.textbox userid;
protected system.web.ui.webcontrols.button logbutton;
protected system.web.ui.webcontrols.textbox pwd;
protected system.web.ui.webcontrols.label msg;
protected system.web.ui.htmlcontrols.htmlform form1;
protected system.web.ui.webcontrols.requiredfieldvalidator rfvuserid;
protected system.web.ui.webcontrols.requiredfieldvalidator rfvpwd;
public string strconnection;
oledbconnection myconn;
public webform1()
{
page.init += new system.eventhandler(page_init);
}
private void page_load(object sender, system.eventargs e)
{
}
private void page_init(object sender, eventargs e)
{
initializecomponent();
string strconnection="provider=microsoft.jet.oledb.4.0;data source="+server.mappath(".")+"..//user.mdb;";
//user.mdb放在與aspx文件同一目錄下
myconn=new oledbconnection(strconnection);
}
private void initializecomponent()
{
this.logbutton.click += new system.eventhandler(this.logbutton_click);
this.load += new system.eventhandler(this.page_load);
}
private void logbutton_click(object sender, system.eventargs e)
{
string userid,pwd;
userid=userid.text;
pwd=pwd.text;
string mysel="select count(*) as icount from user where userid=""+userid+""";
oledbcommand mycmd1=new oledbcommand(mysel,myconn);
mycmd1.connection.open();
oledbdatareader dr1;
dr1=mycmd1.executereader();
dr1.read();
string count=dr1["icount"].tostring();
dr1.close();
mycmd1.connection.close();
string drpwd,drroles;
if(count!="0")
{
mysel="select * from user where userid=""+userid+""";
oledbcommand mycmd=new oledbcommand(mysel,myconn);
mycmd.connection.open();
oledbdatareader dr;
dr=mycmd.executereader();
dr.read();
drpwd=dr["password"].tostring();
dr.close();
if(drpwd==pwd)
{
session["logid"]=userid;//新建一個session
response.redirect("main.aspx");
}
else
msg.text="登錄密碼錯.";
}
else
msg.text="沒有這個用戶.";
}
}
}