Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old August 8th, 2012, 05:46 AM
Authorized User
Points: 121, Level: 2
Points: 121, Level: 2 Points: 121, Level: 2 Points: 121, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default

I'm sorry, You have right... explain ...
I need disable the RequiredFieldValidator where this conditions:
  1. If DateStart or DateEnd are empty or null enable the RequiredFieldValidator1 and RequiredFieldValidator2 else disable the RequiredFieldValidator1 and RequiredFieldValidator2.
  2. 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:
Code:
- Start
- End
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..
Reply With Quote
  #12 (permalink)  
Old August 8th, 2012, 06:15 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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?

Quote:
What I mean?
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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #13 (permalink)   Spam!  
Old August 8th, 2012, 07:25 AM
Authorized User
Points: 121, Level: 2
Points: 121, Level: 2 Points: 121, Level: 2 Points: 121, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default

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..
Reply With Quote
  #14 (permalink)  
Old August 8th, 2012, 07:33 AM
Authorized User
Points: 121, Level: 2
Points: 121, Level: 2 Points: 121, Level: 2 Points: 121, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default

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>
Reply With Quote
  #15 (permalink)  
Old August 8th, 2012, 04:17 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #16 (permalink)  
Old August 9th, 2012, 03:19 AM
Authorized User
Points: 121, Level: 2
Points: 121, Level: 2 Points: 121, Level: 2 Points: 121, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Imar View Post
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)
    {

    }
}
Reply With Quote
  #17 (permalink)  
Old August 9th, 2012, 09:50 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
cms9651 (August 9th, 2012)
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does anyone know if it is possible to call a C# method from a javascript in ASPX page LeoMathew ASP.NET 1.0 and 1.1 Professional 4 November 18th, 2009 10:09 AM
Javascript isnt work well on my aspx page msbsam ASP.NET 2.0 Professional 1 July 11th, 2008 08:00 AM
Validator controls throwing JavaScript errors Bob Bedell ASP.NET 2.0 Professional 7 December 31st, 2007 06:05 AM
Javascript with aspx page zaheerabbas.sk ASP.NET 1.0 and 1.1 Professional 2 April 4th, 2007 11:36 PM
Javascript Works Fine in Aspx page but... vivek_inos Javascript 0 June 14th, 2006 01:57 AM



All times are GMT -4. The time now is 05:33 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.