2011-08-22 78 views
0

我正在開發一個應用程序,該應用程序在動態生成的ASP.net頁面上具有GridView項目,並在網格視圖中更新項目時執行部分回傳。這部分回傳導致標籤索引丟失或至少被忽略,因爲標籤順序似乎重新啓動。網格視圖本身已經具有捕獲的預渲染,以便從網格視圖中的修改項目計算新值。有沒有辦法在預渲染調用之前獲取哪些元素具有頁面焦點?發件人對象是網格視圖本身。失去焦點的實際元素

回答

0

您可以嘗試使用此功能,該功能將返回導致回發的控件。有了這個,你應該可以重新選擇它,或找到下一個標籤索引。

private Control GetControlThatCausedPostBack(Page page) 
    { 
     //initialize a control and set it to null 
     Control ctrl = null; 

     //get the event target name and find the control 
     string ctrlName = Page.Request.Params.Get("__EVENTTARGET"); 
     if (!String.IsNullOrEmpty(ctrlName)) 
      ctrl = page.FindControl(ctrlName); 

     //return the control to the calling method 
     return ctrl; 
    } 

下面是一個動態生成輸入的實例,它通過AJAX更改總數。我使用此代碼根據導致回發的控件的選項卡索引來確定下一個選項卡索引。很顯然,這段代碼是根據我的用法量身定製的,但經過一些調整,我認爲它也可以適用於您。

int currentTabIndex = 1; 
WebControl postBackCtrl = (WebControl)GetControlThatCausedPostBack(Page);     

foreach (PlaceHolder plcHolderCtrl in pnlWorkOrderActuals.Controls.OfType<PlaceHolder>()) 
{ 
    foreach (GuardActualHours entryCtrl in plcHolderCtrl.Controls.OfType<GuardActualHours>()) 
    { 
     foreach (Control childCtrl in entryCtrl.Controls.OfType<Panel>()) 
     { 
      if (childCtrl.Visible) 
      { 
       foreach (RadDateInput dateInput in childCtrl.Controls.OfType<RadDateInput>()) 
       { 
        dateInput.TabIndex = (short)currentTabIndex; 
        if (postBackCtrl != null) 
        { 
         if (dateInput.TabIndex == postBackCtrl.TabIndex + 1) 
          dateInput.Focus(); 
        } 
        currentTabIndex++; 
       } 
      }       
     } 
    } 
}