 |
| ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 8th, 2012, 05:46 AM
|
|
Authorized User
|
|
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
I'm sorry, You have right... explain ...
I need disable the RequiredFieldValidator where this conditions:
- If DateStart or DateEnd are empty or null enable the RequiredFieldValidator1 and RequiredFieldValidator2 else disable the RequiredFieldValidator1 and RequiredFieldValidator2.
- If DateStart1 or DateEnd2 are empty or null enable the RequiredFieldValidator2 and RequiredFieldValidator3 else disable the RequiredFieldValidator3 and RequiredFieldValidator4.
You write that my problem is the return false in OnClientClick event:
Code:
<asp:ImageButton ID="Button1" ImageUrl="http://p2p.wrox.com/images/icon.gif" runat="server" OnClick="Button1_Click"
OnClientClick="javascript:InitValidators();return false;" />
I delete return false in OnClientClick event:
Code:
<asp:ImageButton ID="Button1" ImageUrl="http://p2p.wrox.com/images/icon.gif" runat="server" OnClick="Button1_Click"
OnClientClick="javascript:InitValidators();" />
And tried my aspx page.
When call the OnClientClick event in ImageButton with ID "Button1" the function InitValidators response in alert javascript with this ErrorMessage:
Code:
- Start
- End
- Start1
- End1
Instead of:
Because in this case I ask DateStart and DateEnd.
If delete return false in OnClientClick event not working the conditions 1 and 2.
What I mean?
Code:
<asp:TextBox ID="DateStart" runat="server" Width="70"></asp:TextBox>
<img src="Images/calen.gif" id="Img1" alt="DateStart" onmouseover="this.style.cursor='pointer'"
onmouseout="this.style.cursor='default'" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="DateStart"
PopupButtonID="Img1" Format="dd/MM/yyyy" Animated="true">
</ajaxToolkit:CalendarExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DateStart"
ErrorMessage="Start" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="DateStart"
ErrorMessage="Invalid date" Display="Dynamic" CssClass="redb" ValidationExpression="^(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)\d\d$">
</asp:RegularExpressionValidator>
<asp:TextBox ID="DateEnd" runat="server" Width="70"></asp:TextBox>
<img src="Images/calen.gif" id="Img2" alt="End" onmouseover="this.style.cursor='pointer'"
onmouseout="this.style.cursor='default'" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="DateEnd"
PopupButtonID="Img2" Format="dd/MM/yyyy" Animated="true">
</ajaxToolkit:CalendarExtender>
<asp:ImageButton ID="Button1" ImageUrl="http://p2p.wrox.com/images/icon.gif" runat="server" OnClick="Button1_Click"
OnClientClick="javascript:InitValidators();" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="DateEnd"
ErrorMessage="End" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="DateEnd"
ErrorMessage="Invalid date" Display="Dynamic" CssClass="redb" ValidationExpression="^(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)\d\d$">
</asp:RegularExpressionValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Invalid Date"
ControlToValidate="DateEnd" ControlToCompare="DateStart" Type="Date"
Operator="GreaterThanEqual" CssClass="redb" SetFocusOnError="false" Display="Dynamic">
</asp:CompareValidator>
<asp:TextBox ID="DateStart1" runat="server" Width="70"></asp:TextBox>
<img src="Images/calen.gif" id="Img3" alt="Start" onmouseover="this.style.cursor='pointer'"
onmouseout="this.style.cursor='default'" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender3" runat="server" TargetControlID="DateStart1"
PopupButtonID="Img3" Format="dd/MM/yyyy" Animated="true">
</ajaxToolkit:CalendarExtender>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="DateStart1"
ErrorMessage="Start1" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="DateStart1"
ErrorMessage="Invalid date" Display="Dynamic" CssClass="redb" ValidationExpression="^(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)\d\d$">
</asp:RegularExpressionValidator>
<asp:TextBox ID="DateEnd1" runat="server" Width="70"></asp:TextBox>
<img src="Images/calen.gif" id="Img4" alt="End" onmouseover="this.style.cursor='pointer'"
onmouseout="this.style.cursor='default'" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender4" runat="server" TargetControlID="DateEnd1"
PopupButtonID="Img4" Format="dd/MM/yyyy" Animated="true">
</ajaxToolkit:CalendarExtender>
<asp:ImageButton ID="Button2" ImageUrl="http://p2p.wrox.com/images/icon.gif" runat="server" OnClick="Button2_Click"
OnClientClick="javascript:InitValidators();return false;" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="DateEnd1"
ErrorMessage="End1" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="DateEnd1"
ErrorMessage="Invalid date" Display="Dynamic" CssClass="redb" ValidationExpression="^(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)\d\d$">
</asp:RegularExpressionValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server" ErrorMessage="Invalid Date"
ControlToValidate="DateEnd1" ControlToCompare="DateStart1" Type="Date"
Operator="GreaterThanEqual" CssClass="redb" SetFocusOnError="false" Display="Dynamic">
</asp:CompareValidator>
<script type="text/javascript">
function InitValidators() {
var DateStart = document.getElementById('<%=DateStart.ClientID%>');
var DateEnd = document.getElementById('<%=DateEnd.ClientID%>')
var DateStart1 = document.getElementById('<%=DateStart1.ClientID%>');
var DateEnd1 = document.getElementById('<%=DateEnd1.ClientID%>')
if (DateStart.value == '' || DateEnd.value == '') {
//If you want to enable validation then use follwoing code
ValidatorEnable(document.getElementById('<%= RequiredFieldValidator1.ClientID%>'), true);
ValidatorEnable(document.getElementById('<%=RequiredFieldValidator2.ClientID%>'), true);
}
else {
ValidatorEnable(document.getElementById('<%= RequiredFieldValidator1.ClientID%>'), false);
ValidatorEnable(document.getElementById('<%=RequiredFieldValidator2.ClientID%>'), false);
}
if (DateStart1.value == '' || DateEnd1.value == '') {
//If you want to enable validation then use follwoing code
ValidatorEnable(document.getElementById('<%= RequiredFieldValidator3.ClientID%>'), true);
ValidatorEnable(document.getElementById('<%=RequiredFieldValidator4.ClientID%>'), true);
}
else {
ValidatorEnable(document.getElementById('<%= RequiredFieldValidator3.ClientID%>'), false);
ValidatorEnable(document.getElementById('<%=RequiredFieldValidator4.ClientID%>'), false);
}
}
</script>
Last edited by cms9651; August 8th, 2012 at 06:02 AM..
|
|

