6

i have a textbox in update panel. when a user type something i fetch related data from database and fill that in another textbox. My problem is that after autopostback focus on any of the textboxs is lost. How can i manage this using javascript or code because i used both like in code i used

 System.Web.UI.ScriptManager.GetCurrent(this).SetFocus(this.txtReference);

and javascript i find one more that is

    <script type="text/javascript">
    var postbackElement;
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(beginRequest);
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);


    function beginRequest(sender, args) {
        postbackElement = args.get_postBackElement();
    }


    function pageLoaded(sender, args) {
        var updatedPanels = args.get_panelsUpdated();
        if (typeof (postbackElement) === "undefined") {
            alert('if Loop');
            return;
        }
        else if (postbackElement.id.toLowerCase().indexOf('button1') > -1) {
        alert('else');
            for (i = 0; i < updatedPanels.length; i++) {

                document.getElementById('<%= txtAcctNo.ClientID %>').focus();
            }
        }


    }
</script>

but not working because 'button1 undefined'. What i place there because all event performed on OnTextChanged="" in aspx page.

So please help me through code or javascript how can i do this .

techBeginner
  • 3,792
  • 11
  • 43
  • 59
A.Goutam
  • 3,422
  • 9
  • 42
  • 90

1 Answers1

12

I suggest you to try with SetFocus method server side

Page.SetFocus(IdOfControl);
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • 1
    Happy to help you Chuck Norris – Aghilas Yakoub Aug 14 '13 at 12:39
  • The problem with this is that if the page is long and that textbox is in the middle somewhere, this will move the page so that textbox is at the lowest part of the visible page. Not the behavior one wants - partial page updates are bout preserving everything else on the page the way it is, especially the scroll position on the page. –  Apr 05 '16 at 14:06