Page.isValid检查整个页面,而不仅仅是选定的验证组

Page.isValid checks the whole page, not just the selected validation group
2021-09-15
  •  译文(汉语)
  •  原文(英语)

我的表格上有几个验证器,有2个不同的验证器 ValidationGroups

在提交表单之前,我只需要检查ValidationGroup="main"控件是否有效,就可以尝试以下操作:

Page.Validate("main");
if (Page.IsValid) 
//do something

它不断返回false,我快疯了.

经过检查以确切确认哪个验证程序失败之后,看起来好像正在检查另一个验证器中的内容ValidationGroup.

我如何确保它只验证"主"组?

这是我完整的提交代码:(也许我正在检查失败的验证器是否错误?),但是无论哪种方式,我都会为page.isvalid变得假:(

Page.Validate("main");
            if ((Page.IsValid) && (user.userRole.Contains("9")))
                btnSubmit.Visible = true;
            else
            {
                foreach (BaseValidator valControl in Page.Validators)
                {
                    WebControl assControl = Page.FindControl(valControl.ControlToValidate) as WebControl;
                    if (valControl.IsValid)
                    {
                        //do something.

                    }
                    else
                    {
                        //do something.
                        lblMessage.Text = valControl.ToString();
                    }

                } 
                btnSubmit.Visible = false;
            }
解决过程1

请通读.它指出

Validate方法验证指定的验证组.在验证组上调用Validate方法后,仅当指定的验证组和导致页面发布到服务器的控件的验证组均有效时,IsValid方法才会返回true.

速聊1:
我的page_load方法中有此代码.我不确定什么"他控制导致页面发布到服务器的控件是有效的".方法 :-/
速聊2:
哪个控件引发了相应的服务器端事件?此外,如果您仅关注某些验证组/控件,为什么还要对其他验证组/控件进行验证?
速聊3:
其他验证器位于模式弹出面板中.仅在触发弹出窗口时使用的...
速聊4:
您是否已在特定的"提交"按钮上设置了ValidationGroup?
速聊5:
是的,但是我想检查表单在首次加载时是否完成,以了解如何或隐藏特定按钮(不是导致验证的按钮)

i have a few validators on my form, with 2 different ValidationGroups

before submitting the form, i only need to check for ValidationGroup="main" controls to be valid i tried the following:

Page.Validate("main");
if (Page.IsValid) 
//do something

it kept returning false and i was going crazzzy.

after a check to see exactly which validator fails, it looks like it's chocking on a validator that is in a different ValidationGroup.

how can i make sure that it only validates "main" group?

this is my full submit code: (maybe i'm checking for failed validator wrong?) but either way i keep getting false for page.isvalid :(

Page.Validate("main");
            if ((Page.IsValid) && (user.userRole.Contains("9")))
                btnSubmit.Visible = true;
            else
            {
                foreach (BaseValidator valControl in Page.Validators)
                {
                    WebControl assControl = Page.FindControl(valControl.ControlToValidate) as WebControl;
                    if (valControl.IsValid)
                    {
                        //do something.

                    }
                    else
                    {
                        //do something.
                        lblMessage.Text = valControl.ToString();
                    }

                } 
                btnSubmit.Visible = false;
            }
Solutions1

Please read through this. It states that

The Validate method validates the specified validation group. After calling the Validate method on a validation group, the IsValid method will return true only if both the specified validation group and the validation group of the control that caused the page to be posted to the server are valid.

Talk1:
i have this code in my page_load method. i'm not sure what "he control that caused the page to be posted to the server are valid." means :-/
Talk2:
which control raised the corresponding server side event? Also, if you are only concerned about certain validation group/controls, why you have validation on others?
Talk3:
the other validators are in a modal pop up panel. that are used when that pop up is triggered only...
Talk4:
Have you set ValidationGroup on the particular Submit button?
Talk5:
yes, but i want to check if the form is complete when it firsts loads, to eitehr how or hide a specific button (not one that causes validation)
转载于:https://stackoverflow.com/questions/15528028/page-isvalid-checks-the-whole-page-not-just-the-selected-validation-group

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入