August 8th, 2012, 06:15 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I really tried, but I still don't get it. There's too much code that - I think - is not relevant to the problem which confuses matters. Also, all these controls having their default name like RequiredFieldValidator1 doesn't help much in clarifying.
Quote:
|
Because in this case I ask DateStart and DateEnd.
|
Do you? Where do you ask for these fields? Is it because you're leaving fields empty or something?
Quote:
|
the function InitValidators response in alert javascript with this ErrorMessage
|
InitValidators only enables or disables the controls. I think it's the validation framework that shows the alert. So maybe validation kicks in before your InitValidators does?
That's indeed the question ;-)
Again: try recreating a page like this with only a single control and a single validator control and see if you can repro the problem. Then I'll take a look and try to run it locally. This means you shouldn't be using stuff like GridViews and databases; just a few siple controls to demonstrate the problem.
Imar
|

August 8th, 2012, 07:25 AM
|
|
Authorized User
|
|
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
Ok, please try this code semplified:
Code:
<%@ Page Language="C#" AutoEventWireup="true" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body onload="InitValidators()">
<form id="form1" runat="server">
<div>
<asp:ValidationSummary ID="ValidationSummary1" ShowMessageBox="true" runat="server"
ShowSummary="false" />
<asp:TextBox ID="DateStart" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDateStart" runat="server" ControlToValidate="DateStart"
ErrorMessage="DateStart" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:TextBox ID="DateEnd" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Invalid Date"
ControlToValidate="DateEnd" ControlToCompare="DateStart" Type="Date" Operator="GreaterThanEqual"
CssClass="redb" SetFocusOnError="false" Display="Dynamic"></asp:CompareValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDateEnd" runat="server" ControlToValidate="DateEnd"
ErrorMessage="DateEnd" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:ImageButton ID="Button1" ImageUrl="http://p2p.wrox.com/images/cerca_icon.gif" runat="server" OnClick="Button1_Click"
OnClientClick="javascript:InitValidators();" />
<br />
<br />
<asp:TextBox ID="DateStart1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDateStart1" runat="server" ControlToValidate="DateStart1"
ErrorMessage="DateStart1" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:TextBox ID="DateEnd1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDateEnd1" runat="server" ControlToValidate="DateEnd1"
ErrorMessage="DateEnd1" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server" ErrorMessage="Invalid Date"
ControlToValidate="DateEnd1" ControlToCompare="DateStart1" Type="Date" Operator="GreaterThanEqual"
CssClass="redb" SetFocusOnError="false" Display="Dynamic"></asp:CompareValidator>
<asp:ImageButton ID="Button2" ImageUrl="http://p2p.wrox.com/images/cerca_icon.gif" runat="server" OnClick="Button2_Click"
OnClientClick="javascript:InitValidators();" />
<asp:Label ID="MessagesLabel" runat="server" Text=""></asp:Label>
<br />
<br />
</div>
</form>
<script type="text/javascript" language="javascript">
function InitValidators() {
var DateStart = document.getElementById('<%=DateStart.ClientID%>');
var DateEnd = document.getElementById('<%=DateEnd.ClientID%>')
var DateStart1 = document.getElementById('<%=DateStart1.ClientID%>');
var DateEnd1 = document.getElementById('<%=DateEnd1.ClientID%>')
if (DateStart.value == '' || DateEnd.value == '') {
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateStart.ClientID%>'), true);
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateEnd.ClientID%>'), true);
}
else {
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateStart.ClientID%>'), false);
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateEnd.ClientID%>'), false);
}
if (DateStart1.value == '' || DateEnd1.value == '') {
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateStart1.ClientID%>'), true);
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateEnd1.ClientID%>'), true);
}
else {
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateStart1.ClientID%>'), false);
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateEnd1.ClientID%>'), false);
}
}
</script>
</body>
</html>
Last edited by cms9651; August 8th, 2012 at 07:32 AM..
|
|

