Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

i have written compare validation for two textboxes,first time i give wrong inputs and i will get alert message which i have given,second time i will give valid inputs and navigate to next page,when i navigate back to previous page using browser back button i once again get that alert message?but i dont want that to happen,How to do that


C#
<asp:CompareValidator ID="CompareValidator1" ControlToValidate="txtFromDate"
               ControlToCompare="txtToDate" Display="Dynamic" runat="server"
               ErrorMessage="To Date should be Greater than From Date" Operator="LessThan"
               Type="Date" Font-Size="Small" >

this is compare validation

C#
protected void btnOk_Click(object sender, EventArgs e)
       {
               if (Validate()) //this method makes sure the textboxes are not empty
               {
                   string Data = combo.SelectedItem.Text;
                   string FromDate = txtFromDate.Text;
                   string ToDate = txtToDate.Text;
                   if (ValidateNoOfDays(Data, FromDate, ToDate))
                   {
                       try
                       {
                           Session["Data"] =Data;
                           Session["FromDate"] = FromDate;
                           Session["ToDate"] = ToDate;

                           Response.Redirect("Show.aspx");
                       }
                       catch (Exception ex)
                       {
                           Response.Write(ex.Message);
                       }
                   }
               }
           }


C#
private bool ValidateNoOfDays(string data, string FromDate, string ToDate)
       {
           bool Valid = true;
           try
           {
               string myStringVariable = "";
               DateTime F_Date = Convert.ToDateTime(FromDate);
               DateTime T_Date = Convert.ToDateTime(ToDate);
               switch (data)
               {
                   case "Week":
                       TimeSpan _WeekT = T_Date.Subtract(F_Date);
                       if ((_WeekT.Days) > 7)
                       {
                           Valid = false;
                           myStringVariable = "Week Cannot have more than 7 days";
                           ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + myStringVariable + "');", true);

                       }
                       break;

                   case "Month":
                       TimeSpan _MonthT = T_Date.Subtract(F_Date);
                       if ((_MonthT.Days) > 30)
                       {
                           Valid = false;
                           myStringVariable = "Month Cannot have more than 30 days";
                           ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + myStringVariable + "');", true);
                       }
                       break;


                   case "Year":
                       TimeSpan _YearT = T_Date.Subtract(F_Date);
                       if ((_YearT.Days) > 365)
                       {
                           Valid = false;
                           myStringVariable = "year Cannot have more than 365 days";
                           ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + myStringVariable + "');", true);
                       }
                       break;
               }
           }
           catch (Exception ex)
           {

           }
           return Valid;
       }
Posted
Updated 19-Oct-11 22:51pm
v2

when you click back button, in its button click event, clear text of both textboxes...like -

TextBox1.Text = "";
TextBox2.Text = "";
 
Share this answer
 
Comments
girish sp 20-Oct-11 0:38am    
you mean back button of browser??because it happened when i clicked browser back button,no back button in my application.thanks for your reply
sgjoshi85 20-Oct-11 0:40am    
clear these textboxes on page_load() of your page..where you have these textboxes..
girish sp 20-Oct-11 3:22am    
if i clear those textboxes in pageload later i cannot read the dates in button click event of that page,it will be null and i will get exception
XML
which textbox you are validating first? I assume you set ControlToValidate = 2nd textbox..

Here is the code i tried and working fyn..
On First.aspx --
 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:CompareValidator ID="CompareValidator1" ControlToValidate="TextBox2" ControlToCompare="TextBox1" Operator="Equal" runat="server" ErrorMessage="No Match!"></asp:CompareValidator>
        <br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

On button_Click event, it redirects to Second.aspx page... now from here when i click browser's back arrow..it doesn't shown me alert message...

Try out this!
 
Share this answer
 
Comments
girish sp 20-Oct-11 3:39am    
sure i will do this and mark as solved if it works, and thanks very much for your valuable answer.
girish sp 20-Oct-11 4:38am    
hi,
this control validation part was working fine,after user enters inputs i will take the difference between two textboxes(of type date),if the diff is greater than some value i will give alert.later user enters values whose diff is within that range and click ok,it will take him to next page,now upon back button i am getting
girish sp 20-Oct-11 4:39am    
i will post sample code i am doing ,may be then you will get idea what i am trying to do..
sgjoshi85 20-Oct-11 4:40am    
ya plz copy ur sample code..
girish sp 20-Oct-11 4:52am    
hi,check out..i have pasted my code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900