简体   繁体   English

自定义ASP.NET控件中的Begin / EndRequestHandler多个处理程序

[英]Begin/EndRequestHandler multiple handlers in custom ASP.NET controls

I have a custom ASP.NET control, a dropdown/treeview, which needs to add Begin(End-)RequestHandlers to prevent scrolling to the top of container during UpdatePanel partial postbacks (as described here ). 我有一个自定义ASP.NET控制,下拉/树状,其需要添加开始(端值)RequestHandlers防止UpdatePanel的期间滚动到容器的顶部部分回发(如所描述的在这里 )。 They are added dynamically like so: 它们是动态添加的,如下所示:

Page.ClientScript.RegisterStartupScript(this.GetType(), this.ClientID, string.Format(@"
                  var xPos, yPos;
                  var prm = Sys.WebForms.PageRequestManager.getInstance();

                  function BeginRequestHandler(sender, args) {{
                    if ($get('{0}') != null) {{
                      // Get X and Y positions of scrollbar before the partial postback
                      xPos = $get('{0}').scrollLeft;
                      yPos = $get('{0}').scrollTop;
                    }}
                 }}

                 function EndRequestHandler(sender, args) {{
                     if ($get('{0}') != null) {{
                       // Set X and Y positions back to the scrollbar
                       // after partial postback
                       $get('{0}').scrollLeft = xPos;
                       $get('{0}').scrollTop = yPos;
                     }}
                 }}

                 prm.add_beginRequest(BeginRequestHandler);
                 prm.add_endRequest(EndRequestHandler);  ", this.ItemsContainer.ClientID), true);

The problem begins when I add more than one instance of the control to the page. 当我向页面添加多个控件实例时,问题就开始了。 Both scripts are rendered and registered BUT in the end only one, the last one on the page, ends up attached to the the containers, ie. 两个脚本都被渲染和注册但最终只有一个,页面上的最后一个,最终附加到容器,即。 wherever there is update in panel X or Y, only the JS for panel Y is executed - twice! 在面板X或Y中有更新的地方,只执行面板Y的JS - 两次!

A better option would surely be appending just one pair of Begin/End handlers, which I could perform by, for example, adding a static key for the RegisterStartupScript method. 一个更好的选择肯定会只附加一对Begin / End处理程序,我可以执行这些处理程序,例如,为RegisterStartupScript方法添加一个静态键。 The only problem is that I would need to pass a parameter of the panel for currently updating UpdatePanel. 唯一的问题是我需要传递面板的参数以便当前更新UpdatePanel。

Any idea how to do this, and combine with the above? 知道如何做到这一点,并与上述相结合?

This modification automatically restores the scroll position of all panels. 此修改会自动恢复所有面板的滚动位置。

<script type="text/javascript">
    (function () {
        var scrolledDivs = [];
        var prm = Sys.WebForms.PageRequestManager.getInstance();

        prm.add_beginRequest(function (sender, args) {

            //store the scroll positions of all "scrolled" div elements
            //UpdatePanel and Panel both are div elements
            scrolledDivs = [];

            $('div').each(function () {
                var div = $(this);
                if (div.scrollLeft() != 0 || div.scrollTop() != 0) {
                    scrolledDivs.push({
                        element: this,
                        scrollLeft: div.scrollLeft(),
                        scrollTop: div.scrollTop()
                    });
                }
            });
        });

        prm.add_endRequest(function (sender, args) {
            //restore scroll positions
            $.each(scrolledDivs, function (index, value) {
                $(value.element).scrollLeft(value.scrollLeft).scrollTop(value.scrollTop);
            });
        });
    })();
</script>

Note: you need to include JQuery 注意:您需要包含JQuery

Using a bit modified code from LostInComputer the solution looks like this: 使用LostInComputer中的位修改代码,解决方案如下所示:

            Page.ClientScript.RegisterStartupScript(this.GetType(), "scrollRestorer", @"
                  var scrolledDivs = [];
                  var prm = Sys.WebForms.PageRequestManager.getInstance();

                  function BeginRequestHandler(sender, args) {
                    //store the scroll positions of all 'scrolled' div elements
                    //UpdatePanel and Panel both are div elements
                    scrolledDivs = [];

                    $('div').each(function () {
                        var div = $(this);
                        if (div.scrollLeft() != 0 || div.scrollTop() != 0) {
                            scrolledDivs.push({
                                element: this.id,
                                scrollLeft: div.scrollLeft(),
                                scrollTop: div.scrollTop()
                            });
                        }
                    });
                 }

                 function EndRequestHandler(sender, args) {
                    //restore scroll positions
                    $.each(scrolledDivs, function (index, value) {
                        $('#' + value.element).scrollLeft(value.scrollLeft).scrollTop(value.scrollTop);
                    });
                 }

                 prm.add_beginRequest(BeginRequestHandler);
                 prm.add_endRequest(EndRequestHandler);  ", true);

The item itself cannot be stored in array, only ID should be stored. 项目本身不能存储在数组中,只应存储ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM