All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP技巧:PHP中时间和日期函数总结_[PHP教程]
PHP安全《PHP Security》_[PHP教程]
掌握AJAX之AJAX通讯技术简介_[AJAX教程]
C#,3.0新特性初步研究,Part4:使用集合类型初始化器_[Asp.Net教程]
数据仓库的概念_mssql学习_编程技术
ASP.NET Remoting体系结构(五)
同步两个Sql,server(1)_[SQL,Server教程]
.NET,命名规范(控件、数据类型等)_[Asp.Net教程]
SqlServer教程:SqlServer中各数据类型的长度、精度_[SQL Server教程]
c#中的错误处理
Reading number is top 10 pictures
Men don't mature ten sign
HongMenYan premiere XinLiangGong clairvoyant outfit PK YiFeiLiu1
The money of more than 100 countries and regions20
美女挤公交
Plump, too plump!1
谁认识这位校花
China telecom 114 spokesman MeiYanXu2
好身材能把衣服穿出3D效果
湖边的风景
看如何给单纯的少女洗脑
Download software ranking
豪门浪荡史
Boxer's Top ten classic battle8
The cock of the Grosvenor LTD handsome
Take off clothes to survival
电车之狼R
Eclipse 4.2.2 For Win64
C++编程教程第三版
Such love down(擒爱记)
The Bermuda triangle2
Boxer's Top ten classic battle9
delv published in(发表于) 2014/1/23 3:13:55 Edit(编辑)
利用C#来做ASP.NET的登陆页面_[Asp.Net教程]

利用C#来做ASP.NET的登陆页面_[Asp.Net教程]

利用C#来做ASP.NET的登陆页面_[Asp.Net教程]

一、新建一个数据库
  新建一个access数据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属性为“LogButton”,Text属性别为“登录”;
  最后加入一个Label控件,ID属性为“Msg”。
  Default.aspx源代码如下:
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="lsj.WebForm1" %>













Height="26px">登录名
密 码




请输入登录名!
请输入登录密码!





三、编写default.aspx.cs文件。
  双击LogButton,
  1、加入using System.Data.OleDb;
  2、先在class中声明:
  public string strConnection;
  OleDbConnection myConn;
  3、加入数据库链接:
  把下面代码加入“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="没有这个用户.";



  好了,全部工作已经完成,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 description for WebForm1.
 ///

 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="没有这个用户.";
  }
 }
}







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.