locked
Multiple Validation Groups in c# RRS feed

  • Question

  • User-2004582644 posted

    Hi,

    On the form aspx page I have two validation groups: Group1 and Group2

    I have an save button that needs to validate all validation groups.

    But how can it be done server side?

    I try this code using Page.Validate without success.

    <html>
    <body>
         <form runat="server">
              <asp:textbox id="TextBox1" runat="server"/>
              <asp:requiredfieldvalidator ValidationGroup="Group1"
                                                           ErrorText="Need to Fill in Value 1  !"
                                                           ControlToValidate="TextBox1"
                                                           runat="server"/>
                <asp:textbox id="TextBox2" runat="server"/>
    
                <asp:requiredfieldvalidator ValidationGroup="Group2"
                                                             ErrorText="Need to Fill in Value 2 !"
                                                             ControlToValidate="TextBox2"
                                                             runat="server"/> 
    
                <asp:button text="Group1" ValidationGroup="Group1" runat="server"/>
                <asp:button text="Group2" ValidationGroup="Group2" runat="server"/>
    
               <asp:ImageButton ID="Save" runat="server" OnClick="Save_Click"
                ImageUrl="/aspnet/Img/save_button.gif" 
                CommandArgument="Save"
                OnClientClick="if (!confirm('Are you sure?')) return false;" />
         </form>
    </body>
    </html>
    
    protected void Save_Click(object sender, ImageClickEventArgs e)
    {
        Page.Validate("Group1");
        Page.Validate("Group2");
    
        if (Page.IsValid)
        {
            //validate page
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('Stop.');", true);
        }
    }

    Friday, April 10, 2020 1:54 PM

Answers

  • User475983607 posted

    Chevy Marl Sunderland

    It is a form in which the temporary saving of data can be performed in different stages.

    I don't get it.  If you've already validated the group why are you validating the group again?

    Chevy Marl Sunderland

    Tried but the from is validate with single form empty.

    Works for me.  Your original code does not work because you are always validating the  "Group2" the last validation.  If you want to validate both then you need an conditional statement; a pattern like the following.

    protected void Save_Click(object sender, ImageClickEventArgs e)
    {
        Page.Validate("Group1");
        if (!Page.IsValid)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('Stop.');", true);
    		return;
        }
    	
    	Page.Validate("Group2");
    	if (!Page.IsValid)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('Stop.');", true);
    		return;
        }
    }

    You might be interested in reading the reference docs.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page.validate?view=netframework-4.8

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 10, 2020 2:43 PM

All replies

  • User475983607 posted

    Chevy Marl Sunderland

    On the form aspx page I have two validation groups: Group1 and Group2

    I have an save button that needs to validate all validation groups.

    But how can it be done server side?

    It does not make sense to have two different validation groups if you need to validate both groups are the same time.  Can you explain the use case as there might be a better approach.

    However, the following SO post covers how to validate a group or groups.

    https://stackoverflow.com/questions/1196191/checking-if-a-validationgroup-is-valid-from-code-behind

    Friday, April 10, 2020 2:04 PM
  • User-2004582644 posted

    mgebhard

    Chevy Marl Sunderland

    On the form aspx page I have two validation groups: Group1 and Group2

    I have an save button that needs to validate all validation groups.

    But how can it be done server side?

    It does not make sense to have two different validation groups if you need to validate both groups are the same time.  Can you explain the use case?

    Thank you for reply.

    It is a form in which the temporary saving of data can be performed in different stages.

    For each individual section it's associated one validation group.

    When the saving of the data is definitive I must check that all the data saved temporarily is present within each individual section of the form.

    If at least one data is absent, I can't have to permanently save the data.

    Friday, April 10, 2020 2:11 PM
  • User-2004582644 posted

    Chevy Marl Sunderland

    On the form aspx page I have two validation groups: Group1 and Group2

    I have an save button that needs to validate all validation groups.

    But how can it be done server side?

    It does not make sense to have two different validation groups if you need to validate both groups are the same time.  Can you explain the use case as there might be a better approach.

    However, the following SO post covers how to validate a group or groups.

    https://stackoverflow.com/questions/1196191/checking-if-a-validationgroup-is-valid-from-code-behind

    Tried but the from is validate with single form empty.

    Friday, April 10, 2020 2:25 PM
  • User475983607 posted

    Chevy Marl Sunderland

    It is a form in which the temporary saving of data can be performed in different stages.

    I don't get it.  If you've already validated the group why are you validating the group again?

    Chevy Marl Sunderland

    Tried but the from is validate with single form empty.

    Works for me.  Your original code does not work because you are always validating the  "Group2" the last validation.  If you want to validate both then you need an conditional statement; a pattern like the following.

    protected void Save_Click(object sender, ImageClickEventArgs e)
    {
        Page.Validate("Group1");
        if (!Page.IsValid)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('Stop.');", true);
    		return;
        }
    	
    	Page.Validate("Group2");
    	if (!Page.IsValid)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('Stop.');", true);
    		return;
        }
    }

    You might be interested in reading the reference docs.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.page.validate?view=netframework-4.8

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 10, 2020 2:43 PM