2011-08-22 62 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++; 
       } 
      }       
     } 
    } 
}