现在的位置: 首页 > 综合 > 正文

ASP.net重复提交解决方法

2012年12月10日 ⁄ 综合 ⁄ 共 1965字 ⁄ 字号 评论关闭

 一、提交的内容在当前页面中处理,防止刷新当前页面再次提交
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        AdoSql AddComent = new AdoSql();
        AddComent.Add_Input_Coment(this.GetNewsID(), this.TextBox3.Text, this.TextBox1.Text, this.TextBox2.Text);
        //防止刷新页面时候的回发问题
        int iResult;
        int iUp = 1000;
        Random ro = new Random();
        iResult = ro.Next(iUp);
        string url = "read_news.aspx?n_id="+Request.QueryString["n_id"];
        url = url +"&r=" + iResult.ToString();
        Response.Redirect(url);
    }

二、转向到新页面,后退重复提交的处理。
1、设置页面无缓存
protected void Page_Load(object sender, EventArgs e)
    {
        //设置页面无缓存,设置页面无效
        Response.Cache.SetNoStore();
    }
protected void btn_Reg_Click(object sender, EventArgs e)
{
省略.......
  try
        {
            usi.Persist(t);
            t.Commit();
            CommonHelper.DoUpAlert(this.Page, "成功!");
            Response.Write("<script   language=javascript>window.location.href=window.location.href;</script>");  
            Response.Redirect("~/index.aspx");
        }
        catch (Exception ex)
        {
            t.Rollback();
            CommonHelper.DoUpAlert(this.Page, "失败!");
        }

}

2、
//页面加载
protected void Page_Load(object sender, EventArgs e)
{
   //可以在页面加载时设置页面的缓存为“SetNoStore()”,即无缓存
   Response.Cache.SetNoStore();
   //Session中存储的变量“IsSubmit”是标记是否提交成功的
   if ((bool)Session["IsSubmit"])
   {
     //如果表单数据提交成功,就设“Session["IsSubmit"]”为false
     Session["IsSubmit"] = false;
     //显示提交成功信息
     ShowMsg.Text = " * 提交成功!";
   }
   else
     //否则的话(没有提交,或者是页面刷新),不显示任何信息
     ShowMsg.Text = "";
}
//提交按钮(btnOK)单击事件
protected void btnOK_Click(object sender, EventArgs e)
{
   if (txtTitle.Text.ToString().Trim() == "") ........
     else if (txtText.Text.ToString().Trim() == "") .......
      else
   {
     //这里是将数据提交到数据库中,省略
     /*
     string sql = "insert into tab...values(...)";
     MyConn.ExecQuery(sql);
     */
     //提交成功后,设“Session["IsSubmit"]”为true
     Session["IsSubmit"] = true;
     //强制转换页面(不可少,否则刷新仍会重复提交,仍转到本页),
     //通过页面的转换将缓存中的提交的数据都释放了,即提交的标单数据不会被保存到缓存里,
    // 如果后退的话,将会出现该页无法显示
     Response.Redirect("post.aspx");
  }
}

抱歉!评论已关闭.