ASP.NET学习篇(4)——服务器端的控件
2024-07-10 13:11:17
供稿:网友
一、 验证控件
验证控件有以下几种:
requiredfieldvalidator,必须填内容
comparevalidator,同某个常量或另外的控件的值做比较
rangevalidator,范围验证
regularexpressionvalidator,同一个模式做比较,用于email,邮政编码等验证
customvalidator,自定义条件验证
validationsummary,显示验证后的错误信息摘要,,如验证失败,则page对象isvalid属性被设为false,同时,自动显示出错误信息。
验证控件的作用是自动产生客户端验证脚本,否则,在提交回服务器后在服务器端进行验证。验证控件 只对部分控件的特定属性有效:
htmlinputtext value
htmltextarea value
htmlselect value
htmlinputfile value
textbox text
listbox selecteditem
dropdownlist selecteditem
radiobuttonlist selecteditem
。requiredfieldvalidator
。comparevalidator
属性:
controltovalidate 验证的控件
controltocompare 比较的值
operator 比较方法
例:
<asp: comparevalidator id="compare" runat="server"
controltovalidate="password "
controltocompare ="confirmpwd "
operator ="equal"
>
</asp: comparevalidator>
。rangevalidator
属性:
controltovalidate 验证的控件
miniumcontrol 最小值
maxiumcontrol 最大值
例:
<asp: rangevalidator id="range " runat="server"
controltovalidate="age"
miniumcontrol = 15
maxiumcontrol = 100
>
</asp: rangevalidator>
。regularexpressionvalidator
属性:
controltovalidate 验证的控件
validationexpression 匹配模式,用正则表达式(一个连接)
例:
<asp:regularexpressionvalidator id="regular1" runat="server"
controltovalidate="textbox1"
validationexpression="^/d{5}$"
display="static"
font-size="10pt">
邮政编码必须是5个数字
</asp:regularexpressionvalidator>
。customvalidator
属性:
controltovalidate 验证的控件
onservervalidationfunction 服务器端验证函数名称
clientvalidationfunction 客户端验证函数名称
例:
<script language="c#" runat="server">
bool servervalidate(object source,string value){
...
}
</script>
<asp:customvalidator id="customvalidate" runat="server"
controltovalidate="text1"
onservervalidationfunction="servervalidate"
clientvalidationfunction="clientvalidate">
不是一个偶数哦!
</asp:customvalidator>
<script language="javascript">
function clientvalidate(source,value)
{
……
}
</script>
(连接到一个完整的验证程序页面)更多内容可参看:
email地址验证程序
asp.net验证控件
二、 datagrid
下面是用datagrid 显示一个数据表的代码
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.ado" %>
<script language="vb" runat="server">
sub page_load(sender as object, e as eventargs)
dim conn as adoconnection
dim dtcmd as adodatasetcommand
dim ds as dataset
dim str_sql as string
dim str_conn as string
str_sql = "select * from test"
str_conn = "dbq="&server.mappath("test.mdb") &";driver={microsoft access driver (*.mdb)};"
connobj = new adoconnection(str_conn)
dtscmdobj = new adodatasetcommand(str_sql, connobj)
dtsobj = new dataset()
dtscmdobj.filldataset(dtsobj, "test")
dtgrid.datasource = dtsobj.tables("test").defaultview
dtgrid.databind()
end sub
</script>
<html>
<body>
<asp:datagrid id="dtgrid" headerstyle-font- bold="true" tooltip="data grid provided with asp+" runat="server" maintainviewstate="false" />
</body>
</html>
datagrid对象用databind()方法绑定数据源,菜鸟学堂: