views:

4632

answers:

2

Duplicate of Asp.Net Button Event on refresh fires again??? GUID?

hello, ive a website and when a user click a button and the page postback, if the user refresh the Page or hit F5 the button method is called again.

any one know some method to prevent page refresh with out redirect the page to the same page again ?

something like if (page.isRefresh) or something... or if exist any javascript solution is better.

this seen to works.... but when i refresh it does not postback but show the before value in the textbox

http://www.dotnetspider.com/resources/4040-IsPageRefresh-ASP-NET.aspx

private Boolean IsPageRefresh = false;
protected void Page_Load(object sender, EventArgs e)
{        
    if (!IsPostBack)
    {
        ViewState["postids"] = System.Guid.NewGuid().ToString();
        Session["postid"] = ViewState["postids"].ToString();
        TextBox1.Text = "Hi";

    }
    else
    {
        if (ViewState["postids"].ToString() != Session["postid"].ToString())
        {
            IsPageRefresh = true;
        }
        Session["postid"] = System.Guid.NewGuid().ToString();
        ViewState["postids"] = Session["postid"];
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    if (!IsPageRefresh) // check that page is not refreshed by browser.
    {
        TextBox2.Text = TextBox1.Text + "@";

    }
}
+2  A: 

You can test for the Page.IsPostBack property to see if the page is responding to an initial request or if it's handling a PostBack such as your button click event. Here's a bit more information: w3schools on IsPostBack

Unfortunately that's not going to solve your problem since IsPostBack will be true when the user clicks the button as well as when they refresh the page after the button action has taken place.

If you're doing a task like performing CRUD on some data, you can Response.Redirect the user back to the same page when you're done processing and get around this problem. It has the side benefit of reloading your content (assuming you added a record to the DB it would now show in the page...) and prevents the refresh problem behavior. The only caveat is they still resubmit the form by going back in their history.

Postbacks were a bad implementation choice for the Asp.net and generally are what ruin the Webforms platform for me.

John
Response.Redirect doesn't get around the problem. A user can still go back with the back button in a browser and refresh at the piont where they had submitted that form and it will still send another request to the DB.
CoffeeAddict
A: 

Thanks for comments and sorry for my mistake, I found this code in: http://www.codeproject.com/KB/aspnet/Detecting_Refresh.aspx And this time tested ;)

private bool _refreshState; private bool _isRefresh;

protected override void LoadViewState(object savedState)
{
    object[] AllStates = (object[])savedState;
    base.LoadViewState(AllStates[0]);
    _refreshState = bool.Parse(AllStates[1].ToString());
    _isRefresh = _refreshState == bool.Parse(Session["__ISREFRESH"].ToString());
}

protected override object SaveViewState()
{
    Session["__ISREFRESH"] = _refreshState;
    object[] AllStates = new object[2];
    AllStates[0] = base.SaveViewState();
    AllStates[1] = !(_refreshState);
    return AllStates;
}

protected void btn_Click(object sender, EventArgs e)
{
    if (!_isRefresh)
        Response.Write(DateTime.Now.Millisecond.ToString());
}
Saber
this does not work for Refresh, when you refresh the page it tells the browser to repeat the last event
jmpena
That just stops the button from working more than once, regardless if the post comes from a reload or an actual button click.
Guffa
yeah this works. its the same code that i found, but when you click the button and then hit refresh it show "HI" and not the new value. :P i think i can live with, ill try to modify this. thanks
jmpena
ok, sorry its not the same that i found!
jmpena
but what if viewstate is disabled in the page directive?
CoffeeAddict