login.ascx
由于在页面上很频繁使用登陆,把它做成一个控件是很有必要的,下面就是我写的一个简单的登陆控件,大家可以根据的需要完善一下。
1@ Import Namespace="System.Data"
1@ Import Namespace="System.Data.Oledb"
1<script language="VB" runat="server">
2Sub Page_Load(sender As Object,e As EventArgs)
3session("Accessed")=false
4If session("UserName")="" And Not IsPassLogin() then
5lblLogin.visible=true
6lblMessage.visible=false
7IsPassLogin()
8else
9lblMessage.Text=ShowPrompt(True)
10lblMessage.visible=true
11lblLogin.visible=false
12End If
13End Sub
14'用户登陆验证
15Public Function IsPassLogin() As Boolean
16Dim MyConnection As OledbConnection
17Dim strConnection As String="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("DataBase/popmarry.mdb")
18MyConnection=New OledbConnection(strConnection)
19Dim sqlCommand="SELECT * FROM InMember WHERE MemberID='" & UserID & "'" & " AND Pwd='" & UserPassword & "'"
20
21Dim MyAdapter As New OleDbDataAdapter(sqlCommand,MyConnection)
22Dim ds As DataSet=New DataSet()
23MyAdapter.Fill(ds,"InMember")
24Dim dt As DataTable=ds.Tables("InMember")
25If dt.Rows.Count>0 Then
26Session("UserName")=UserID
27Return(true)
28Else
29Return(false)
30End If
31End Function
32
33'显示登陆信息
34Public Function ShowPrompt(bolInfo As Boolean) As String
35If bolInfo Then
36Dim strWelcome As String
37strWelcome="<table width='165' cellpadding='3' border='0' align='center'><tr><td align='center'>欢迎您,<font color='#FF0000'>" & Session("UserName") & "</font></td></tr><tr><td align='center'>"
38strWelcome &="<table width='150' border='0'><tr><td align='center'>状态: <font color='#FF3300'>新婚网会员</font></td></tr></table>"
39strWelcome &="<table width='110' border='0'><tr><td align='center'>控制面板</td><td align='center'><a onclick='return LogoutConfirm()' href='login/logout.aspx'>退出登录</a></td></tr></table></td></tr></table>"
40'返回成功登陆信息
41Return(strWelcome)
42Else
43Dim strError As String
44strError="<table width='130'align='center'><tr><td>登录失败:<br>1.用户名或密码错误<br>2.此帐号被锁定<br>3.未<a href='../register/Register.asp' target='_blank'>注册</td></tr>"
45strError &="<tr><td height='20'><center><a href='javascript:onclick=history.go(-1)'>【返回】</a></center></td></tr></td></tr></table>"
46'返回登陆失败信息
47Return(strError)
48End If
49End Function
50
51Public Property UserID As String
52Get
53UserID=txtUserID.Text
54End Get
55Set
56txtUserID.Text=value
57End Set
58End Property
59
60'定义UserPassword的属性
61Public Property UserPassword As String
62Get
63UserPassword=txtUserPassword.Text
64End Get
65Set
66txtUserPassword.Text=value
67End Set
68End property
69</script>
1<body leftmargin="0" marginheight="0" marginwidth="0" topmargin="0">
2<table width="168">
3<tr>
4<td>
5<asp:label id="lblLogin" runat="server">
6<table height="60" width="168">
7<tr>
8<td align="center" width="63">用户名:</td>
9<td align="left" colspan="2">
10<asp:textbox borderstyle="Groove" height="20" id="txtUserID" runat="server" width="90px"></asp:textbox>
11<asp:requiredfieldvalidator controltovalidate="txtUserID" display="Dynamic" errormessage="<font color=red>*</font>" id="rfdUserID" runat="server"></asp:requiredfieldvalidator>
12</td>
13</tr>
14<tr>
15<td align="center">密 码:</td>
16<td align="left" colspan="2">
17<asp:textbox borderstyle="Groove" height="20" id="txtUserPassword" runat="server" textmode="Password" width="90px"></asp:textbox>
18<asp:requiredfieldvalidator controltovalidate="txtUserPassword" display="Dynamic" errormessage="<font color=red>*</font>" id="rfdPassword" runat="server"></asp:requiredfieldvalidator>
19</td>
20</tr>
21<tr align="center">
22<td colspan="3"><input border="0" height="18" name="image" src="images/form_login.gif" type="image" width="40"/>
23 <img border="0" height="18" src="images/form_reg.gif" width="40"/>
24<img border="0" height="18" src="images/form_lost.gif" width="60"/></td>
25</tr>
26</table>
27</asp:label>
28<asp:label id="lblMessage" runat="server">
29<table width="100%">
30<tr>
31<td> </td>
32</tr>
33</table>
34</asp:label>
35</td>
36</tr>
37</table></body>