MOZILLA
MOZILLA

Reputation: 5980

Refresh the page after a postback action in asp.net

I have command button added in my asp.net grids. After performing an action using that button, we refresh the grid to reflect the new data. (basically this action duplicates the grid row).

Now when user refresh the page using F5, an alert message is displayed (to resend the information to server) if we select "retry", the action is repeated automatically.

I know this is a common problem in asp.net, how can we best handle this?

Upvotes: 18

Views: 64414

Answers (7)

user2911651
user2911651

Reputation: 1

for example: if you click on 'button' system will catch the event 'button_click'. if you refresh the page, system will re execute again the same event. to don t have this problem, in your event insert : on your event

private void button_click(object sender, System.EventArgs e)
{
    button.Enabled =false;
    button.Enabled =true;
}

is what you meant?

Upvotes: -1

pistolilla
pistolilla

Reputation:

Inside your <asp:Repeater> tag put this:

EnableViewState="false"

This will cause your control to refresh every time the page loads, no matter if it's a postback or not.

Upvotes: 1

Rune Grimstad
Rune Grimstad

Reputation: 36340

The problem is that asp.net buttons perform form posts when you push a button. If you replace the button with a link your problem should go away. You can also use a button that performs a javascript function that sets the document.location to the address of your page.

Upvotes: 2

Barbaros Alp
Barbaros Alp

Reputation: 6434

You need to call response.Redirect(Request.Url.ToString());

or you can wrap the grid with updatepanel and after every command bind the datasource to grid

Upvotes: 1

chris
chris

Reputation: 37490

Search for GET after POST - http://en.wikipedia.org/wiki/Post/Redirect/Get - basically, redirect to the current page after you're finished processing your event.

Something like:

Response.Redirect(Request.RawUrl)

Upvotes: 38

Micha&#235;l Carpentier
Micha&#235;l Carpentier

Reputation: 2135

If I well understood, you simply have to check if you are in a post-back situation before populating your grid.
Assuming you do that on Page_Load, simply surround the operation with post-back test like this:

private void Page_Load(object sender, EventArgs e)
{
    if(!this.IsPostBack)
    {
        // populate grid
    }
}

Upvotes: 1

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422320

If you think you don't need postback paradigm, you might want to look at ASP.NET MVC.

Upvotes: 2

Related Questions