Response.Write("<script language=javascript>history.go(-2);</script>");
实现返回上一页面,加入到“确定”以及“取消”按钮的onclick事件中使用也很方便,但遇到了个问题:
我的“确定”按钮是要判断某一TextBox是否非空并提示的,在我提示过一次以后,再点“确定”按钮history.go(-2)返回的就成了我提示之前的页面,也就是当前页面而不是上一页面了,请问这该如何写?
6 个解决方案
#1
初始化一个int值为-2,然后每次出错-2?好像很容易出错,这个应该有比较直观的办法吧?
#2
ViewState中放一个值,每次Page_Load就-1.
#3
没用过viewstate,去查了下看到很多人都说那个比较容易出错,就想了个类似的方法。
建了一个全局整形变量初始值为-1,也是每次Page_Load就-1,但还是和我前面一样的情况。
建了一个全局整形变量初始值为-1,也是每次Page_Load就-1,但还是和我前面一样的情况。
#4
放在viewstate中可以
if (ViewState["count"] != null)
{
int count = (int)ViewState["count"];
ViewState["count"] = count - 1;
}
else
{
ViewState["count"] = -1;
}
if (ViewState["count"] != null)
{
int count = (int)ViewState["count"];
ViewState["count"] = count - 1;
}
else
{
ViewState["count"] = -1;
}
#5
..
#6
if (!string.IsNullOrEmpty(Request.Form["Birthday"]))
{
string birth = Request.Form["Birthday"];
if (!Validator1.IsDateTime(birth)) {
Response.Write("<script>alert('出生年月格式XXXX-XX-XX,请重新录入!');history.go(-2);</script>");
return;
}
DateTime dt = Convert.ToDateTime(Request.Form["Birthday"]);
int span = DateTime.Now.Year - dt.Year;
if (span > 16 && span < 45)
{
PXmem1.Birthday = dt;
}
else
{
Response.Write("<script>alert('年龄不在16-45岁之间,录入失败!');history.go(-2);</script>");
return;
}
}
#region 验证日期
/**/
/// <summary>
/// 验证日期
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsDateTime(string source)
{
try
{
DateTime time = Convert.ToDateTime(source);
return true;
}
catch
{
return false;
}
}
#endregion
#region 验证身份证是否有效
/**/
/// <summary>
/// 验证身份证是否有效
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public static bool IsIDCard(string Id)
{
if (Id.Length == 18)
{
bool check = IsIDCard18(Id);
return check;
}
else
{
return false;
}
}
public static bool IsIDCard18(string Id)
{
if (Id.Length != 18)
{ return false; }
long n = 0;
if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
{
return false;//数字验证
}
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(Id.Remove(2)) == -1)
{
return false;//省份验证
}
string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, out time) == false)
{
return false;//生日验证
}
string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
char[] Ai = Id.Remove(17).ToCharArray();
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
}
int y = -1;
Math.DivRem(sum, 11, out y);
if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
{
return false;//校验码验证
}
return true;//符合GB11643-1999标准
}
#endregion
#region 是不是Int型的
/**/
/// <summary>
/// 是不是Int型的
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsInt(string source)
{
Regex regex = new Regex(@"^(-){0,1}\d+$");
if (regex.Match(source).Success)
{
if ((long.Parse(source) > 0x7fffffffL) || (long.Parse(source) < -2147483648L))
{
return false;
}
return true;
}
return false;
}
#endregion
{
string birth = Request.Form["Birthday"];
if (!Validator1.IsDateTime(birth)) {
Response.Write("<script>alert('出生年月格式XXXX-XX-XX,请重新录入!');history.go(-2);</script>");
return;
}
DateTime dt = Convert.ToDateTime(Request.Form["Birthday"]);
int span = DateTime.Now.Year - dt.Year;
if (span > 16 && span < 45)
{
PXmem1.Birthday = dt;
}
else
{
Response.Write("<script>alert('年龄不在16-45岁之间,录入失败!');history.go(-2);</script>");
return;
}
}
#region 验证日期
/**/
/// <summary>
/// 验证日期
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsDateTime(string source)
{
try
{
DateTime time = Convert.ToDateTime(source);
return true;
}
catch
{
return false;
}
}
#endregion
#region 验证身份证是否有效
/**/
/// <summary>
/// 验证身份证是否有效
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public static bool IsIDCard(string Id)
{
if (Id.Length == 18)
{
bool check = IsIDCard18(Id);
return check;
}
else
{
return false;
}
}
public static bool IsIDCard18(string Id)
{
if (Id.Length != 18)
{ return false; }
long n = 0;
if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
{
return false;//数字验证
}
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(Id.Remove(2)) == -1)
{
return false;//省份验证
}
string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, out time) == false)
{
return false;//生日验证
}
string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
char[] Ai = Id.Remove(17).ToCharArray();
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
}
int y = -1;
Math.DivRem(sum, 11, out y);
if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
{
return false;//校验码验证
}
return true;//符合GB11643-1999标准
}
#endregion
#region 是不是Int型的
/**/
/// <summary>
/// 是不是Int型的
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsInt(string source)
{
Regex regex = new Regex(@"^(-){0,1}\d+$");
if (regex.Match(source).Success)
{
if ((long.Parse(source) > 0x7fffffffL) || (long.Parse(source) < -2147483648L))
{
return false;
}
return true;
}
return false;
}
#endregion
#1
初始化一个int值为-2,然后每次出错-2?好像很容易出错,这个应该有比较直观的办法吧?
#2
ViewState中放一个值,每次Page_Load就-1.
#3
没用过viewstate,去查了下看到很多人都说那个比较容易出错,就想了个类似的方法。
建了一个全局整形变量初始值为-1,也是每次Page_Load就-1,但还是和我前面一样的情况。
建了一个全局整形变量初始值为-1,也是每次Page_Load就-1,但还是和我前面一样的情况。
#4
放在viewstate中可以
if (ViewState["count"] != null)
{
int count = (int)ViewState["count"];
ViewState["count"] = count - 1;
}
else
{
ViewState["count"] = -1;
}
if (ViewState["count"] != null)
{
int count = (int)ViewState["count"];
ViewState["count"] = count - 1;
}
else
{
ViewState["count"] = -1;
}
#5
..
#6
if (!string.IsNullOrEmpty(Request.Form["Birthday"]))
{
string birth = Request.Form["Birthday"];
if (!Validator1.IsDateTime(birth)) {
Response.Write("<script>alert('出生年月格式XXXX-XX-XX,请重新录入!');history.go(-2);</script>");
return;
}
DateTime dt = Convert.ToDateTime(Request.Form["Birthday"]);
int span = DateTime.Now.Year - dt.Year;
if (span > 16 && span < 45)
{
PXmem1.Birthday = dt;
}
else
{
Response.Write("<script>alert('年龄不在16-45岁之间,录入失败!');history.go(-2);</script>");
return;
}
}
#region 验证日期
/**/
/// <summary>
/// 验证日期
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsDateTime(string source)
{
try
{
DateTime time = Convert.ToDateTime(source);
return true;
}
catch
{
return false;
}
}
#endregion
#region 验证身份证是否有效
/**/
/// <summary>
/// 验证身份证是否有效
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public static bool IsIDCard(string Id)
{
if (Id.Length == 18)
{
bool check = IsIDCard18(Id);
return check;
}
else
{
return false;
}
}
public static bool IsIDCard18(string Id)
{
if (Id.Length != 18)
{ return false; }
long n = 0;
if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
{
return false;//数字验证
}
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(Id.Remove(2)) == -1)
{
return false;//省份验证
}
string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, out time) == false)
{
return false;//生日验证
}
string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
char[] Ai = Id.Remove(17).ToCharArray();
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
}
int y = -1;
Math.DivRem(sum, 11, out y);
if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
{
return false;//校验码验证
}
return true;//符合GB11643-1999标准
}
#endregion
#region 是不是Int型的
/**/
/// <summary>
/// 是不是Int型的
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsInt(string source)
{
Regex regex = new Regex(@"^(-){0,1}\d+$");
if (regex.Match(source).Success)
{
if ((long.Parse(source) > 0x7fffffffL) || (long.Parse(source) < -2147483648L))
{
return false;
}
return true;
}
return false;
}
#endregion
{
string birth = Request.Form["Birthday"];
if (!Validator1.IsDateTime(birth)) {
Response.Write("<script>alert('出生年月格式XXXX-XX-XX,请重新录入!');history.go(-2);</script>");
return;
}
DateTime dt = Convert.ToDateTime(Request.Form["Birthday"]);
int span = DateTime.Now.Year - dt.Year;
if (span > 16 && span < 45)
{
PXmem1.Birthday = dt;
}
else
{
Response.Write("<script>alert('年龄不在16-45岁之间,录入失败!');history.go(-2);</script>");
return;
}
}
#region 验证日期
/**/
/// <summary>
/// 验证日期
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsDateTime(string source)
{
try
{
DateTime time = Convert.ToDateTime(source);
return true;
}
catch
{
return false;
}
}
#endregion
#region 验证身份证是否有效
/**/
/// <summary>
/// 验证身份证是否有效
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
public static bool IsIDCard(string Id)
{
if (Id.Length == 18)
{
bool check = IsIDCard18(Id);
return check;
}
else
{
return false;
}
}
public static bool IsIDCard18(string Id)
{
if (Id.Length != 18)
{ return false; }
long n = 0;
if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
{
return false;//数字验证
}
string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
if (address.IndexOf(Id.Remove(2)) == -1)
{
return false;//省份验证
}
string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
DateTime time = new DateTime();
if (DateTime.TryParse(birth, out time) == false)
{
return false;//生日验证
}
string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
char[] Ai = Id.Remove(17).ToCharArray();
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
}
int y = -1;
Math.DivRem(sum, 11, out y);
if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
{
return false;//校验码验证
}
return true;//符合GB11643-1999标准
}
#endregion
#region 是不是Int型的
/**/
/// <summary>
/// 是不是Int型的
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static bool IsInt(string source)
{
Regex regex = new Regex(@"^(-){0,1}\d+$");
if (regex.Match(source).Success)
{
if ((long.Parse(source) > 0x7fffffffL) || (long.Parse(source) < -2147483648L))
{
return false;
}
return true;
}
return false;
}
#endregion