Wesley
Wesley

Reputation: 5620

Can I handle PostBack and refresh differently?

This is a general question I haven't seen an answer for.

Can I tell the difference between PostBack and refresh in code to ensure people don't submit the same item repeatedly?

Application is C# based ASP.NET. Code below:

protected void SubmitListItem(object sender, EventArgs e)
{
    if (Page.IsPostBack) //Fires on both submit and F5
    {
        SPUser user = web.CurrentUser;
        string alias = user.Email.Substring(0, user.Email.IndexOf('@'));
        if (ListChoice.SelectedItem.Text == "comment")
        {
            SPList TargetList = web.Lists.TryGetList("Offer Comments");
            SPListItem item = TargetList.Items.Add();
            item["Title"] = TitleBox.Text;
            item["Body"] = BodyBox.Text;
            item["OfferID"] = OfferID;
            item["Alias"] = alias;
            item.SystemUpdate();
            TargetList.Update();
            LoadOffers();
        }
        else
        {
            SPList TargetList = web.Lists.TryGetList("Offer Best Practices");
            SPListItem item = TargetList.Items.Add();
            item["Title"] = TitleBox.Text;
            item["Body"] = BodyBox.Text;
            item["OfferID"] = OfferID;
            item.SystemUpdate();
            TargetList.Update();
            LoadOffers();
        }
    }
}

Upvotes: 2

Views: 641

Answers (1)

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68747

IsPostBack

Although hitting F5 will send a post request if the previous request was a post. So you'll need to make sure to handle that case as well. The worst case scenario is when a user hits a button repeatedly, sending multiple post requests at the same time. Usually handled by disabling the button when its clicked. It will work differently on different browsers/devices, so you'll need to specify your audience.

Upvotes: 4

Related Questions