当在单页中多次放置此用户控件时,将在该用户控件的所有实例中触发用户控件中的必填字段验证

发布于 2024-10-02 05:37:04 字数 121 浏览 0 评论 0原文

我有用户控件,其中需要进行字段验证。我在单个网页中有该用户控件的 2 个实例。现在,如果我单击第一个用户控件上的按钮,则会为 2 个用户控件触发验证消息。但只有该用户控件的必填字段必须被触发。请任何人解决这个问题。 谢谢 马达维

I have usercontrol which has required field validation in it. I have 2 instances of this usercontrol in single web page . Now if I click button on first usercontrol the validation messages are fired for 2 user controls. But only the required fields of that user control has to be fired. Please could any one solve this problem.
Thanks
Madhavi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

始终不够 2024-10-09 05:37:04

如果您尚未在用户控件中设置 requiredFieldValidator 的 ValidationGroup 属性,则每当提交表单时都会触发字段验证,无论哪个按钮导致回发。

如果您希望将特定的验证器与特定的提交按钮相关联,那么您必须将它们关联到同一个 ValdiationGroup。

如果您在用户控件中设置 ValidationGroup,但发现验证正在针对控件的所有实例触发,那么您将需要获取用户控件实例的某些属性并将其合并到 ValidationGroup 中,以确保用户控件经过独占验证对于控件上的任何提交按钮。

这是一个示例:

<asp:TextBox ID="txtTest" runat="server"/>
<asp:RequiredFieldValidator ID="txtTestReqVal"
     runat="server"
     Display="Dynamic"
     ControlToValidate="txtTest"
     Text="* Field is required"
     ValidationGroup="valGroup<%= ClientId %>"
     CssClass="modelError"
     />
<asp:Button ID="btnSubmit" runat="server" 
     Text="Submit" 
     CausesValidation="true" 
     ValidationGroup="valGroup<%= ClientId %>" 
     />

这里的技巧是 <%= ClientId %>部分。这会将实例客户端唯一 ID 注入到验证组值中。这意味着这些控件的所有验证将通过用户控件的单个唯一实例分组在一起。通过这种方式,您可以在同一页面上显示同一用户控件的多个实例,但所有实例都经过唯一验证。

If you have not set the ValidationGroup property of the RequiredFieldValidator within your usercontrol then field validation will fire whenever the form is submitted regardless of which button caused the postback.

If you wish to associated specific validators with specific submit buttons then you will have to associate them to the same ValdiationGroup.

If you are setting the ValidatioGroup within your user control but finding that the validation is firing for all instances of the control then you will need to take some attribute of the user control instance and incorporate it into the ValidationGroup to ensure the user control is exclusively validated for any submit button on the control.

Here is an example:

<asp:TextBox ID="txtTest" runat="server"/>
<asp:RequiredFieldValidator ID="txtTestReqVal"
     runat="server"
     Display="Dynamic"
     ControlToValidate="txtTest"
     Text="* Field is required"
     ValidationGroup="valGroup<%= ClientId %>"
     CssClass="modelError"
     />
<asp:Button ID="btnSubmit" runat="server" 
     Text="Submit" 
     CausesValidation="true" 
     ValidationGroup="valGroup<%= ClientId %>" 
     />

The trick here is the <%= ClientId %> part. This will inject the instances client side unique id into the validation group value. This means that all validation for these controls will be grouped together by the single unique instance of the user control. This way you can have multiple instances of the same user control present on the same page but all uniquely validated.

半窗疏影 2024-10-09 05:37:04

让您的验证组 runat="server"

然后给每个验证组一个唯一的验证组,如下所示:

string validationGroup = Guid.NewGuid().ToString();
txtContactNameValidator.ValidationGroup = validationGroup;
txtContactNumberValidator.ValidationGroup = validationGroup;
btnSave.ValidationGroup = validationGroup;

这会将用户控件彼此隔离,无论页面上有多少个用户控件。

make your validation groups runat="server"

Then give each one a unique validation group like this:

string validationGroup = Guid.NewGuid().ToString();
txtContactNameValidator.ValidationGroup = validationGroup;
txtContactNumberValidator.ValidationGroup = validationGroup;
btnSave.ValidationGroup = validationGroup;

This will isolate the user controls from each other no matter how many are on the page.

守望孤独 2024-10-09 05:37:04

我认为您需要在每个字段验证控件上指定一个 validatongroup 属性。

每个用户控件都定义了自己的验证组。

请参阅此处

解决此问题的一种方法是在您的您可以传入验证组名称的 usercontrol。

<uc:mycontrol id=u1 validationgroup="valA" .. />
<uc:mycontrol id=u2 validationgroup="valB" .. />

这种方法的一个问题是,您需要将验证组添加到用户控件上页面加载中的每个验证控件。

I think you need to specify a validatongroup attribute on every field validation control.

Each user control would have its own validation group defined.

See here

One way to solve this is to expose a public property on your usercontrol which you can pass in the validation group name.

<uc:mycontrol id=u1 validationgroup="valA" .. />
<uc:mycontrol id=u2 validationgroup="valB" .. />

One problem with this approach is that you will need to add the validation group to every validation control within page load on the usercontrol.

自由如风 2024-10-09 05:37:04

我认为验证组不起作用。因为它是相同的用户控件,但在页面中放置了两次。任何按钮都会触发对两个用户控件的验证

I think the validation group will not work.. As it is the same user control but dropped 2 times in the page. Any of the button will fire validation on both user control wh

彩虹直至黑白 2024-10-09 05:37:04
function ValidateRadio(button)
    {  
 var radioOptions = document.getElementById(button.id.split("_")[0] +'_rblPollOptions');
 var RVPollOptions = document.getElementById(button.id.split("_")[0] +'_RVPollOptions');
 var options = radioOptions.getElementsByTagName("input");
 var radioSelected=false;
  for (j=0; j < options.length; j++)
                {
                        if(options[j].checked)
                        {
                           radioSelected=true;
                           break; // Found it, proceed to next question  

                        }
   }
 if (!radioSelected) // no option selected
                {       // warn user, focus question //alert("You did not answer question");

                        RVPollOptions.style.visibility = "visible"; 
                         return false;
                }       
        }
</script>

<asp:RequiredFieldValidator ID="RVPollOptions" runat="server" ControlToValidate="rblPollOptions"
            ErrorMessage="Select option!"> </asp:RequiredFieldValidator>  
<asp:Button ID="btnPoll" Text="Vote" OnClientClick="javascript:return ValidateRadio(this)" runat="server"  OnClick="btnPoll_Click" />
function ValidateRadio(button)
    {  
 var radioOptions = document.getElementById(button.id.split("_")[0] +'_rblPollOptions');
 var RVPollOptions = document.getElementById(button.id.split("_")[0] +'_RVPollOptions');
 var options = radioOptions.getElementsByTagName("input");
 var radioSelected=false;
  for (j=0; j < options.length; j++)
                {
                        if(options[j].checked)
                        {
                           radioSelected=true;
                           break; // Found it, proceed to next question  

                        }
   }
 if (!radioSelected) // no option selected
                {       // warn user, focus question //alert("You did not answer question");

                        RVPollOptions.style.visibility = "visible"; 
                         return false;
                }       
        }
</script>

<asp:RequiredFieldValidator ID="RVPollOptions" runat="server" ControlToValidate="rblPollOptions"
            ErrorMessage="Select option!"> </asp:RequiredFieldValidator>  
<asp:Button ID="btnPoll" Text="Vote" OnClientClick="javascript:return ValidateRadio(this)" runat="server"  OnClick="btnPoll_Click" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文