【问题标题】:Maintain Focus after onfocus__doPostBack in asp.net在 asp.net 中的 onfocus__doPostBack 之后保持焦点
【发布时间】:2013-02-07 19:28:49
【问题描述】:

当用户点击文本框时,我必须执行服务器端事件。到目前为止,我可以使用 onfocus 事件来管理调用 javascript 函数,但是如果我尝试删除 onfocus,执行一些代码,将焦点放回控件上,然后它会在无限循环中再次触发 onfocus 事件。下面的示例...

HTML

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" onfocus="CallServer(this);"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" onfocus="CallServer(this);"></asp:TextBox>

Javascript

function CallServer(obj) {
  if (obj != "") {
    var control = document.getElementById(obj.id)
    __doPostBack(obj.id, "onfocus");
  }
}

代码隐藏

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

   If IsPostBack Then
      Dim target As String = Page.Request.Params.Get("__EVENTTARGET")
      Dim eventarg As String = Page.Request.Params.Get("__EVENTARGUMENT")
      Dim PostControl As Control = Nothing

      If target <> "" Then
         PostControl = Page.FindControl(target)
      End If

      If eventarg = "onfocus" Then
         CType(PostControl, TextBox).Attributes("onfocus") = "null"

         ...do some code

         Page.SetFocus(PostControl)

         CType(ctrl, TextBox).Attributes.Add("onfocus", "CallServer(this)")
      End If
   End If

End Sub

【问题讨论】:

    标签: javascript asp.net vb.net visual-studio-2010


    【解决方案1】:

    您可以通过使用 Page_PreRender 中的 ScriptManager.SetFocus 来完成此操作。本例中的 ScriptManager 位于 MasterPage 中。

    Private Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
    
        If HiddenFieldPostBackControl.Value <> "" Then
            Dim PostBackControl As Control = FindControlById(HiddenFieldPostBackControl.Value)
            If PostBackControl IsNot Nothing Then
                Dim sm As ScriptManager = ScriptManager.GetCurrent(Master.Page)
                sm.SetFocus(PostBackControl )
            End If
        End If
    
    End Sub
    

    注意:如果您正在使用动态控件(必须在每次回发时重新创建),则必须在将事件添加到其他控件时删除目标控件的 (OnFocus of OnFocusIn) 事件。这必须在 Page_PreRender 之前完成。

    function ControlOnFocus(ctrl, request, doc, min, max, docSec) {
            if (ctrl != "") {
                var hf = document.getElementById("MainContent_HiddenFieldPostControl").value = ctrl.id;
                var c = document.getElementById(ctrl.id);
                PostControl = ctrl;
    
                //ONFOCUS
                if (request.toLowerCase() == 'server') {
                    //SERVER Handler
                    __doPostBack(ctrl.id, "onfocus");
                } else {
                    //CLIENT Handler
                    //Your code here...
                }
    
                ReAssignEvent(ctrl.id);
    
                var hf = document.getElementById("MainContent_hfValidated");
                hf.innerText = "";
            };
        };
     function ReAssignEvent(ControlID) {
            //loop through an array with (id,onfocus event) for each control
            //this array is only required for the focus event
    
            for (var i = 0; i < FocusArray.length; i++) {
    
                var carr = FocusArray[i].split("[1]"); //split the element to get id and event
                var carrId = carr[0]; //get the id
                var c = document.getElementById(carrId); //find control and assign control to var
                var e = carr[1]; //assing event as string to var
    
                if (c != null) {
                    if (ControlID!= carrId) {
                        if (c.type == "text" || c.type == "select-one") {
                            c.setAttribute("onfocus", e);
                        } else {
                            c.setAttribute("onfocusin", e);
                        }
                    } else {
                        if (c.type == "text" || c.type == "select-one") {
                            c.removeAttribute("onfocus");
                        } else {
                            c.removeAttribute("onfocusin");
                        }
                    }
                }
    
            }
     }
    

    【讨论】:

      猜你喜欢
      • 2019-07-01
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      相关资源
      最近更新 更多