基于验证更改控件的样式(ASP.NET) [英] Changing a control's style based on validation (ASP.NET)
问题描述
我有一个非常简单的形式有以下困扰片断:
< ASP:面板类=正常ID =Panel1的=服务器>
<强>< ASP:标签ID =Panel1Error级=错误可见=假=服务器/>< / STRONG> <标签=TextBox1的><环境监察及GT; * LT; / EM>不要将此处留空< /标签>
< ASP:文本框ID =TextBox1的=服务器/> < ASP:的RequiredFieldValidator ID =TextBox1RFV=服务器
的ControlToValidate =TextBox1中的ErrorMessage =该字段不能为空。
显示=无/> < ---其他验证--->
< / ASP:面板>
有我想要当页面验证失败做两件事情:
-
Panel1的风格变化(以其中一个显示不同的颜色来指示错误)。我可以通过调用Page.Validate在Page_Load中,然后通过迭代Page.Validators,让每个校验的父控件,它转换为一个小组,然后设置.CssClass似乎并不像一个极好的解决方案,要做到这一点,但它得到的完成任务 - 有没有更好的办法
? -
我要采取任何验证错误(S)都扔掉,并把它们在Panel1Error标签,以及将其设置为可见。这是我有点困惑。我起初以为我可能指定在一个验证写入其的ErrorMessage的标签,但我却没有这样的运气。如果我只是折腾标签内的验证,其格式设置弄乱了页面的整个布局,无论我直接分配给它的'错误'的CSS类或让它在标签中。
只是为了澄清,在生产,我会做这个过程中多个面板页面上,每一个表单元素,preventing我从显式调用面板和只是说Panel1.CssClass等。
- 我会推荐一个JavaScript的解决方案。 ASP.NET注入一个全局变量JS所谓Page_Validators,这是所有验证器的数组横跨在页面上。我写这个的<一个href=\"http://john.rummell.info/john/blog/post/Apply-the-same-CSS-class-to-all-validators-in-a-web-project.aspx\"相对=nofollow>我的博客。这是一个不同的解决方案,但它应该给你足够的洞察力开始。
- 使用的ValidationSummary 与控制一个的ValidationGroup 每个小组。
I've got a very simple form with the following troubled snippet:
<asp:Panel class="normal" ID="Panel1" runat="server">
<strong><asp:Label ID="Panel1Error" class="error" Visible="false" runat="server"/></strong>
<label for="TextBox1"><em>*</em> Don't leave this blank</label>
<asp:TextBox ID="TextBox1" runat="server" />
<asp:RequiredFieldValidator ID="TextBox1RFV" runat="server"
ControlToValidate="TextBox1" ErrorMessage="This field cannot be blank."
Display="None" />
<--- other validators --->
</asp:Panel>
There are two things I want to do when the page fails validation:
Change the style of Panel1 (to one which shows different colors to indicate an error). I was able to do this by calling Page.Validate in Page_Load, then iterating over Page.Validators, getting each validator's parent control, casting it to a Panel, then setting .CssClass Doesn't seem like a superb solution, but it got the job done - is there a better way?
I want to take whatever validation error(s) are thrown and put them in the Panel1Error label, as well as set it to visible. This is where I am a bit baffled. I thought at first I could possibly specify the Label in which a validator writes its ErrorMessage, but I had no such luck. If I just toss the validator inside the Label, its formatting messes up the entire layout of the page, regardless of whether I directly assign it the 'error' CSS class or just leave it in the Label.
Just to clarify, in production, I would be doing this process for multiple Panels on a page, each with one form element, preventing me from calling the Panels explicitly and just saying Panel1.CssClass, etc.
- I would recommend a javascript solution. ASP.NET injects a global js variable called Page_Validators, which is an array of all of the validator spans on the page. I wrote about this on my blog. It's a different solution, but it should give you enough insight to get started.
- Use ValidationSummary controls with a ValidationGroup for each panel.
这篇关于基于验证更改控件的样式(ASP.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!