我有两个带有比较范围验证器的日期文本框。当我选择错误的日期时,我会从验证器获得预期的红旗,但日期文本框不会在红旗警报后使用清除按钮逻辑重置为空。如果有红旗,它不会触及Clear_Click中的断点。请帮忙。
aspx代码:
<asp:TextBox ID="FromDate" runat="server" />
<asp:TextBox ID="ToDate" runat="server" />
<asp:CompareValidator ID="DateCompareValidator" ControlToCompare="FromDate"
ControlToValidate="ToDate" Type="Date" Operator="GreaterThanEqual" ErrorMessage="*Invalid date range - Please check"
runat="server"></asp:CompareValidator>
<asp:Button ID="ClearButton" Text="Clear" runat="server" OnClick="Clear_Click"/>
CodeBehind .cs:
protected void Clear_Click(object sender, EventArgs e)
{
FromDate.Text = String.Empty;
ToDate.Text = String.Empty;
}
答案 0 :(得分:1)
如果您只想清除TextBox,我不会回发到服务器。无论有没有jQuery,我都会使用简单的JavaScript
。例如:
<asp:TextBox ID="FromDate" runat="server" />
<asp:TextBox ID="ToDate" runat="server" />
<asp:CompareValidator ID="DateCompareValidator" ControlToCompare="FromDate"
ControlToValidate="ToDate" Type="Date" Operator="GreaterThanEqual" ErrorMessage="*Invalid date range - Please check"
runat="server"></asp:CompareValidator>
<input type="button" id="clearButton" value="Clear" />
<script>
$(function() {
$( "#clearButton" ).click(function() {
$("#<%= FromDate.ClientID %>").val("");
$("#<%= ToDate.ClientID %>").val("");
});
});
</script>
这是一个非常小的例子(没有asp.net控件):
$(function() {
$( "#clearButton" ).click(function(e) {
e.preventDefault();
$("#FromDate").val("");
$("#ToDate").val("");
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input ID="FromDate" type="text" />
<input ID="ToDate" type="text" />
<input type="button" id="clearButton" value="Clear" />
&#13;
修改强>
Clear_Click
真的被解雇了吗?在我的测试中,每个思考都很完美,请看下面的例子: ASPX:
<asp:Button ID="ClearButton" Text="Clear" runat="server" OnClick="Clear_Click" />
</div>
</form>
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ButtonTest2
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Clear_Click(object sender, EventArgs e)
{
FromDate.Text = String.Empty;
ToDate.Text = String.Empty;
}
}
}
你的代码与我的相似吗?