原創的使用者登入伺服器控制項[vb.net]

來源:互聯網
上載者:User
伺服器|控制項|原創 自學.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


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。