How to avoid duplicate entry from asp.net on Postback?

时光怂恿深爱的人放手 提交于 2019-12-08 03:58:10

问题


I have a dropdown list that pulls data from template table. I have an Add button to insert new template. Add button will brings up jQuery popup to insert new values. There will be a save button to save the new data. On_Save_Click I enter the new data and close the popup.

Here is the proplem: When I refresh the page, the page entering the values again. So, I get duplicate entries!

Question: How can I avoid this issue? I check out Satckoverflow and Google, both they suggest to redirect to another page. I don't want to redirect the user to another page. How can I use the same form to avoid this issue? Please help.


回答1:


You can use viewstate or session to indicate if data already inserted (button pressed).

Something like this:

private void OnbuttonAdd_click()
{
   if(ViewState["DataInserted"] != "1")
   {
      ...
      // Add new entry...
      ...
      if(data inserted successfully)
      {
         ViewState["DataInserted"] = "1";
      }
   }
}

Edit:

public bool DataInserted
{
    get
    {
        if (HttpContext.Current.Session["DataInserted"] == null)
        {
            HttpContext.Current.Session["DataInserted"] = false;
        }
        bool? dataInserted = HttpContext.Current.Session["DataInserted"] as bool?;

        return dataInserted.Value;
    }
    set
    {
        HttpContext.Current.Session["DataInserted"] = value;
    }
}

...

private void OnbuttonAdd_click()
{
   if(!DataInserted)
   {
      ...
      // Add new entry...
      ...
      if(data inserted successfully)
      {
         DataInserted = true;
      }
   }
}



回答2:


The simplest way is to use a post/redirect/get pattern.

Basically, the refresh action for page build with post requires to repost the data. Using this pattern, you will reload the whole page.

With ASP.Net, you have a simple alternative, use an UpdatePanel. This will refresh only part of the page using AJAX. As the page itself is still the result of a GET request, you can refresh the page. And as you use ASP.Net, it's quite easy to integrate.

Finally, you can use a home made AJAX refresh. A combination of jQuery, KnockOut and rest services (for example), can help you to avoid refreshing the full page in benefits of an ajax call.




回答3:


There is some experience:

  1. Disable Submit button on click (in client side by JavaScript).
  2. change Session['issaved'] = true on save operation at server side and change it on new action to false.
  3. use view state for pass parameters like RecordId (instead of QueryString) to clear on refresh page. i always pass parameter's with Session to new page, then at page load set ViewState['aaa']=Session['aaa'] and clear Sessions.

...I hope be useful...




回答4:


Do this it is very easy and effective

Intead of giving IsPostBack in the page load(),please provide inside the button click (To send or insert data)

Call the same page again after reseting all input values

 protected void Btn_Reg_Click1(object sender, EventArgs e)
    {
        try
        {
            if (IsPostBack)
            {
                Registration_Save();
                Send_Mail();
                txtEmail.Text = "";
                txtname.Text = "";
                Response.Redirect("~/index.aspx");
            }


        }
        catch (Exception) { }

    }

You won't see any server messages after refreshing the page..



来源:https://stackoverflow.com/questions/11511481/how-to-avoid-duplicate-entry-from-asp-net-on-postback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!