嘿..我想将当前日期与用户输入的日期进行比较..但是,到目前为止我遇到了错误..
我试过这样的事情:<asp:TextBox id="txtDate1" runat="server" />
<asp:CompareValidator runat="server" ErrorMessage="The date must be greater than today"
ControlToValidate="txtDate1" type="date"
ValuetoCompare="DateTime.Today.ToShortDateString()" />
我收到一条错误消息,指出“{1}}属性DateTime.Today.ToShortDateString()
的值无法转换为'date'类型
我也试过ValueToCompare
,我得到了同样的错误信息。
请帮助我,我非常感激。
答案 0 :(得分:17)
您只是将ValueToCompare
属性用作文字字符串。如果要执行代码以获取动态值,则需要在其中使用ASP标记。试试这个:
<asp:comparevalidator runat="server"
errormessage="The date must be greater than today"
controltovalidate="txtDate1" type="date"
valuetocompare="<%# DateTime.Today.ToShortDateString() %>" />
然后在Page_Load
方法中,请致电Page.DataBind()
。
这将在加载页面时执行数据手册代码,并将值放在引号之间。
答案 1 :(得分:6)
<asp:CompareValidator ID="CompareValidator3" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Date should be on or after today"
Operator="GreaterThanEqual" Type="Date">
</asp:CompareValidator>
在页面加载事件中,将验证器的值设置为
进行比较CompareValidator3.ValueToCompare = DateTime.Now.ToShortDateString();
答案 2 :(得分:1)
我们可以在代码中设置ValueToCompare
if (!Page.IsPostBack)
{
string currentDate = DateTime.Today.ToShortDateString();
Comparevalidator1.ValueToCompare = currentDate;
}
用于比较验证器:
<asp:CompareValidator ID="Comparevalidator1" runat="server" ErrorMessage="The date must be greater than today"
Operator="GreaterThan" ControlToValidate="txtDate1" Type="date" Display="Dynamic" />
为什么不使用Page.DataBind?
请考虑以下情形。我只需要点击Action按钮就可以显示gridview。数据源以声明方式定义。但是,如果我使用Page.DataBind(),它甚至会在页面加载时显示网格。
<form id="form1" runat="server">
<asp:TextBox ID="txtDate1" CssClass="firstBox" runat="server" Text=""></asp:TextBox>
<asp:CompareValidator ID="Comparevalidator1" runat="server" ErrorMessage="The date must be greater than today"
Operator="GreaterThan" ControlToValidate="txtDate1" Type="date" Display="Dynamic" />
<asp:Button ID="btnAction" class="submitButton" runat="server" Text="Action" OnClick="btnAction_Click" />
<asp:Button ID="btnDummy" class="submitButton" runat="server" Text="Dummy" OnClick="btnDummy_Click" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" DataSource="<%# EmployeesResult %>">
</asp:GridView>
</form>
背后的代码
public partial class ThirdTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Page.DataBind();
if (!Page.IsPostBack)
{
string currentDate = DateTime.Today.ToShortDateString();
txtDate1.Text = currentDate;
Comparevalidator1.ValueToCompare = currentDate;
}
}
protected void btnAction_Click(object sender, EventArgs e)
{
GridView1.DataBind();
string value = GridView1.DataSource.ToString();
}
protected void btnDummy_Click(object sender, EventArgs e)
{
}
//Propertry
public List<Employee> EmployeesResult
{
get
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee { EmpID = 1, EmpName = "Emp1" });
employees.Add(new Employee { EmpID = 2, EmpName = "Emp2" });
return employees;
}
}
}
答案 3 :(得分:0)
试试这个。
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Date is required" ControlToValidate="txtmDate"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Date is not valid (MM.DD.YYYY)" ControlToValidate="txtDate" ValidationExpression="(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d" ></asp:RegularExpressionValidator>
用户输入日期(日期格式)的方式也很重要。在这里,我使用了MM.DD.YYYY日期格式。
<asp:CompareValidator ID="CompareValidatorGreaterThanToday" runat="server" ErrorMessage="The date must be greater than today" ControlToValidate="txtDate" Type="date" Operator="GreaterThan" ValueToCompare="<%# DateTime.Today.ToShortDateString() %>" ></asp:CompareValidator>
然后在你的Page_Load方法(* .aspx.cs)中,调用Page.DataBind()。
示例:
protected void Page_Load(object sender, EventArgs e)
{
Page.DataBind();
}
答案 4 :(得分:-1)
尝试下面写的:
<asp:CompareValidator runat="server" ErrorMessage="The date must be greater than today"
ControlToValidate="txtDate1" type="DateTime"
ValuetoCompare='<%# DateTime.Now.ToString("d") '%> />