急问是不是要用到customvalidator?
解决方案 »
- 大家来看一下这样的一个地址,怎么还原成正常的字串???
- cs文件无法识别前台标签
- gridview中的dropdownlist怎么从数据库取这一行的值?
- 请问个位高手一个问题?悬赏(50)找解答
- 如何在aspx文件中直接显示web.config中的值?
- DataGrid怎么返回某一列的值的总和
- 我用Base64编码,把参数加密,但是别人看到了编码后的参数,他是不是还可以自己写个程序,用Base64编码再给解出来?
- 我写了一个公共类,里面有一些公共的函数,我想在其他页面里用这些公共函数,怎么办?
- 请教众微软高手:如何用命令行的方式ReBuild一个asp.net的项目?
- window.open打开的窗口,如何捕捉它的关闭事件?
- 怎样动态产生功能列表?
- 为什么将WEB用户自定义控件添加到工具栏中后,在有的项目中能从工具栏拖到WEB窗口中,在有的项目中却不能拖动到WEB窗口(拖动时鼠标呈现
如果是web form:
1.写客户端js脚本进行验证;
2.写服务器端程序在页面提交的时候验证;
3.也可以自定义CustomValidator进行验证,验证事件的代码与2差不多
Dim Firstdate as Date
i=weekday(today(),7)
FirstDate=today.AddDays(-i)
..............<TD height="42" width=263><asp:textbox id="txtTime" runat="server" Width="250px" MaxLength="100" Columns="50" Height="30px"></asp:textbox></TD>
..............
<asp:RangeValidator ID="rangeValDate" Type="Date" display="Dynamic" ControlToValidate="txttime" ErrorMessage="错误的日期!" MaximumValue=datetime.today
MinimumValue=Firstdate Runat="server"/>报错信息
The value 'datetime.today' of the MaximumValue property of 'rangeValDate' cannot be converted to type 'Date'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: The value 'datetime.today' of the MaximumValue property of 'rangeValDate' cannot be converted to type 'Date'.Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
另用RangeValidator验证日期时间类型要注意:如果在未以编程方式设置应用程序区域性的情况下为 BaseCompareValidator.Type 属性指定 ValidationDataType.Date,则应将非特定区域性的格式(如 YYYY/MM/DD)用于 MaximumValue 和 MinimumValue 属性。否则,可能无法正确解释日期。
MinimumValue=Firstdate.ToString("YYYY/MM/DD");
void ServerValidation (object source, ServerValidateEventArgs arguments)
{
DateTime dt = DateTime.Parse(arguments.Value);
DateTime no = DateTime.Now;
DateTime ol = no.AddDays(-7);
arguments.IsValid = ((dt>ol)&&(dt<no)); }
验证控件:
<asp:CustomValidator id="CustomValidator1"
ControlToValidate="Text1"
OnServerValidate="ServerValidation"
Display="Static"
ErrorMessage="Not an even number!"
ForeColor="green"
Font-Name="verdana"
Font-Size="10pt"
runat="server"/>
const string CookieName="WebdiyersCookie";
void Page_Load(object src,EventArgs e){
if(!Page.IsPostBack)
minDTbox.Text="2003-9-1";
val1.MaximumValue=DateTime.Today.ToString("yyyy/MM/dd");
val1.MinimumValue=(DateTime.Parse(minDTbox.Text)).ToString("yyyy/MM/dd");
}</script>
<HTML><HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="Webdiyer">
</HEAD><BODY>
<form runat="server">
<asp:Label runat="server" id="lbl"/><p>
最小日期:<asp:TextBox runat="server" id="minDTbox" Value="2003-9-1"/>
<asp:Button runat="server" Text="设置最小日期" id="btn1" CausesValidation=false/>
<p>
<asp:TextBox runat="server" id="dtbox"/>
<asp:RequiredFieldValidator runat="server" ErrorMessage="必须输入一个日期值" ControlToValidate="dtbox" Display="dynamic"/>
<asp:CompareValidator runat="server" ErrorMessage="日期格式错误" Operator="DataTypeCheck" Type="Date" ControlToValidate="dtbox" Display="dynamic"/>
<asp:RangeValidator runat="server" id="val1" Type="Date" ErrorMessage="日期太大或太小" ControlToValidate="dtbox" Display="dynamic"/>
<asp:Button runat="server" Text="提交"/>
</form></BODY></HTML>
Line 256: <asp:button id="butAdd" onclick="Add_Click" runat="server" Width="120px" Type="submit" text=" 添加 "></asp:button><BR>
Line 257: <BR>
Line 258: <asp:RangeValidator ID="rangeValDate" Type="Date" display="Dynamic" ControlToValidate="txttime" ErrorMessage="错误的日期!" MaximumValue=DateTime.Today.ToString("yyyy/MM/dd")
Line 259: MinimumValue="2002-10-1" Runat="server"/>
Line 260:
Source File: c:\inetpub\wwwroot\WebApplication3\log.aspx Line: 258
这句要写在后台代码中:
rangeValDate.MaximumValue=DateTime.Today.ToString("yyyy/MM/dd")
string b = string.Format("{0:yyyy/MM/dd}",DateTime.Now.AddDays(1));