在控制模糊更新的ValidationSummary列表? [英] Update ValidationSummary list on control blurs?
问题描述
一个的ValidationSummary将显示在回传错误的列表。由于每个字段是固定的,它的验证器被触发,任何验证文本将消失。我想自动更新的ValidationSummary为好。
A ValidationSummary will show a list of errors on postback. As each field is fixed, it's validator is fired, and any validation text will disappear. I want to automatically update the ValidationSummary as well.
以下正常工作:
<asp:TextBox ID="ForenameTextBox" onblur="ValidationSummaryOnSubmit()" runat="server" />
,但这不是理想的,因为它意味着改变,并在所有的字段保持此。 ( ValidationSummaryOnSubmit
是微软的功能。)于是,我就动态地做到这一点:
but this isn't ideal, as it means changing and maintaining this on all fields. (ValidationSummaryOnSubmit
is a Microsoft function.) So I tried to do it dynamically:
addEvent(window, "load", UpdateValidationSummary);
function addEvent(obj, evType, fn) {
if (obj.addEventListener) {
obj.addEventListener(evType, fn, true);
} else {
if (obj.attachEvent) {
var r = obj.attachEvent("on" + evType, fn);
return r;
}
}
}
function removeEvent(obj, evType, fn) {
if (obj.removeEventListener) {
obj.removeEventListener(evType, fn, true);
return true;
} else if (obj.detachEvent) {
var r = obj.detachEvent("on" + evType, fn);
return r;
}
}
function UpdateValidationSummary() {
if (typeof (Page_Validators) == "undefined") {
return;
}
var i, val, ctrl;
for (i = 0; i < Page_Validators.length; i++) {
val = Page_Validators[i];
if (val.controltovalidate != null && val.controltovalidate != "") {
ctrl = document.getElementById(val.controltovalidate);
if (ctrl != null && typeof (ValidationSummaryOnSubmit) == "function") {
//add call to ValidationSummary on blur
addEvent(ctrl, "blur", ValidationSummaryOnSubmit);
}
}
}
}
这不工作,虽然 - 整个的ValidationSummary消失,当一个领域是固定的,而ValidationSummaryOnSubmit功能似乎被调用两次。如果我用一个简单的任务,而不是它的工作原理的addEvent
的功能,但我想,以应付可能已经有一些在的onblur回事领域
事件。
This doesn't work though - the whole ValidationSummary disappears when one field is fixed, and the ValidationSummaryOnSubmit function seems to get called twice. If I use a simple assignment instead of the addEvent
function it works, but I want to cater for fields that might already have something going on in the onBlur
event.
基本上,我想我只需要一个电话到 ValidationSummaryOnSubmit
函数添加到的onblur
的清单处理程序为每个控件。为什么不code以上似乎这样做呢?
Basically I think I just need to add a call to the ValidationSummaryOnSubmit
function to the "list" of onBlur
handlers for each control. Why doesn't the code above seem to do this?
推荐答案
下面是一个服务器端的方式来获得所有文本框的的onblur属性(也应与验证组工作):
Here's a server-side approach to get the onblur attribute on all TextBoxes (which should also work with Validation Groups):
- 创建从文字框派生的类,例如,TextBoxEx
- 添加属性在这个派生类中如
this.Attributes.Add(的onblur的String.Format(ValidationSummaryOnSubmit('{0}'),this.ValidationGroup);
- 使用标签映射,使所有现有的
&LT的; ASP:文本框&GT;
标签仍然可以工作:&LT;&的System.Web GT;
&LT;网页和GT;
&LT; tagMapping&GT;
&LT;添加tagType =System.Web.UI.WebControls.TextBox
mappedTagType =MyControls.TextBoxEx/&GT;
&LT; / tagMapping&GT;
&LT; /页&GT;
&LT; /system.web>
- Create a class derived from TextBox, e.g., TextBoxEx
- Add the attribute in this derived class e.g.,
this.Attributes.Add("onblur", string.Format("ValidationSummaryOnSubmit('{0}')", this.ValidationGroup);
- Use tag mapping so that all of your existing
<asp:TextBox>
tags will still work:<system.web> <pages> <tagMapping> <add tagType="System.Web.UI.WebControls.TextBox" mappedTagType="MyControls.TextBoxEx"/> </tagMapping> </pages> </system.web>
另一种方法是使用 ControlAdapters 添加的属性。
Another way would be to use ControlAdapters to add the attribute.
相关链接:
了解ASP.NET验证
MSDN - tagMapping元素的网页
这篇关于在控制模糊更新的ValidationSummary列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!