原創(chuàng)的用戶登錄服務(wù)器控件[vb.net]
2024-07-10 13:05:29
供稿:網(wǎng)友
自學(xué).net一年,第一個自己寫的用戶登錄控件
事件checkedresult,該事件參數(shù)e繼承自eventargs,含有用戶名,密碼,登錄合法性(布爾值)
事件enterempty,當(dāng)用戶名、密碼文體框?yàn)榭罩禃r引發(fā)
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服務(wù)器名
get
return _server
end get
set(byval value as string)
_server = value
end set
end property
public property databaseusername() as string 屬性--數(shù)據(jù)庫登錄用戶
get
return _databaseusername
end get
set(byval value as string)
_databaseusername = value
end set
end property
public property databasepass() as string 屬性--數(shù)據(jù)庫登錄密碼
get
return _databasepass
end get
set(byval value as string)
_databasepass = value
end set
end property
public property database() as string 屬性--數(shù)據(jù)庫名
get
return _database
end get
set(byval value as string)
_database = value
end set
end property
public property datatable() as string 屬性--數(shù)據(jù)表
get
return _datatable
end get
set(byval value as string)
_datatable = value
end set
end property
private readonly property connstr() as string 屬性--構(gòu)成連接字符串
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() 過程--驗(yàn)證連接數(shù)據(jù)庫字符串的完整性
if database = "" or datatable = "" or databaseusername = "" or databasepass = "" then
throw new exception("缺少相應(yīng)的參數(shù)!")
end if
end sub
protected overrides sub render(byval writer as system.web.ui.htmltextwriter) 呈現(xiàn)服務(wù)器控件
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() 驗(yàn)證用戶的合法性,引發(fā)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,該事件參數(shù)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