失去焦点的实际元素

时间:2011-08-22 19:43:07

标签: c# asp.net

我正在开发一个在ASP.net页面上有一个GridView项目的应用程序,该项目是动态生成的,并且在网格视图中更新项目时会进行部分回发。这种部分回发导致选项卡索引丢失或至少被忽略,因为选项卡顺序似乎重新启动。网格视图本身已经具有捕获的预渲染,以便从网格视图中的已修改项目计算新值。有没有办法在预渲染调用之前获取哪个元素具有页面焦点?发件人对象是网格视图本身。

1 个答案:

答案 0 :(得分: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++;
                }
            }                        
        }
    }
}
相关问题