c# - How to maintain the focus on same control after asynchronous postback? -
i have 3 textboxes , 1 in updatepanel refresh every 4 seconds. during refresh controls not in update panel loosing focus. want keep focus on controls.
here code:
<asp:updatepanel id="upnlchat" runat="server"> <contenttemplate > <asp:textbox id="txtchatbox" readonly="true" textmode="multiline" cssclass="mymultitextboxclass" runat="server"></asp:textbox> </contenttemplate> <triggers> <asp:asyncpostbacktrigger controlid="timerchat" /> </triggers> </asp:updatepanel> <asp:timer id="timerchat" interval="4000" runat="server" ontick="timerchat_tick" ></asp:timer> <table> <tr><td>user: </td><td colspan="2"><asp:textbox id="txtuser" runat="server" ></asp:textbox><br /></td> <td> <asp:requiredfieldvalidator id="requiredusername" runat="server" errormessage="username required" controltovalidate="txtuser" ></asp:requiredfieldvalidator> </td></tr> <tr><td>message: </td><td><asp:textbox id="txtmsg" cssclass="mymsg" textmode="multiline" onkeydown="if (event.keycode == 13) { btnsend.focus(); this.form.submit(); }" runat="server"></asp:textbox></td> <td colspan="2"><asp:button id="btnsend" runat="server" onclick="btnsend_click" onclientclick="scroll()" text="send" /> </td></tr> </table>
can please me?
here modified version of @darshan's script uses jquery. solution because 1 script work updatepanels don't have write code each , every updatepanel.
my modifications are:
- uses jquery
- performs null checks
- doesn't pollute global namespace
- edit: tabbing works expected textboxes autopostback.
/* retain element focus after updatepanel postback ***************************************************/ jquery(function ($) { var focusedelementid = ""; var prm = sys.webforms.pagerequestmanager.getinstance(); prm.add_beginrequest(function (source, args) { var fe = document.activeelement; if (fe != null) { focusedelementid = fe.id; } else { focusedelementid = ""; } }); prm.add_endrequest(function (source, args) { if (focusedelementid != "") { $("#" + focusedelementid).focus(); } }); });
chosen.jquery
this version work chosen jquery plugin.
/* retain element focus after updatepanel postback ***************************************************/ jquery(function ($) { var focusedelementselector = ""; var prm = sys.webforms.pagerequestmanager.getinstance(); prm.add_beginrequest(function (source, args) { var fe = document.activeelement; focusedelementselector = ""; if (fe != null) { if (fe.id) { focusedelementselector = "#" + fe.id; } else { // handle chosen js plugin var $chzn = $(fe).closest('.chosen-container[id]'); if ($chzn.size() > 0) { focusedelementselector = '#' + $chzn.attr('id') + ' input[type=text]'; } } } }); prm.add_endrequest(function (source, args) { if (focusedelementselector) { $(focusedelementselector).focus(); } }); });
Comments
Post a Comment