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
<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
protected void btnOk_Click(object sender, EventArgs e)
{
if (Validate())
{
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);
}
}
}
}
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;
}