一、 验证控件
验证控件有以下几种:
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()方法绑定数据源,