views:

1323

answers:

3

Hi All,

I have a WebPage on which I have to detect and prevent usage of Browser Refresh Button. (Now Please, dont suggest me Response.Redirect, I wont be able to use it in this scenario).

On looking at this page http://aspalliance.com/687_Preventing_Duplicate_Record_Insertion_on_Page_Refresh.4 I found the way. I'm planning to put this idea in a Control and place the control on every page.

Now that my page contains so many buttons and other controls. My concern is, If it is a refresh post... I dont want any events to get fired...

It will be tedious to go and check whether it is a refresh post in the beginning of every event as my entire application is almost built.

Any ideas that would help me.

  • Raja

Including the below text for sake of more clarity :

Hi All, I hope a little misunderstanding... I dont want to stop user pressing the Refresh button... But all I want is to adjust my application's response accordingly... Imagine a scenario, when user clicks BUTTON-A a popup window opens with another page. Now when the user comes back and clicks refresh button in the main window, the click event of BUTTON-A is fired again and popup window is opened again... In such scenario, I want to refresh the page as such, without opening the popup window. so, I need to stop ASP.NET from firing the click event of BUTTON-A (or any other similar buttons)

+4  A: 

I know you're not going to want to hear this but users expect to be able to hit the refresh button. Breaking something they like will make them unhappy. They'll blame you and your name will be mud.

Just think about those sites that try to block the Back button: do you like them?

serialhobbyist
I understand... But in this page, I myself is providing a Refresh Button, which the user can use to refresh the contents... I want to block the button click and other events from getting fired again.
The King
@The King- what's so good about your own Refresh Button vs the one built into the browser?
RichardOD
My own refresh button just reloads the page.. and will not fire the last controls event again.
The King
Well, I can see you're trying but personally, if a page was giving me trouble, I'd hit F5 without even thinking about it. I wouldn't go looking round the page for a button. And even it was in the middle, right in the way, I'd still hit F5 because hitting a key is always easier than using a trackpad to centre a mouse on a button and then clicking. Really, you shouldn't be trying to break the browser - people won't be happy.
serialhobbyist
A: 

You cannot prevent the user from pressing the Refresh button. It's build in functionality in the browser that resends the previous request, get or post. It's just there.

That is why that you normally make a redirect after a post to a get (e.g. the user posts to user/1/edit and the response redirects to user/1/view), so that a refresh will not cause double post.

I'm sorry that this is not what you want to hear, but when making a web application, you should try to follow web standards and let the user be able to use those browser features that he/she expects: back, forward, and refresh. And I know that your application is almost finished.

But if you start creating hacks for preventing refresh, or other stuff where you're not flowing with the technology, but going up against the stream, your application will start carrying around a bad package, and as the application lives on and is extended, this bad package is going to be a burden for further development.

Pete
Hi Pete, I hope a little misunderstanding... I dont want to stop user pressing the Refresh button... But all I want is to adjust my application's response accordingly... Imagine a scenario, when user clicks BUTTON-A a popup window opens with another page. Now when the user comes back and clicks refresh button in the main window, the click event of BUTTON-A is fired again and popup window is opened again... In such scenario, I want to refresh the page as such, without opening the popup window. so, I need to stop ASP.NET from firing the click event of BUTTON-A (or any other similar buttons)
The King
In that particular case, I would place the javascript to open the new window directly as an event handler to the button, i.e. not making it a server control. Sounds to me that you are placing a server control with an event handler that injects a javascript to open a new page on the reload? That particular case should never be a postback
Pete
by "I would place the javascript to open the new window directly as an event handler to the button" I mean a client side javascript event handler
Pete
+1  A: 

This is at least a starting point on how you can do it. I'm not sure all logic is 100%, but it is something to begin with...

protected void Page_Load(object sender, EventArgs e)
{
    foreach (Control control in Controls)
    {
        DisableEvent(control);
    }
}
protected void Page_PreRender(object sender, EventArgs e)
{
    foreach (Control control in Controls)
    {
        UpdateViewstate(control);
    }
}
private void DisableEvent(Control current)
{
    foreach (Control control in current.Controls)
    {
        if (control.GetType() == typeof(Button))
        {
            if (IsPostBack)
            {
                if (Session["update" + control.ID].ToString() != ViewState["update" + control.ID].ToString())
                {
                    RemoveClickEvent((Button)control);
                }
                else
                {
                    ((Button)control).Click += new EventHandler(Button_Disable);
                }
            }
            else
            {
                Session["update" + control.ID] = Server.UrlEncode(System.DateTime.Now.ToString());
            }
        }
        DisableEvent(control);
    }
}
private void UpdateViewstate(Control current)
{
    foreach (Control control in current.Controls)
    {
        if (control.GetType() == typeof(Button))
        {
            ViewState["update" + control.ID] = Session["update" + control.ID];
        }
        UpdateViewstate(control);
    }
}

void RemoveClickEvent(Button b) {
    System.Reflection.FieldInfo f1 = typeof(Button).GetField("EventClick", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic); 
    object obj = f1.GetValue(b);
    System.Reflection.PropertyInfo pi = typeof(Button).GetProperty("Events", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    System.ComponentModel.EventHandlerList list = (System.ComponentModel.EventHandlerList)pi.GetValue(b, null); 
    list.RemoveHandler(obj, list[obj]); 
}

protected void Button_Disable(object sender, EventArgs e)
{
    Button b = (Button)sender;
    Session["update" + b.ID] = Server.UrlEncode(System.DateTime.Now.ToString());
}
awe