在行内按钮单击后,GridView将滚动回到顶部

时间:2014-08-25 06:50:43

标签: asp.net gridview scroll position state

我在GridView内有UpdatePanel。 GridView(GV)包含行内按钮。如果GV列表很长并按下底行的按钮,页面会滚动到顶部,我找不到我点击的按钮。

我尝试过MaintainScrollPositionOnPostback="true",但由于我使用的是UpdatePanel,这似乎无法正常工作。

我希望屏幕保留在我单击按钮的行上。

1 个答案:

答案 0 :(得分:0)

设置UpdatePanel UpdateMode="Always"

添加此功能:

  public static string GetPostBackControlId(Page page)
    {
        Control control = null;
        // first we will check the "__EVENTTARGET" because if post back made by the controls
        // which used "_doPostBack" function also available in Request.Form collection.
        string controlName = page.Request.Params["__EVENTTARGET"];
        if (!String.IsNullOrEmpty(controlName))
        {
            control = page.FindControl(controlName);
        }
        else
        {
            // if __EVENTTARGET is null, the control is a button type and we need to
            // iterate over the form collection to find it

            // ReSharper disable TooWideLocalVariableScope
            string controlId;
            Control foundControl;
            // ReSharper restore TooWideLocalVariableScope

            foreach (string ctl in page.Request.Form)
            {
                // handle ImageButton they having an additional "quasi-property" 
                // in their Id which identifies mouse x and y coordinates
                if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
                {
                    controlId = ctl.Substring(0, ctl.Length - 2);
                    foundControl = page.FindControl(controlId);
                }
                else
                {
                    foundControl = page.FindControl(ctl);
                }

                if (!(foundControl is Button || foundControl is ImageButton)) continue;

                control = foundControl;
                break;
            }
        }
        return control == null ? String.Empty : control.ID;
    }

page_load中使用它来获取回发controlId:

if (IsPostBack)
 {
 string controlName = GetPostBackControlId(this);
}