发布网友 发布时间:2022-04-24 03:34
共3个回答
热心网友 时间:2022-05-17 11:04
1、先连接数据库,建一个判断用户名是否存在的类,返回布尔类型的值。简单写一下:select * from 用户表 where username=控件内的值,判断数据表的Rows.count是否>0,返回True或False;2、在登陆的Click事件中写个if条件嵌套语句,第一层判断返回true或者false,第二层判断用户名或密码是否正确,简单写一下:bool result= 判断是否存在;if(result=false){ if(用户名密码正确) { 执行操作; } else { messagebox.show("用户名或密码错误!") } }else{ messagebox.show("用户名已注册!")} 写得很简单,希望对你有所帮助!
热心网友 时间:2022-05-17 12:22
控件你都拖出来了,你在拖个数据源,然后绑定下,废话不啰嗦!
热心网友 时间:2022-05-17 13:56
我以前自己编的,你自己看吧,看不明白再追!
CS页代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string connString = Convert.ToString(ConfigurationManager.ConnectionStrings["001ConnectionString"]);
//001ConnectionString是我在webconfig里配置的数据库连接。
SqlConnection conn = new SqlConnection(connString);
string strsql = "select * from User_table where User_name='" + UserName.Text + "' and Password='" + Password.Text + "'";
SqlCommand cmd = new SqlCommand(strsql, conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.Read())
{
Response.Redirect("index.aspx");
conn.Close();
}
else
{
FailureText.Text = "登陆失败,请检查登陆信息!";
conn.Close();
Response.Write("<script language=javascript>alert('登陆失败!.');</script>");
}
}
protected void Button2_Click(object sender, EventArgs e) //文本框重置按钮
{
UserName.Text = "";
Password.Text = "";
}
}
下面是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">
<asp:Panel ID="Panel1" runat="server" Height="101px" Width="231px" Wrap="False">
<table>
<tr>
<td align="center" colspan="2">
用户登陆</td>
</tr>
<tr>
<td style="width: px">
用户名:</td>
<td style="width: 100px">
<asp:TextBox ID="UserName" runat="server" Wrap="False"></asp:TextBox></td>
</tr>
<tr>
<td style="width: px">
密码:</td>
<td style="width: 100px">
<asp:TextBox ID="Password" runat="server" TextMode="Password" Width="148px" Wrap="False" ></asp:TextBox></td>
</tr>
<tr>
<td align="center" colspan="2" style="text-align: center">
<asp:Button ID="Button1" runat="server" Text="登陆" Width="50px" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="重置" Width="50px" OnClick="Button2_Click" /></td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Label ID="FailureText" runat="server" Width="77px"></asp:Label></td>
</tr>
</table>
</asp:Panel>
</form>
</body>
</html>