1

I have a series of controls on a page, including some textboxes that serve to record employee timesheet. When OnTextChanged fires, the page postback and update the overall working hours and minutes.

The problem is, when the user clicks the save button, a postback happens, but it is not because of save button's action but because OnTextChanged has fired. The user believes that the saving has gone well until he/she access the page again and doesn't find his/her data. It's next to the impossible to explain to user that they need to click twice because the first time the textbox loses focus and the second time it's the right one.

Is there a way to store the value of the last element that had the focus before the postback occured? I've tried to access the value of __LASTFOCUS, but I'm getting an empty string.

string lastFocus = Page.Request.Params.Get("__LASTFOCUS"); 

Thanks for helping

8
  • You could slap an update panel bandaid on the textbox.
    – asawyer
    Dec 19, 2012 at 16:46
  • This is confusing: "when the user clicks the save button, a postback happens, but it is not because of save button's action but because OnTextChanged has fired". Dec 19, 2012 at 16:46
  • @TimSchmelter OnTextChanged causes a postback when the textbox loses focus. I think what he is describing is that the user types a number, then clicks Save and mistakes the OnTextChanged postback for the form submit postback. edit - Unless I am not remembering how that event works correctly.
    – asawyer
    Dec 19, 2012 at 16:47
  • @asawyer: "causes a postback when the textbox loses focus" No, only if you set AutoPostBack="true". But i assume that it's "false" since it's triggered only if the user clicked the save-button. Dec 19, 2012 at 16:50
  • @TimSchmelter Ah yes that's right. Well I would assume then that this is the situation then. Richard77 would have to let us know.
    – asawyer
    Dec 19, 2012 at 16:51

2 Answers 2

2

If you are trying to grab the last control that lost focus which has AutoPostBack="true", I believe you can get the Name of this control from Request.Form.Get("__EVENTTARGET"). This form variable contains the ID of any control invoking postback (in most, if not all, scenarios).

2
  • After the user enters the value to the textbox, he then clicks the save button. However, the textbox fires a OnTextChanged that causes the postback. __EVENTTARGET is giving me the name of the text. What I want is the name of the save button.
    – Richard77
    Dec 19, 2012 at 20:32
  • Why? Why do you have AutoPostBack turned to on in this case? Dec 20, 2012 at 0:58
0

I have a similar situation with a GridView with an arbitrary amount of textboxes, each with AutoPostBack on and OnTextChanged event handlers. What I wanted to do was be able to tab out of a textbox, postback, then restore focus to the textbox that had focus before postback e.g. the textbox that I tabbed to.

This is what I ended up with:

function RestoreFocus(source, args) {
        var val = $("#<%=postbackFocusID.ClientID %>").attr("value");
        var el = document.getElementById(val);
        if (el != null)
            el.focus();
    }
    function PersistElementThatHasFocus(source, args) {
        $("#<%=postbackFocusID.ClientID %>").attr("value", document.activeElement.id);
    }
    function AddRequestHandler() {
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_endRequest(RestoreFocus);
        prm.add_beginRequest(PersistElementThatHasFocus);
    }

The postbackFocusID is just an asp:HiddenField.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.