August 8th, 2012, 07:33 AM
|
|
Authorized User
|
|
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
Ok, please try this code semplified:
Code:
<%@ Page Language="C#" AutoEventWireup="true" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body onload="InitValidators()">
<form id="form1" runat="server">
<div>
<asp:ValidationSummary ID="ValidationSummary1" ShowMessageBox="true" runat="server"
ShowSummary="false" />
<asp:TextBox ID="DateStart" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDateStart" runat="server" ControlToValidate="DateStart"
ErrorMessage="DateStart" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:TextBox ID="DateEnd" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Invalid Date"
ControlToValidate="DateEnd" ControlToCompare="DateStart" Type="Date" Operator="GreaterThanEqual"
CssClass="redb" SetFocusOnError="false" Display="Dynamic"></asp:CompareValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDateEnd" runat="server" ControlToValidate="DateEnd"
ErrorMessage="DateEnd" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:ImageButton ID="Button1" ImageUrl="images/cerca_icon.gif" runat="server" OnClick="Button1_Click"
OnClientClick="javascript:InitValidators();" />
<br />
<br />
<asp:TextBox ID="DateStart1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDateStart1" runat="server" ControlToValidate="DateStart1"
ErrorMessage="DateStart1" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:TextBox ID="DateEnd1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDateEnd1" runat="server" ControlToValidate="DateEnd1"
ErrorMessage="DateEnd1" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server" ErrorMessage="Invalid Date"
ControlToValidate="DateEnd1" ControlToCompare="DateStart1" Type="Date" Operator="GreaterThanEqual"
CssClass="redb" SetFocusOnError="false" Display="Dynamic"></asp:CompareValidator>
<asp:ImageButton ID="Button2" ImageUrl="images/cerca_icon.gif" runat="server" OnClick="Button2_Click"
OnClientClick="javascript:InitValidators();" />
<asp:Label ID="MessagesLabel" runat="server" Text=""></asp:Label>
<br />
<br />
</div>
</form>
<script type="text/javascript" language="javascript">
function InitValidators() {
var DateStart = document.getElementById('<%=DateStart.ClientID%>');
var DateEnd = document.getElementById('<%=DateEnd.ClientID%>')
var DateStart1 = document.getElementById('<%=DateStart1.ClientID%>');
var DateEnd1 = document.getElementById('<%=DateEnd1.ClientID%>')
if (DateStart.value == '' || DateEnd.value == '') {
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateStart.ClientID%>'), true);
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateEnd.ClientID%>'), true);
}
else {
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateStart.ClientID%>'), false);
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateEnd.ClientID%>'), false);
}
if (DateStart1.value == '' || DateEnd1.value == '') {
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateStart1.ClientID%>'), true);
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateEnd1.ClientID%>'), true);
}
else {
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateStart1.ClientID%>'), false);
ValidatorEnable(document.getElementById('<%= RequiredFieldValidatorDateEnd1.ClientID%>'), false);
}
}
</script>
</body>
</html>
|
|

