服务器|控件|原创
自学.net一年,第一个自己写的用户登录控件
事件CheckedResult,该事件参数e继承自EventArgs,含有用户名,密码,登录合法性(布尔值)
事件EnterEmpty,当用户名、密码文体框为空值时引发
Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data.SqlClient
")> Public Class Login
Inherits System.Web.UI.WebControls.WebControl
Implements IPostBackDataHandler
Public Delegate Sub CheckedEvent(ByVal sender As Object, ByVal e As LoginEvent)
Public Event EnterEmpty As EventHandler
Public Event CheckedResult As CheckedEvent
Private _user As String
Private _pass As String
Private _dataBase As String
Private _dataTable As String
Private _dataBaseUserName As String
Private _dataBasePass As String
Private _server As String
Private _colUser As String
Private _colPass As String
Private _txtUser As String
Private _txtPass As String
Public Property ColUser() As String 属性--用户名字段
Get
Return _colUser
End Get
Set(ByVal value As String)
_colUser = value
End Set
End Property
Public Property ColPass() As String 属性--密码字段
Get
Return _colPass
End Get
Set(ByVal value As String)
_colPass = value
End Set
End Property
Public Property Server() As String 属性--SQL服务器名
Get
Return _server
End Get
Set(ByVal value As String)
_server = value
End Set
End Property
Public Property DataBaseUserName() As String 属性--数据库登录用户
Get
Return _dataBaseUserName
End Get
Set(ByVal value As String)
_dataBaseUserName = value
End Set
End Property
Public Property DataBasePass() As String 属性--数据库登录密码
Get
Return _dataBasePass
End Get
Set(ByVal value As String)
_dataBasePass = value
End Set
End Property
Public Property DataBase() As String 属性--数据库名
Get
Return _dataBase
End Get
Set(ByVal value As String)
_dataBase = value
End Set
End Property
Public Property DataTable() As String 属性--数据表
Get
Return _dataTable
End Get
Set(ByVal value As String)
_dataTable = value
End Set
End Property
Private ReadOnly Property ConnStr() As String 属性--构成连接字符串
Get
Dim _connStr As New Text.StringBuilder()
With _connStr
.Append("server=" & Server & ";")
.Append("Initial catalog=" & DataBase & ";")
.Append("User ID=" & DataBaseUserName & ";")
.Append("PassWord=" & DataBasePass & ";")
End With
Return _connStr.ToString
End Get
End Property
Private Property txtUser() As String 属性--获取用户名文本框值
Get
Return _txtUser
End Get
Set(ByVal value As String)
_txtUser = value
End Set
End Property
Private Property txtPass() As String 属性--获取密码文本框值
Get
Return _txtPass
End Get
Set(ByVal value As String)
_txtPass = value
End Set
End Property
Private Sub CheckInit() 过程--验证连接数据库字符串的完整性
If DataBase = "" Or DataTable = "" Or DataBaseUserName = "" Or DataBasePass = "" Then
Throw New Exception("缺少相应的参数!")
End If
End Sub
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter) 呈现服务器控件
Try
CheckInit()
writer.RenderBeginTag(HtmlTextWriterTag.Table)
writer.RenderBeginTag(HtmlTextWriterTag.Tr)
writer.RenderBeginTag(HtmlTextWriterTag.Td)
writer.Write("用户名:")
writer.RenderEndTag()
writer.RenderBeginTag(HtmlTextWriterTag.Td)
writer.AddAttribute(HtmlTextWriterAttribute.Type, "TextBox")
writer.AddAttribute(HtmlTextWriterAttribute.Name, Me.UniqueID & ":User")
writer.AddAttribute(HtmlTextWriterAttribute.Maxlength, "30")
writer.RenderBeginTag(HtmlTextWriterTag.Input)
writer.RenderEndTag()
writer.RenderEndTag()
writer.RenderEndTag()
writer.RenderBeginTag(HtmlTextWriterTag.Tr)
writer.RenderBeginTag(HtmlTextWriterTag.Td)
writer.Write("密码:")
writer.RenderEndTag()
writer.RenderBeginTag(HtmlTextWriterTag.Td)
writer.AddAttribute(HtmlTextWriterAttribute.Type, "Password")
writer.AddAttribute(HtmlTextWriterAttribute.Name, Me.UniqueID & ":Pass")
writer.AddAttribute(HtmlTextWriterAttribute.Maxlength, "30")
writer.RenderBeginTag(HtmlTextWriterTag.Input)
writer.RenderEndTag()
writer.RenderEndTag()
writer.RenderEndTag()
writer.RenderBeginTag(HtmlTextWriterTag.Tr)
writer.AddAttribute(HtmlTextWriterAttribute.Cols, "2")
writer.RenderBeginTag(HtmlTextWriterTag.Td)
writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit")
writer.AddAttribute(HtmlTextWriterAttribute.Name, Me.UniqueID)
writer.AddAttribute(HtmlTextWriterAttribute.value, "提交")
writer.RenderBeginTag(HtmlTextWriterTag.Input)
writer.RenderEndTag()
writer.RenderEndTag()
; writer.RenderEndTag()
writer.RenderEndTag()
Catch _error As Exception
System.Web.HttpContext.Current.Response.Write("未能完成请求,错误信息如下:" & _error.Message)
Exit Sub
End Try
End Sub
Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NamevalueCollection) As Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData
Dim _txtUserName As String = postCollection(Me.UniqueID & ":User")
Dim _txtPass As String = postCollection(Me.UniqueID & ":Pass")
If _txtUserName = "" Or _txtPass = "" Then
Return True
Else
txtUser = _txtUserName
txtPass = _txtPass
OnCheckMain()
Return False
End If
End Function Public Sub RaisePostDataChangedEvent() Implements System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent
RaiseEvent EnterEmpty(Me, New EventArgs())
End Sub
Private Sub OnCheckMain() 验证用户的合法性,引发CheckedResult事件
Dim _connStr As String = ConnStr
Dim _conn As New SqlConnection(_connStr)
Dim _comm As New SqlCommand()
Dim _dataReader As SqlDataReader
Try
_comm.Connection = _conn
_comm.CommandText = "select * from " & DataTable & " where " & ColUser & "= " & txtUser & " and " & ColPass & "= " & txtPass & " "
_comm.CommandType = CommandType.Text
_conn.Open()
_dataReader = _comm.ExecuteReader(CommandBehavior.CloseConnection)
If _dataReader.Read Then
RaiseEvent CheckedResult(Me, New LoginEvent(txtUser, txtPass, True))
Else
RaiseEvent CheckedResult(Me, New LoginEvent(txtUser, txtPass, False))
End If
_dataReader.Close()
Catch _error As Exception
Throw New Exception(_error.Message)
Finally
If _conn.State = ConnectionState.Open Then _conn.Close()
End Try
End Sub
End Class
---------------------------------------------------------源码2
2004.6.10
Programmer by czclk
自定义事件类LoginEvent,该事件参数e继承自EventArgs,含有用户名,密码,登录合法性(布尔值)
Public Class LoginEvent
Inherits EventArgs
Public Sub New()
End Sub
Public Sub New(ByVal user As String, ByVal pass As String, ByVal result As Boolean)
_userName = user
_userPass = pass
_CheckedPass = result
End Sub
Private _userName As String
Private _userPass As String
Private _CheckedPass As Boolean
Public Property CheckPass() As Boolean
Get
Return _CheckedPass
End Get
Set(ByVal value As Boolean)
_CheckedPass = value
End Set
End Property
Public Property UserName() As String
Get
Return _userName
End Get
Set(ByVal value As String)
_userName = value
End Set
End Property
Public Property UserPass() As String
Get
Return _userPass
End Get
Set(ByVal value As String)
_userPass = value
End Set
End Property
End Class