asp.net登陆验证怎么实现呢
要做个登陆界面它跟数据库表关联如果用户名不存在显示用户不存在如果密码不对提示密码错误这个功能怎么实现?代码怎么写?搞不来用C#语言...
要做个登陆界面它跟数据库表关联如果用户名不存在显示用户不存在如果密码不对提示密码错误这个功能怎么实现?代码怎么写?搞不来 用C#语言
展开
3个回答
2013-08-22
展开全部
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("用户名已注册!")} 写得很简单,希望对你有所帮助!
广东轻亿云软件
2024-05-14 广告
2024-05-14 广告
广东轻亿云软件科技有限公司在软件开发领域深耕多年,积累了丰富的经验和技术实力。我们深知API接口在现代软件开发中的重要性,因此,我们与多家业界领先的API接口提供商保持着紧密的合作关系,确保我们的产品和服务能够充分利用这些接口,为用户提供更...
点击进入详情页
本回答由广东轻亿云软件提供
2013-08-22
展开全部
控件你都拖出来了,你在拖个数据源,然后绑定下,废话不啰嗦!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-22
展开全部
我以前自己编的,你自己看吧,看不明白再追!
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: 89px">
用户名:</td>
<td style="width: 100px">
<asp:TextBox ID="UserName" runat="server" Wrap="False"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 89px">
密码:</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>
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: 89px">
用户名:</td>
<td style="width: 100px">
<asp:TextBox ID="UserName" runat="server" Wrap="False"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 89px">
密码:</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>
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询