Validation Groups - Server side

G

Guest

Ok, so I'm working through examples in the apress book "Pro ASP.NET 2.0 in C#
2005". The Validation groups example doesn't work the way I thought it
should. It consists of 2 textboxes and 2 buttons and 2 required field
validators. One set of controls is set to validation group "G1" and the
other set to "G2". There is also a label to display what happens on the
server.

The example appears to work flawlessly at the browser but results at the
server vary. Specifically, leave the first textbox empty and enter text in
the 2nd textbox (group "G2" controls) and click it's Validate button. The
page posts back and the server reports that both groups evaluate to false. I
had expected The "G2" contol to evaluate to true. Am I missing somrthing
here? See below for the HTML and code behind.

Bill



<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="10ValidationGroups.aspx.cs"
Inherits="Ch04ServerControls_10ValidationGroups" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Validation Groups</title>
</head>
<body>
<form id="form1" runat="server">
<div style="font-size: 16pt; color: #0000cc">
Pro ASP.NET 2.0 in C#<br />
Chapter 4, Page 144<br />
<hr style="color: #ff0099; height: 5px" />
Web Controls - Validation Groups
<br />
<asp:HyperLink ID="HyperLink1" runat="server" font-size="10pt"
NavigateUrl="~/Default.aspx">[Home]</asp:HyperLink>
<br />
<br />
</div>
<div>
<asp:panel ID="Panel1" runat="server" Height="67px" Width="281px">
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="G1"
Width="229px"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ErrorMessage="*Required."
Width="185px" ControlToValidate="TextBox1"
ValidationGroup="G1"></asp:RequiredFieldValidator><br />
<asp:Button ID="Button1" runat="server" Text="Validate Group 1"
ValidationGroup="G1"
Width="139px" OnClick="ValidatAll_Click" /></asp:panel>
<br />

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<br />
<asp:panel ID="Panel2" runat="server" Height="67px" Width="281px">
<asp:TextBox ID="TextBox2" runat="server" ValidationGroup="G2"
Width="230px"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
runat="server" ErrorMessage="*Required."
Width="186px" ControlToValidate="TextBox2"
ValidationGroup="G2"></asp:RequiredFieldValidator><br />
<asp:Button ID="Button2" runat="server" Text="Validate Group 2"
ValidationGroup="G2"
Width="139px" OnClick="ValidatAll_Click" /></asp:panel>
</div>
</form>
</body>
</html>




using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Ch04ServerControls_10ValidationGroups :
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ValidatAll_Click(object sender, EventArgs e)
{
Label1.Text = "Valid: " + Page.IsValid.ToString();
Page.Validate("G1");
Label1.Text += "<br/>Group 1 Valid: " + Page.IsValid.ToString();
Page.Validate("G2");
Label1.Text += "<br/>Group 2 Valid: " + Page.IsValid.ToString();
}
}
 
J

Joey

Ok, so I'm working through examples in the apress book "Pro ASP.NET 2.0 in C#
2005". The Validation groups example doesn't work the way I thought it
should. It consists of 2 textboxes and 2 buttons and 2 required field
validators. One set of controls is set to validation group "G1" and the
other set to "G2". There is also a label to display what happens on the
server.

The example appears to work flawlessly at the browser but results at the
server vary. Specifically, leave the first textbox empty and enter text in
the 2nd textbox (group "G2" controls) and click it's Validate button. The
page posts back and the server reports that both groups evaluate to false. I
had expected The "G2" contol to evaluate to true. Am I missing somrthing
here? See below for the HTML and code behind.

Bill

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="10ValidationGroups.aspx.cs"
Inherits="Ch04ServerControls_10ValidationGroups" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Validation Groups</title>
</head>
<body>
<form id="form1" runat="server">
<div style="font-size: 16pt; color: #0000cc">
Pro ASP.NET 2.0 in C#<br />
Chapter 4, Page 144<br />
<hr style="color: #ff0099; height: 5px" />
Web Controls - Validation Groups
<br />
<asp:HyperLink ID="HyperLink1" runat="server" font-size="10pt"
NavigateUrl="~/Default.aspx">[Home]</asp:HyperLink>
<br />
<br />
</div>
<div>
<asp:panel ID="Panel1" runat="server" Height="67px" Width="281px">
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="G1"
Width="229px"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ErrorMessage="*Required."
Width="185px" ControlToValidate="TextBox1"
ValidationGroup="G1"></asp:RequiredFieldValidator><br />
<asp:Button ID="Button1" runat="server" Text="Validate Group 1"
ValidationGroup="G1"
Width="139px" OnClick="ValidatAll_Click" /></asp:panel>
<br />

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<br />
<asp:panel ID="Panel2" runat="server" Height="67px" Width="281px">
<asp:TextBox ID="TextBox2" runat="server" ValidationGroup="G2"
Width="230px"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
runat="server" ErrorMessage="*Required."
Width="186px" ControlToValidate="TextBox2"
ValidationGroup="G2"></asp:RequiredFieldValidator><br />
<asp:Button ID="Button2" runat="server" Text="Validate Group 2"
ValidationGroup="G2"
Width="139px" OnClick="ValidatAll_Click" /></asp:panel>
</div>
</form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Ch04ServerControls_10ValidationGroups :
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ValidatAll_Click(object sender, EventArgs e)
{
Label1.Text = "Valid: " + Page.IsValid.ToString();
Page.Validate("G1");
Label1.Text += "<br/>Group 1 Valid: " + Page.IsValid.ToString();
Page.Validate("G2");
Label1.Text += "<br/>Group 2 Valid: " + Page.IsValid.ToString();
}



}- Hide quoted text -

