在.NET2.0中已经有了登录和注册控件,可以轻松的实现用户登录和注册模块的建设,并且后台能自动生成数据库,建立相应的数据表,极大的方便了用户的开发和管理~在2.0中并且有强大成员角色管理,通过web.config文件能很好的管理用户的访问权限,在这里也就不废话了,进入正题. public partial class WebUserControl : System.Web.UI.UserControl } <%@ Control Language=”C#” AutoEventWireup=”true” CodeFile=”WebUserControl.ascx.cs” Inherits=”WebUserControl” %> 简单的登录页面 <html xmlns=”http://www.w3.org/1999/xhtml” > 登录成功后的页面: public partial class Hello : System.Web.UI.Page 就这样了~我学编程有段时间了,第一次发表文章. 来源:http://www.cnblogs.com/kuite
\n
首先在web.config文件中添加连接数据库的连接字符串
<connectionStrings>
<add name=”kuite” connectionString =”server=localhost;user id=sa;password=123;database=kuite”/>
</connectionStrings>
新建一个用户控件这是后台代码:
using System;
using System.Data;
using System.Data.SqlClient;
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;
\n
{
protected void Page_Load(object sender, EventArgs e)
{
\n
protected void Button1_Click(object sender, EventArgs e)
{
string t1=TextBox1.Text;
string t2=TextBox2.Text;
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["kuite"].ConnectionString;
cmd.Connection = conn;
cmd.CommandText = “select id,psw from customer where id=’” + t1 + “‘ and psw=’” + t2 + “‘”;
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["user"] = t1;
Response.Redirect(“Hello.aspx”);
}
else
{
Label3.Text = “用户名或密码错误”;
}
}
}
\n
用户控件前台代码:
\n
<table>
<tr>
<td style=”height: 26px”>
<asp:Label ID=”Label1″ runat=”server” Text=”用户名”></asp:Label></td>
<td style=”height: 26px”>
<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
</td>
<td style=”height: 26px”>
</td>
</tr>
<tr>
<td>
<asp:Label ID=”Label2″ runat=”server” Text=”密码”></asp:Label>
</td>
<td>
<asp:TextBox ID=”TextBox2″ runat=”server” TextMode=”Password” Width=”154px”></asp:TextBox>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td align=”center”>
<asp:Button ID=”Button1″ runat=”server” Text=”登录” onClick=”Button1_Click” />
</td>
<td>
</td>
</tr>
</table>
<br />
<asp:Label ID=”Label3″ runat=”server”></asp:Label>
\n
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”login.aspx.cs” Inherits=”login” %>
<%@ Register TagPrefix=”uc1″ TagName=”usctr” Src=”~/WebUserControl.ascx” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
\n
<head runat=”server”>
<title>设计家园 http://www.dwww.cn</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<uc1:usctr ID=”usc” runat=”server” />
</div>
</form>
</body>
</html>
\n
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;
\n
{
protected void Page_Load(object sender, EventArgs e)
{
string s1 = (string)Session["user"];
Response.Write(“欢迎光临” + s1);
}
}
\n
学习阶段,技术不深,望广大高手指点.以后会发表更好的文章~
\n