如何为所有 ASP.Net 验证器换肤?
我希望 ASP.Net 3.5 网站中的所有验证器的 CssClass 值为“error”。 我最初的想法是在主题皮肤中执行此操作,如下所示:
<asp:CompareValidator runat="server"
CssClass="error" />
<asp:CustomValidator runat="server"
CssClass="error" />
<asp:RequiredFieldValidator runat="server"
CssClass="error" />
<belCommon:ZipCodeValidator runat="server"
CssClass="error" />
<belCommon:PhoneNumberValidator runat="server"
CssClass="error" />
这只是我使用的验证器的部分列表。 理想情况下,我想做这样的事情:
<asp:BaseValidator runat="server"
CssClass="error" />
并且因为所有验证器都继承自 BaseValidator,所以我希望这会起作用,但事实并非如此。 有没有办法在不将每个验证器控件显式添加到皮肤的情况下实现此目的?
更新:
我发现了一种使用 javascript 的不同方法:
Sys.Application.add_init(function(sender, args)
{
if (Page_Validators != null)
{
for (i = 0; i < Page_Validators.length; i++)
{
Page_Validators[i].className = "error";
}
}
});
ASP.Net 生成一个名为 Page_Validators 的 javascript 变量,它是验证范围的数组。 该脚本检查它是否存在,然后循环并设置类名。 我将其添加到母版页中并且到目前为止它的工作原理。
I want all of the validators in an ASP.Net 3.5 website to have a CssClass value of "error". My initial thought was to do this in a theme skin like so:
<asp:CompareValidator runat="server"
CssClass="error" />
<asp:CustomValidator runat="server"
CssClass="error" />
<asp:RequiredFieldValidator runat="server"
CssClass="error" />
<belCommon:ZipCodeValidator runat="server"
CssClass="error" />
<belCommon:PhoneNumberValidator runat="server"
CssClass="error" />
This is only a partial listing of validators that I use. Ideally, I would like to do something like this:
<asp:BaseValidator runat="server"
CssClass="error" />
And because all validators inherit from BaseValidator, I would expect that this would work, but it doesn't. Is there a way to accomplish this without adding every single validator control to the skin explicitly?
Update:
I found a different approach using javascript:
Sys.Application.add_init(function(sender, args)
{
if (Page_Validators != null)
{
for (i = 0; i < Page_Validators.length; i++)
{
Page_Validators[i].className = "error";
}
}
});
ASP.Net generates a javascript variable called Page_Validators, which is an array of the validation spans. This script checks to see if it exists and then loops through and sets the class name. I added this to the master page and its working so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,在定义皮肤时,您专门使用单独的控件,并且您没有办法以任何其他方式指定它,因为它根据所使用的标签进行匹配。
从表面上看,您也在创建自己的自定义验证器,您可以修改控件以具有默认的错误 cssclass 以节省一些时间。
Not that I am aware of, when defining a skin you are specifically working with individual controls and you don't have a way of specifying it any other way, as it matches based on the tags used.
From the looks of it though, you are creating your own custom validators as well, you might modify your controls to have a default cssclass of error to save a bit of time.
我很想在服务器页面加载方法中迭代页面的控件并在那里应用 css 类:
这可能不是执行此操作的最佳方法,我还没有机会测试它,但希望如此可能会一路帮助你。
I'd be tempted to iterate over the controls of the page in the server pageload method and to apply the css class there:
This may not be the best way to do this and I've not had the opportunity to test it but hopefully it might help you along the way.
我发现了一种使用 javascript 的不同方法:
ASP.Net 生成一个名为 Page_Validators 的 javascript 变量,它是验证范围的数组。 该脚本检查它是否存在,然后循环并设置类名。 我将其添加到母版页中并且到目前为止它的工作原理。
I found a different approach using javascript:
ASP.Net generates a javascript variable called Page_Validators, which is an array of the validation spans. This script checks to see if it exists and then loops through and sets the class name. I added this to the master page and its working so far.