how to set focus on textbox after post back in asp.net

巧了我就是萌 提交于 2019-12-10 15:24:49

问题


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 .


回答1:


I suggest you to try with SetFocus method server side

Page.SetFocus(IdOfControl);


来源:https://stackoverflow.com/questions/12895098/how-to-set-focus-on-textbox-after-post-back-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!