August 8th, 2012, 04:17 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
I just ran your code and it seems to work fine. When I enter two dates on the first row and click the image, it correctly validates the other two.
What is it you're trying to accomplish? When you enter values in the first row and click the image, do you want to disable the validators in the other row? Can you describe your use case in more detail?
Imar
|
|

August 9th, 2012, 03:19 AM
|
|
Authorized User
|
|
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by Imar
Hi there,
I just ran your code and it seems to work fine. When I enter two dates on the first row and click the image, it correctly validates the other two.
What is it you're trying to accomplish? When you enter values in the first row and click the image, do you want to disable the validators in the other row? Can you describe your use case in more detail?
Imar
|
Hi Imar;
Thanks you very much for your help.
I'm really happy for your quickly answer and congratulations for your book:
BEGINNING ASP.NET 4 IN C# AND VB
IN C# AND VB
Spaanjaars Imar.
In your book I have found the solution to my problem: Chapter 9 Validating User Input, pages 311-316.
In new version of my aspx page I use the method a CustomValidator instead of a RequiredFieldValidator.
I'm really happy with my financial investment in your book...
Good bye
Page.aspx
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="SqlServer_button" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager" EnableScriptGlobalization="true" EnableScriptLocalization="true"
runat="server">
</asp:ScriptManager>
<div>
<asp:ValidationSummary ID="ValidationSummary1" ShowMessageBox="true" runat="server"
ShowSummary="false" ValidationGroup="Group1" />
<asp:ValidationSummary ID="ValidationSummary2" ShowMessageBox="true" runat="server"
ShowSummary="false" ValidationGroup="Group2" />
<asp:TextBox ID="StartDate" runat="server" Width="70" ValidationGroup="Group1"></asp:TextBox>
<img src="/webapplication1/Images/pop-up-calendar.gif" id="Img1" alt="Date Start"
onmouseover="this.style.cursor='pointer'" onmouseout="this.style.cursor='default'" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="StartDate"
PopupButtonID="Img1" Format="dd/MM/yyyy" Animated="true">
</ajaxToolkit:CalendarExtender>
<asp:TextBox ID="EndDate" runat="server" Width="70" ValidationGroup="Group1"></asp:TextBox>
<img src="/webapplication1/Images/pop-up-calendar.gif" id="Img2" alt="Date End" onmouseover="this.style.cursor='pointer'"
onmouseout="this.style.cursor='default'" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="EndDate"
PopupButtonID="Img2" Format="dd/MM/yyyy" Animated="true">
</ajaxToolkit:CalendarExtender>
<asp:CustomValidator ID="StartDateValidator" runat="server" ErrorMessage="StartDate is required."
ClientValidationFunction="InitValidators" OnServerValidate="StartDateValidator_Validate"
ValidationGroup="Group1" Display="Dynamic"></asp:CustomValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="Invalid Date"
ControlToValidate="EndDate" ControlToCompare="StartDate" Type="Date" Operator="GreaterThanEqual"
CssClass="redb" SetFocusOnError="false" Display="Dynamic" ValidationGroup="Group1"></asp:CompareValidator>
<asp:ImageButton ID="Button1" ImageUrl="/webapplication1/Images/confirm.png" runat="server"
ValidationGroup="Group1" OnClick="Button1_Click" OnClientClick="if(!confirm('Are You sure?')) return false;" />
<br />
<br />
<asp:TextBox ID="StartDate1" runat="server" Width="70" ValidationGroup="Group2"></asp:TextBox>
<img src="/webapplication1/Images/pop-up-calendar.gif" id="Img3" alt="Data fine"
onmouseover="this.style.cursor='pointer'" onmouseout="this.style.cursor='default'" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender3" runat="server" TargetControlID="StartDate1"
PopupButtonID="Img3" Format="dd/MM/yyyy" Animated="true">
</ajaxToolkit:CalendarExtender>
<asp:TextBox ID="EndDate1" runat="server" Width="70" ValidationGroup="Group2"></asp:TextBox>
<img src="/webapplication1/Images/pop-up-calendar.gif" id="Img4" alt="Data fine"
onmouseover="this.style.cursor='pointer'" onmouseout="this.style.cursor='default'" />
<ajaxToolkit:CalendarExtender ID="CalendarExtender4" runat="server" TargetControlID="EndDate1"
PopupButtonID="Img4" Format="dd/MM/yyyy" Animated="true">
</ajaxToolkit:CalendarExtender>
<asp:CustomValidator ID="StartDate1Validator" runat="server" ErrorMessage="StartDate1 is required."
ClientValidationFunction="InitValidators1" OnServerValidate="StartDate1Validator_Validate"
ValidationGroup="Group2" Display="Dynamic"></asp:CustomValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server" ErrorMessage="Invalid Date"
ControlToValidate="EndDate1" ControlToCompare="StartDate1" Type="Date" Operator="GreaterThanEqual"
CssClass="redb" SetFocusOnError="false" Display="Dynamic" ValidationGroup="Group2"></asp:CompareValidator>
<asp:ImageButton ID="Button2" ImageUrl="/webapplication1/Images/confirm.png" runat="server"
ValidationGroup="Group2" OnClick="Button2_Click" OnClientClick="if(!confirm('Are You sure?')) return false;" />
</div>
</form>
<script type="text/javascript" language="javascript">
function InitValidators(source, args) {
var StartDate = document.getElementById('<%=StartDate.ClientID%>');
var EndDate = document.getElementById('<%=EndDate.ClientID%>'); // added semi-colon
if (StartDate.value == '' || EndDate.value == '') {
args.IsValid = false;
}
}
function InitValidators1(source, args) {
var StartDate1 = document.getElementById('<%=StartDate1.ClientID%>'); // added variable
var EndDate1 = document.getElementById('<%=EndDate1.ClientID%>'); // added variable
if (StartDate1.value == '' || EndDate1.value == '') {
args.IsValid = false;
}
}
</script>
</body>
</html>
Code-behind:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;
using System.Text;
public partial class SqlServer_button : System.Web.UI.Page
{
protected void StartDateValidator_Validate(object sender, ServerValidateEventArgs args)
{
if (string.IsNullOrEmpty(StartDate.Text) || string.IsNullOrEmpty(EndDate.Text))
{
args.IsValid = false;
}
}
protected void StartDate1Validator_Validate(object sender, ServerValidateEventArgs args)
{
if (string.IsNullOrEmpty(StartDate1.Text) || string.IsNullOrEmpty(EndDate1.Text))
{
args.IsValid = false;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(Object sender, EventArgs e)
{
}
protected void Button2_Click(Object sender, EventArgs e)
{
}
}
|
|

August 9th, 2012, 09:50 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Sounds like a good solution. You may also want to look into Validation Groups, briefly mentioned in the book. http://msdn.microsoft.com/en-us/library/ms227424.aspx
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|
 |