How to avoid duplicate entry from asp.net on Postb

2019-05-28 12:27发布

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.

4条回答
放荡不羁爱自由
2楼-- · 2019-05-28 13:00

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;
      }
   }
}
查看更多
Luminary・发光体
3楼-- · 2019-05-28 13:07

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.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-05-28 13:13

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..

查看更多
淡お忘
5楼-- · 2019-05-28 13:14

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...

查看更多
登录 后发表回答