CustomValidator Javascript函数认为单选按钮没有被选中
我有一个用户控件,其中包含一系列单选按钮和一些文本字段。如果选中特定单选按钮,我想验证两个文本字段的内容。
我的控件标记包含以下内容:
<asp:TextBox ID="FromDate" runat="server" Columns="8"></asp:TextBox>
<asp:TextBox ID="ToDate" runat="server" Columns="8"></asp:TextBox>
<asp:RadioButton runat="Server" ID="RadioBetween" GroupName="Date" CssClass="date_group_options_control_radio"/>
<asp:CustomValidator ID="DateValidator" runat="server" Display="Dynamic" ClientValidationFunction="ValidateDateFields_Client" OnServerValidate="ValidateDateFields"></asp:CustomValidator>
<script type="text/javascript">
function ValidateDateFields_Client(source, args)
{
debugger;
var bRadioBetweenSelected = false;
var oRadio = document.getElementById('<%=RadioBetween.ClientID%>');
if (oRadio != null && (oRadio.checked == true || oRadio["checked"] == true))
{
bRadioBetweenSelected = true;
}
if (bRadioBetweenSelected)
{
var oFromDate = document.getElementById('<%=FromDate.ClientID%>');
var oToDate = document.getElementById('<%=ToDate.ClientID%>');
if (oFromDate != null && oToDate != null)
{
var sFromDate = oFromDate.value;
var sToDate = oToDate.value;
source.innerHTML = ValidateFromToDate(sFromDate, sToDate, args);
}
else
{
args.IsValid = true;
}
}
else
{
args.IsValid = true;
}
}
</script>
ValidateFromToDate 仅检查值并确保它们正常。
但它永远不会进入检查,因为我似乎无法判断 RadioBetween 是否被检查。然而,服务器端代码运行良好(其作用完全相同)。
如果我在控件中手动将 Checked 设置为“true”,它将按预期工作。
我如何验证该客户端以保存到服务器的行程?我做错了什么?这应该是相当简单的代码:-)
I have a user control which contains a series of radio buttons and some text fields on it. If a specific radio button is checked I want to validate the contents of two text fields.
My control markup contains this:
<asp:TextBox ID="FromDate" runat="server" Columns="8"></asp:TextBox>
<asp:TextBox ID="ToDate" runat="server" Columns="8"></asp:TextBox>
<asp:RadioButton runat="Server" ID="RadioBetween" GroupName="Date" CssClass="date_group_options_control_radio"/>
<asp:CustomValidator ID="DateValidator" runat="server" Display="Dynamic" ClientValidationFunction="ValidateDateFields_Client" OnServerValidate="ValidateDateFields"></asp:CustomValidator>
<script type="text/javascript">
function ValidateDateFields_Client(source, args)
{
debugger;
var bRadioBetweenSelected = false;
var oRadio = document.getElementById('<%=RadioBetween.ClientID%>');
if (oRadio != null && (oRadio.checked == true || oRadio["checked"] == true))
{
bRadioBetweenSelected = true;
}
if (bRadioBetweenSelected)
{
var oFromDate = document.getElementById('<%=FromDate.ClientID%>');
var oToDate = document.getElementById('<%=ToDate.ClientID%>');
if (oFromDate != null && oToDate != null)
{
var sFromDate = oFromDate.value;
var sToDate = oToDate.value;
source.innerHTML = ValidateFromToDate(sFromDate, sToDate, args);
}
else
{
args.IsValid = true;
}
}
else
{
args.IsValid = true;
}
}
</script>
ValidateFromToDate just checks the values and makes sure that they are sane.
It never goes into the check though because I can't seem to tell whether RadioBetween is checked or not. The server side code, however, works fine (which does exactly the same thing).
If I manually set Checked to be 'true' in the control it works as expected.
How can I validate this client side to save a trip to the server? What am I doing wrong? This should be fairly trivial code :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
目前尚不清楚为什么使用 RadioButton。
通常有多个具有相同 GroupName 的 RadioButton。
也许您只需要一个复选框?
It is not clear why you use the RadioButton.
Usually there are more than one RadioButtons with same GroupName.
Maybe you just need a CheckBox?
您可以借助 FireBug 或其他 JavaScript 调试工具来定位问题。将断点放入函数中,并查看每个执行步骤的变量中插入了哪些值。
You can localize problem with the help of FireBug or other JavaScript debugging tool. Put breakpoint(s) into your function and see what values are inserted into variables on each execution step.
Javascript 获取或设置选中的单选值
Javascript Get or Set Checked Radio Value
事实证明,这是因为我在一个页面中有同一控件的两个版本。隐藏的(在模式对话框的深处)与主要的冲突。在隐藏控件中的控件上调用客户端验证函数两次。
为什么会这样还有待观察,但是当注释掉控件的第二个实例时,一切都工作正常。
ASP.NET 每天都让我感到困惑:-)
这里讨论原因: 使用客户端+服务器端自定义验证进行用户控制;选择了错误的客户端验证器
Turns out that this is because I have two versions of the same control in one page. The hidden one (deep in the depths of a modal dialog) is conflicting with the main one. The client validation function is being called twice on the controls in the hidden control.
Why this is remains to be seen but when commenting out the second instance of the control it all works fine.
Every day ASP.NET confuses me :-)
The reason is discussed here: User Control with Client + Server Side CustomValidation; Wrong Client side validator is picked