利用判断__VIEWSTATE防止页面重复提交,废话不多说,直接贴代码:
初始定义变量
private static string Flag__ViewState;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Flag__ViewState = string.Empty;
}
}
提交:
protected void btnSave_Click(object sender, EventArgs e)
{
if (Request.Headers["Accept"] != "*/*" && Request.Form["__VIEWSTATE"] != Flag__ViewState)
{
Flag__ViewState = Request.Form["__VIEWSTATE"];
try
{
xx ci = new xx();
ci.Save();
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "good", "<script> alert('添加成功!');</script>");
}
catch (Exception)
{
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "faile", "<script> alert('添加失败!');</script>");
}
finally { Flag__ViewState = string.Empty; }
}
}
看不懂的,多用用就会了,还不会的面壁去。