2012-09-06 45 views
0

以下是方案,即使在回發後,我仍需要在頁面中維護標籤順序。即使在回發之後仍然維護標籤順序

我有一個帶有3個文本框的用戶控件,這個控件放在一個帶有其他控件的aspx頁面中。在測試框的onBlur中,我正在做一個__doPostBack將一些數據傳遞給服務器,並在父頁面上處理這個數據,因此Tab鍵順序丟失了。是否有可能在回發中維護標籤順序,其中我從父頁面到控件中的文本框進行選項卡標籤,然後再次標籤到下一個父頁面控件。

我嘗試使用下面的代碼將焦點設置到usercontrol中的下一個文本框中,但是這會導致代碼被調用,即使用戶只是通過單擊頁面而將焦點置於控件之外。

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (this.IsPostBack) 
     { 
      WebControl wcICausedPostBack = (WebControl)GetControlThatCausedPostBack(sender as UserControl); 
      if (wcICausedPostBack != null) 
      { 
       int indx = wcICausedPostBack.TabIndex; 
       Panel selectedPanel = null; 
       if (Panel1.Visible) 
       { 
        selectedPanel = Panel1; 
       } 
       else if (Panel2.Visible) 
       { 
        selectedPanel = Panel2; 
       } 
       if (selectedPanel != null) 
       { 

        var ctrl = from control in selectedPanel.Controls.OfType<WebControl>() 
           where control.TabIndex == (indx + 1) 
           select control; 
        if (ctrl.Count() > 0) 
        { 
         ctrl.First().Focus(); 
        } 
       } 
      } 
     } 
    } 

    protected Control GetControlThatCausedPostBack(UserControl page) 
    { 
     Control control = null; 
     string ctrlname = string.Empty; 
     UserAction updateAction = CurrentUserAction(); 
     if (updateAction != null) 
     { 
      if (!string.IsNullOrEmpty(updateAction.Data)) 
      { 
       string[] splitSting = updateAction.Data.Split('$'); 
       ctrlname = splitSting[splitSting.Count() - 1]; 
      } 
     } 
     if (ctrlname != null && ctrlname != string.Empty) 
     { 
      control = page.FindControl(ctrlname); 
     } 
     else 
     { 
      foreach (string ctl in page.Request.Form) 
      { 
       Control c = page.FindControl(ctl); 
       if (c is System.Web.UI.WebControls.Button || c is System.Web.UI.WebControls.ImageButton) 
       { 
        control = c; 
        break; 
       } 
      } 
     } 
     return control; 
    } 

回答

0

您可以使用jQuery將當前焦點字段ID存儲在隱藏控件中。

將其存儲在隱藏的控件中可以在回發期間保持它。
您可以使用該輸入ID來查找下一個輸入並使用jQuery再次將焦點設置爲它。

$("input").focus(function() { 
    $("#currectFocusField").val($(this).attr("id")); 
}); 

$(document).ready(function(){ 
    var lastFocusedInput = $("#currectFocusField").val(); 
}); 
+0

您提供的代碼,這將被稱爲總是,但我只需要從文本框中切換時設置焦點。 – Arvind

+0

不,它並不總是被調用。它將在您列出(重點放在下一個)輸入和準備好(在回發之後)時被調用, – nunespascal

相關問題