如何保持滚动位置独立于回发?

时间:2015-05-08 07:42:37

标签: javascript asp.net scroll

我在javascript中有一个常规时间间隔事件,它点击一个asp按钮。该应用程序是一个常见的聊天室,我必须刷新gridview(在面板内)以检查新的聊天。但每次单击按钮并刷新gridview时,面板会向上滚动到顶部(即,scrollTop值变为0)。我试过这个但无济于事: -

    <script type="text/javascript">
    function refresh() {
        setInterval(function () {
            var xtop = document.getElementById("Panel1").scrollTop;
            document.getElementById("Button2").click();
            document.getElementById("Panel1").scrollTop = xtop;
        }, 1000);
    }
    </script>

1 个答案:

答案 0 :(得分:0)

If you want to store the scroll position between postbacks, in your CFNotificationCenterGetDarwinNotifyCenter function you can store the scroll position value in a hidden field that you put somewhere in your HTML: CFNotificationCenterGetDarwinNotifyCenter using JavaScript

def azureml_main(dataframe1 = None, dataframe2 = None):

    # Execution logic goes here
    print('Input pandas.DataFrame #1:')
    import pandas as pd
    import numpy as np
    from sklearn.kernel_approximation import RBFSampler
    x =dataframe1.iloc[:,2:1080]
    print x
    df1 = dataframe1[['colname']]

    change = np.array(df1)
    b = change.ravel()
    print b
    rbf_feature = RBFSampler(gamma=1, n_components=100,random_state=1)
    print rbf_feature
    print "test"
    X_features = rbf_feature.fit_transform(x)

There are other options to persist data such as cookies and the ASP.NET ViewState, but a hidden field is simplest for this purpose. You can then read this value from the hidden field upon loading the DOM and scroll the window to that position.

However, if you are posting back every second to check for new messages, you probably have a bigger problem. This kind of functionality is better done asynchronously with AJAX. It will need a bit of looking into as I'm not sure of your implementation here, but essentially you will use JavaScript to ask the server if there are new messages and load them from the server asynchronously, thereby eliminating the use of postbacks.

Here is Mozilla's introduction to AJAX.