We help IT Professionals succeed at work.

How to handle page refresh in ASP.NET

countrymeister
countrymeister asked
on
I have a ImageButton(submit) and a FileUpload controls on my ASP.NET page.
When the user upload a file and clicks the Image button it gets into the Submit_File button click which is tied to the ImageButton click event.

When the user refreshes the page it also gets into the submit click event which I want to prevent.

How can I do this

I tied the following in the code behind, and this prevents the refresh, but prevents a new file being processed, bcos after the first submit and any subsequent refreshes the Session["Submitted"] = true
if (!Page.IsPostBack)
        {
if (Session["Submitted"] == null)
                Session.Add("Submitted", false);
            else
                Session["Submitted"] = false;
}

submit_click event(object sender, eventargs e)
{
 if ((Session["Submitted"] == null) || (Convert.ToBoolean(Session["Submitted"]) == false))
        {
                    }
        else { return; }

processData()
 Session.Add("Submitted", true);
}

I also tried adding a second hidden button with a click event but it always gets into the Submit_file click
Comment
Watch Question

Shaun KlineLead Software Engineer

Commented:
If you do not need to display any confirmation message on the page, you can use a Response.Redirect back to your page. Or, you can provide a confirmation page which you would redirect to after processing the button submit event.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi countrymeister,
In your page directives, check if AutoEventWireUp is set to true or not? if it is true, set it to false. 
<%@ Page AutoEventWireup="false" %>
Regards,
Chinmay


Author

Commented:
Shaun_Kline:
I do not want to redirect to another page

Chinmay_Patel:
AutoEventWireUp is false
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Alright. try this
Once the user has uploaded the file. Set a session variable saying the file is uploaded, maybe a flag that indicates that file is uploaded and then do a response.redirect to your page itself.
In page load, check for this flag. If it is there then display a message to your end user saying that file is uploaded and clear it from the session.
Regards,
Chinmay


Author

Commented:
Chinmay Patel,

I also want to poin out that I am using Master pages, and in there the AutoEentfire up is true
If I set AutoEvent = true in my content page is there a solution for this
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
No It should be false as such. Kindly go through my previous post it will work for sure.

Author

Commented:
Chinmay ,  your solution did work.
I added the following in page load without checking if this is  a posback or not

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Submitted"] != null)
        {
            Session.Remove("Submitted");
        }
     
        if (!Page.IsPostBack)
        {
             blah blah
         }
}

I am curious why did I have to do a response.redirect what are the effects of that
Chief Technology Ninja
Distinguished Expert 2018
Commented:
It resets your page :) Sorry but its a crude way of achieving results but I didn't want you to write any complex logic. There are other logics available on internet just do a quick search with your favorite engine and you will know.
Basically.. I am resetting your page but your session doesn't reset so we are good to go :)