1

I have two validation summary controls on one page. Both have different validation group. please look below code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm11.aspx.cs" Inherits="TestApp.WebForm11" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="g1"/>
            <asp:Label ID="Label1" runat="server" Text="label1"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server" Text="A"></asp:TextBox>
            <asp:CustomValidator ID="CustomValidator1" runat="server" ValidationGroup="g1" OnServerValidate="CustomValidator1_ServerValidate">*</asp:CustomValidator>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="g1" CausesValidation="false" OnClick="Button1_Click" />
            <br />
            <asp:ValidationSummary ID="ValidationSummary2" runat="server" ValidationGroup="g2"/>
            <asp:Label ID="Label2" runat="server" Text="label1"></asp:Label>
            <asp:TextBox ID="TextBox2" runat="server" Text="B"></asp:TextBox>
            <asp:CustomValidator ID="CustomValidator2" runat="server" ValidationGroup="g2" OnServerValidate="CustomValidator2_ServerValidate">*</asp:CustomValidator>
            <br />
            <asp:Button ID="Button2" runat="server" Text="Button" ValidationGroup="g2" CausesValidation="false" OnClick="Button2_Click" />
            <br />
        </div>
    </form>
</body>
</html>

code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestApp
{
    public partial class WebForm11 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;

            int num;
            if(!(int.TryParse(TextBox1.Text, out num)))
            {
                CustomValidator1.ErrorMessage = "cv1 msg";
                args.IsValid = false;
            }
        }

        protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = true;

            int num;
            if (!(int.TryParse(TextBox2.Text, out num)))
            {
                CustomValidator2.ErrorMessage = "cv2 msg";
                args.IsValid = false;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Page.Validate("g1");
            if(this.Page.IsValid)
                Response.Write("button1 success");
            else
                Response.Write("button1 falied");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Page.Validate("g2");
            if (this.Page.IsValid)
                Response.Write("button2 success");
            else
                Response.Write("button2 falied");
        }
    }
}

Now, when I click on button1, it validate the validation group "g1" and show

enter image description here

And when click on button2, it validate the validation group "g2" and show

enter image description here

hmmmmmmm, but I don't want to loose validation summary 1 (group g1) message on click on button2.

Also, I don't want to validate both groups g1 and g2 on click event.

Why I am loosing validation g1 message on button2 click event.

Konamiman
  • 49,681
  • 17
  • 108
  • 138
Maddy
  • 907
  • 3
  • 10
  • 25

1 Answers1

0

It is because only one validation happens in a either click event. You may want to check both validations in both click events. Or insert both on Page reload event.

Chand Priyankara
  • 6,739
  • 2
  • 40
  • 63
  • actually in application, button2 event checks for both g1 and g2. But button1 just checks for g1. Now if user first clicks on button2 it is showing all error message, but then user click on button1 it remove error message for textbox1. it should only remove message of textbox2. – Maddy Jun 16 '15 at 14:36
  • 1
    So the problem is in the sequence. Therefore you can use a [common] variable to identify the state of the user's events and do the trick. – Chand Priyankara Jun 16 '15 at 14:40
  • thank you pushing me for doing that, I was thinking about that. – Maddy Jun 16 '15 at 14:42