导航回到上一页asp.net后得到警报 [英] getting alert after navigating back to to previous page asp.net
本文介绍了导航回到上一页asp.net后得到警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我写了两个文本框的比较验证,第一次我输入错误,我将收到警告信息,第二次我使用浏览器后退按钮导航回上一页,输入有效输入并导航到下一页我再次收到该警报消息?但我不希望发生这种情况,该怎么办
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()) //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);
}
}
}
}
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;
}
推荐答案
单击后退按钮时,在其按钮单击事件中,清除两个文本框的文本...--
TextBox1.Text =";
TextBox2.Text =";
when you click back button, in its button click event, clear text of both textboxes...like -
TextBox1.Text = "";
TextBox2.Text = "";
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!
这篇关于导航回到上一页asp.net后得到警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文