原创的用户登录服务器控件[vb.net]
2024-07-10 13:05:29
供稿:网友
自学.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