Question

I have a Ajax enabled asp form on which user can submit data.

It works fine but when he/she presses f5 or refresh page, form resubmits. This is a common question and duplicate as well.

I have read so many questions on different forums about this problem and implemented some but problem still exists.

I cant do redirection of page as per client requirement. I have taken reference from this question. It worked fine on local machine but when I deployed page was not submitting even once. I gave some of my effort too, as follows`

HiddenField hf = new HiddenField();

 protected void Page_Load(object sender, EventArgs e)
    { 

        if (!IsPostBack)
        {
           createToken();    //to check page is refreshed
        }

    }

public void createToken()
    {
        string token = System.Guid.NewGuid().ToString();
        Session["token"] = token;
        hf.Value = token;
    }
    private bool TokenIsValid()
    {
        string expectedToken = (string)Session["token"];
        if (expectedToken == null)
            return false;

        string actualToken = hf.Value;

        return expectedToken == actualToken;
    }

 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (TokenIsValid()) // check that page is not refreshed by browser.
            {
                //insert logic
        }}

        catch (Exception ex)
        {


        }

there is a Dropdownlist which is causing big problem of autopostback and autopostback can't be disable so value of hidden field is flushing out. I have no idea how to prevent resubmission.

Was it helpful?

Solution

I solved it by passing success message through session, write that message on page_load AND nullified that session if page is fresh one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top