- Show quoted text -

Double check your control-to-validate property for each. Make sure
that for rfv1 its set to txt1 and for rfv2 its set to txt2. Also,
double check the validation group properties for both validators, both
buttons, and both validation summaries. You do not have to set it for
the textboxes.

I have just recently figured how to use validations groups and also
custom validators. Once you figure out how to insert custom logic into
both the client-side and server-side custom validation functions
(unless you already have), you will find that life is MUCH EASIER!!!
 
G

Guest

Ok, so all the stuff you mentioned to check is set correctly as you can see
by the code I included. Yet the server continues to return false for
validation group G2 given the conditions I described in my original post. So
the question remains, is there a problem with my code, or is there a problem
with the way validation groups work at the server?

Bill


Joey said:
Ok, so I'm working through examples in the apress book "Pro ASP.NET 2.0 in C#
2005". The Validation groups example doesn't work the way I thought it
should. It consists of 2 textboxes and 2 buttons and 2 required field
validators. One set of controls is set to validation group "G1" and the
other set to "G2". There is also a label to display what happens on the
server.

The example appears to work flawlessly at the browser but results at the
server vary. Specifically, leave the first textbox empty and enter text in
the 2nd textbox (group "G2" controls) and click it's Validate button. The
page posts back and the server reports that both groups evaluate to false. I
had expected The "G2" contol to evaluate to true. Am I missing somrthing
here? See below for the HTML and code behind.

Bill

<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="10ValidationGroups.aspx.cs"
Inherits="Ch04ServerControls_10ValidationGroups" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Validation Groups</title>
</head>
<body>
<form id="form1" runat="server">
<div style="font-size: 16pt; color: #0000cc">
Pro ASP.NET 2.0 in C#<br />
Chapter 4, Page 144<br />
<hr style="color: #ff0099; height: 5px" />
Web Controls - Validation Groups
<br />
<asp:HyperLink ID="HyperLink1" runat="server" font-size="10pt"
NavigateUrl="~/Default.aspx">[Home]</asp:HyperLink>
<br />
<br />
</div>
<div>
<asp:panel ID="Panel1" runat="server" Height="67px" Width="281px">
<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="G1"
Width="229px"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ErrorMessage="*Required."
Width="185px" ControlToValidate="TextBox1"
ValidationGroup="G1"></asp:RequiredFieldValidator><br />
<asp:Button ID="Button1" runat="server" Text="Validate Group 1"
ValidationGroup="G1"
Width="139px" OnClick="ValidatAll_Click" /></asp:panel>
<br />

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
<br />
<asp:panel ID="Panel2" runat="server" Height="67px" Width="281px">
<asp:TextBox ID="TextBox2" runat="server" ValidationGroup="G2"
Width="230px"></asp:TextBox><br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
runat="server" ErrorMessage="*Required."
Width="186px" ControlToValidate="TextBox2"
ValidationGroup="G2"></asp:RequiredFieldValidator><br />
<asp:Button ID="Button2" runat="server" Text="Validate Group 2"
ValidationGroup="G2"
Width="139px" OnClick="ValidatAll_Click" /></asp:panel>
</div>
</form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Ch04ServerControls_10ValidationGroups :
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ValidatAll_Click(object sender, EventArgs e)
{
Label1.Text = "Valid: " + Page.IsValid.ToString();
Page.Validate("G1");
Label1.Text += "<br/>Group 1 Valid: " + Page.IsValid.ToString();
Page.Validate("G2");
Label1.Text += "<br/>Group 2 Valid: " + Page.IsValid.ToString();
}



}- Hide quoted text -

- Show quoted text -

Double check your control-to-validate property for each. Make sure
that for rfv1 its set to txt1 and for rfv2 its set to txt2. Also,
double check the validation group properties for both validators, both
buttons, and both validation summaries. You do not have to set it for
the textboxes.

I have just recently figured how to use validations groups and also
custom validators. Once you figure out how to insert custom logic into
both the client-side and server-side custom validation functions
(unless you already have), you will find that life is MUCH EASIER!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top