开发者

Required Field Validations in USER CONTROL are fired in all instances of this usercontrol when it is placed multiple times in single page

开发者 https://www.devze.com 2023-01-24 12:14 出处:网络
I have开发者_StackOverflow usercontrol which has required field validation in it. I have2 instances of this usercontrol insingle web page .Now if I click button on first usercontrol the validation mes

I have开发者_StackOverflow 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


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.


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.


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.


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


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" />
0

精彩评论

暂无评论...
验证码 换一张
取 消