2013-02-07 27 views
0

我必須執行服務器端事件,當用戶點擊文本框的。到目前爲止,我還可以管理呼叫使用onfocus事件JavaScript函數,但如果我嘗試刪除聚焦狀態,做了一些代碼,放在一個死循環再次聚焦回到了控制然後重新觸發onfocus事件。下面的示例...保持注意力集中onfocus__doPostBack後在asp.net

的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 

回答

0

您可以通過在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 

注:如果您使用動態控件(這對每一個回發重新創建)的工作,你將不得不刪除(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"); 
        } 
       } 
      } 

     } 
} 
相關問題