I have an application that uses RequiredFieldValidator controls with a ValidationSummary and once a "Finish" button is clicked all the fields are validated. This page has several different parts of it that are made visible or invisible and I noticed that the Validators only activate for visible controls and ignore the invisible ones. This is a useful functionality that is utilized by my program.
我有一个使用RequiredFieldValidator控件和ValidationSummary的应用程序,一旦点击“完成”按钮,所有字段都被验证。这个页面有几个不同的部分,它们是可见的或不可见的,我注意到Validators只激活可见的控件并忽略不可见的控件。这是我的程序使用的有用功能。
however....
I have a need to validate controls that aren't visible since I am splitting up parts of one long page into segments. So I was wondering if there is a way to manually force visible = false validation controls (or ones that are part of an invisible table, etc) to be validated on Page.IsValid, without having to make them visible again?
我需要验证不可见的控件,因为我将一个长页的一部分拆分成段。所以我想知道是否有办法手动强制可见= false验证控件(或属于隐形表的一部分等)在Page.IsValid上进行验证,而不必让它们再次可见?
Thanks in Advance!
提前致谢!
P.S. I tried using the ValidationGroup property to see if that would catch them but it didn't.
附:我尝试使用ValidationGroup属性来查看是否会捕获它们但它没有。
4 个解决方案
#1
If the control isn't .Visible then the html isn't there to validate against. Instead of setting Control.Visible = false, try setting a css class to hide it instead.
如果控件不是.Visible那么html就不存在验证。而不是设置Control.Visible = false,尝试设置一个css类来隐藏它。
In your style sheet:
在你的样式表中:
.invisible { visibility:hidden; }
And then instead of .Visible = false use:
然后代替.Visible = false使用:
Control.CssClass = "invisible";
#2
if the page is divided into segments and only part of the page is shown, couldn't you validate on the method that shows the new segment? The valid status of the control shouldn't be affected once it has been made invisible. (Unless there is a dependancy i guess.).
如果页面被分成段并且只显示了页面的一部分,那么您是否无法验证显示新段的方法?一旦不可见,控件的有效状态不应受到影响。 (除非我猜测有依赖性。)
Don't let them switch segments of the page with invalid input.
不要让他们用无效的输入切换页面的片段。
an alternative would be to use css to make the controls invisible rather than server side. The controls could be in a div with display: none; All the controls would be rendered along with the validation summary.
另一种方法是使用css使控件不可见而不是服务器端。控件可以在div中显示:none;所有控件都将与验证摘要一起呈现。
#3
If you're making these controls invisible server-side, then they're not even rendered on the client to be validated.
如果您使这些控件在服务器端不可见,那么它们甚至不会在客户端上呈现以进行验证。
Are you using AJAX to switch between your sections/make the controls invisible? If so then you should probably validate the controls on those partial page postbacks (as Jeff Martin suggested) before toggling the controls invisible.
您是否使用AJAX在您的部分之间切换/使控件不可见?如果是这样,那么你应该在切换控件不可见之前验证那些部分页面回发的控件(正如Jeff Martin建议的那样)。
I think we need some more info on how you're doing this to give you a good answer.
我想我们需要更多关于你如何做到这一点的信息给你一个很好的答案。
Alright so no AJAX (based on your comment)... if you're using ASP.NET Validation controls they have a client-side portion and a server-side portion. You can see if the validators are valid in your event handlers in your code-behind. You may have to do something specific to get them to fire their validation code server-side, and your invisible controls may not retain their values between multiple postbacks. But it should be doable.
好吧所以没有AJAX(根据你的评论)...如果你使用ASP.NET验证控件,他们有一个客户端部分和一个服务器端部分。您可以在代码隐藏中的事件处理程序中查看验证程序是否有效。您可能必须执行某些特定操作才能使它们在服务器端触发验证代码,并且您的隐形控件可能无法在多个回发之间保留其值。但它应该是可行的。
The gist here is that ASP.NET Validation controls can be set so they don't do anything client-side, but they still provide validation server-side. You're essentially in that boat.
这里的要点是ASP.NET Validation控件可以设置为它们不会在客户端执行任何操作,但它们仍然提供验证服务器端。你基本上就是那艘船。
I think this MSDN article has all you need.
我认为这篇MSDN文章已经满足您的所有需求。
#4
In response to the 2nd comment to Mike Comstock's post (I don't have enough reputation to comment yet :( )...
回应Mike Comstock的帖子的第二条评论(我还没有足够的声誉评论:()......
to remove the spacing try:
删除间距尝试:
.invisible { visibility:hidden; display:none; }
#1
If the control isn't .Visible then the html isn't there to validate against. Instead of setting Control.Visible = false, try setting a css class to hide it instead.
如果控件不是.Visible那么html就不存在验证。而不是设置Control.Visible = false,尝试设置一个css类来隐藏它。
In your style sheet:
在你的样式表中:
.invisible { visibility:hidden; }
And then instead of .Visible = false use:
然后代替.Visible = false使用:
Control.CssClass = "invisible";
#2
if the page is divided into segments and only part of the page is shown, couldn't you validate on the method that shows the new segment? The valid status of the control shouldn't be affected once it has been made invisible. (Unless there is a dependancy i guess.).
如果页面被分成段并且只显示了页面的一部分,那么您是否无法验证显示新段的方法?一旦不可见,控件的有效状态不应受到影响。 (除非我猜测有依赖性。)
Don't let them switch segments of the page with invalid input.
不要让他们用无效的输入切换页面的片段。
an alternative would be to use css to make the controls invisible rather than server side. The controls could be in a div with display: none; All the controls would be rendered along with the validation summary.
另一种方法是使用css使控件不可见而不是服务器端。控件可以在div中显示:none;所有控件都将与验证摘要一起呈现。
#3
If you're making these controls invisible server-side, then they're not even rendered on the client to be validated.
如果您使这些控件在服务器端不可见,那么它们甚至不会在客户端上呈现以进行验证。
Are you using AJAX to switch between your sections/make the controls invisible? If so then you should probably validate the controls on those partial page postbacks (as Jeff Martin suggested) before toggling the controls invisible.
您是否使用AJAX在您的部分之间切换/使控件不可见?如果是这样,那么你应该在切换控件不可见之前验证那些部分页面回发的控件(正如Jeff Martin建议的那样)。
I think we need some more info on how you're doing this to give you a good answer.
我想我们需要更多关于你如何做到这一点的信息给你一个很好的答案。
Alright so no AJAX (based on your comment)... if you're using ASP.NET Validation controls they have a client-side portion and a server-side portion. You can see if the validators are valid in your event handlers in your code-behind. You may have to do something specific to get them to fire their validation code server-side, and your invisible controls may not retain their values between multiple postbacks. But it should be doable.
好吧所以没有AJAX(根据你的评论)...如果你使用ASP.NET验证控件,他们有一个客户端部分和一个服务器端部分。您可以在代码隐藏中的事件处理程序中查看验证程序是否有效。您可能必须执行某些特定操作才能使它们在服务器端触发验证代码,并且您的隐形控件可能无法在多个回发之间保留其值。但它应该是可行的。
The gist here is that ASP.NET Validation controls can be set so they don't do anything client-side, but they still provide validation server-side. You're essentially in that boat.
这里的要点是ASP.NET Validation控件可以设置为它们不会在客户端执行任何操作,但它们仍然提供验证服务器端。你基本上就是那艘船。
I think this MSDN article has all you need.
我认为这篇MSDN文章已经满足您的所有需求。
#4
In response to the 2nd comment to Mike Comstock's post (I don't have enough reputation to comment yet :( )...
回应Mike Comstock的帖子的第二条评论(我还没有足够的声誉评论:()......
to remove the spacing try:
删除间距尝试:
.invisible { visibility:hidden; display:none; }