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
And when click on button2, it validate the validation group "g2" and show
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.
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.