在自动页面刷新RadPanelBar滚动上升

时间:2012-10-03 10:21:19

标签: c# asp.net c#-4.0 telerik

我想保持RadPanelBar的滚动位置。我的RadPanelBar被放置在自动刷新的页面中。当我向下滚动RadPanelBar向下时,它会在页面刷新时向上移动。它不应该。我试图用以下代码处理它,但它不起作用。我错过了什么吗?

这是我的JavaScript

<script type="text/javascript">
function SetScrollPositionOnlineContacts() {

    //var pane = splitter.GetPaneById("RadPanelBarRoster");
    //document.getElementById("RadPanelBarRoster").scrollTop;

    var splitter =$find("RadSplitterBottomParent");
    var intY =  splitter.GetPaneById("RadPanelBarRoster").scrollTop;

    var date = new Date();
    date.setTime(date.getTime() + (1 * 60 * 60 * 1000));
    var expires = "; expires=" + date.toGMTString();
    document.cookie = "cookieDivOnlineContacts" 
                      + "=" 
                      + intY 
                      + expires + "; path=/";
    //document.title = intY;
}

function readCookieOnlineContacts(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}
    </script>

这是我的HTML代码:

<telerik:RadSplitter ID="RadSplitterBottomParent" runat="server" Height="100%" 
    Width="100%" Orientation="Vertical" BackColor="#ECECFF" Skin="WebBlue" 
    LiveResize="True" SplitBarsSize="" HeightOffset="124" Visible="true" 
    BorderSize="0" PanesBorderSize="0">

    <!-- Start Left Roster Panel -->
    <telerik:RadPane ID="RadPaneRoster" runat="server" Width="220px" 
        Scrolling="None">
        <telerik:RadContextMenu ID="RadContextMenuRoster" runat="server" 
            OnClientItemClicked="onRosterContextMenuClick" 
            EnableShadows="True">
            <Items>
                <telerik:RadMenuItem runat="server" Text="View 
                    Conversation">
                </telerik:RadMenuItem>
                <telerik:RadMenuItem runat="server" Text="View Properties">
                </telerik:RadMenuItem>
                <telerik:RadMenuItem runat="server" Text="Add to Favourites" 
                    Enabled="False">
                </telerik:RadMenuItem>
            </Items>
        </telerik:RadContextMenu>
        <telerik:RadPanelBar ID="RadPanelBarRoster" runat="server" 
            Skin="Metro" 
            Width="100%" onscroll="SetScrollPositionOnlineContacts()"
            Height="3000px" OnClientItemClicked="onRosterClick" 
            OnClientContextMenu="onRosterContextMenu"
            PersistStateInCookie="True" Style="width: 100%; border-top: 0; 
            border-bottom: 0;
            z-index: 2" ExpandMode="FullExpandedItem" EnableViewState="True" 
            ViewStateMode="Inherit"
            OnItemClick="RadPanelBarRoster_ItemClick">
        </telerik:RadPanelBar>
    </telerik:RadPane>
</telerik:RadSplitter>

这是我的代码隐藏页面:

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        StringBuilder sbScript = new StringBuilder();
        sbScript.Append(
            "<script language='JavaScript' type='text/javascript'>");
        //For order Panel
        sbScript.Append(
        "document.cookie = cookieDivOnlineContacts + \"" + 
        "=\" + \"\" + -1 + \"; path=/\";");

        sbScript.Append("</script>");
        //// Make use ScriptManager to register the script
        ScriptManager.RegisterStartupScript(
            this, 
            this.GetType(), 
            "@@@@MyCallBackAlertScript", 
            sbScript.ToString(), 
            false);
    }

    StringBuilder sbScript1 = new StringBuilder();
    sbScript1.Append(
        "<script language='JavaScript' type='text/javascript'>");
    //For order Panel
    sbScript1.Append(
        "var strCookOnlineContacts = " + 
        "readCookieRfqOrder(\"cookieDivOnlineContacts\"); " + 
        "document.getElementById(\"RadPanelBarRoster\").scrollTop " + 
        "= strCookOnlineContacts;");

    sbScript1.Append("</script>");

    // Make use ScriptManager to register the script
    ScriptManager.RegisterStartupScript(
        this, 
        this.GetType(), 
        "@@@@MyCallBackAlertScript", 
        sbScript1.ToString(), 
        false);

}

我是初学者,所以请给我适当的帮助。

1 个答案:

答案 0 :(得分:2)

你正在尝试使用cookies,这里也是一段代码试试这个, 将此代码放在JavaScript部分

这将在请求开始时保存滚动位置,并且当页面完全呈现时它将存储向后滚动的位置。

 function pageLoad()
        {
            $("your radID").scroll(function() {
            SaveScrollPosition();
            });
        }

    var yPos;
        function SaveScrollPosition(){
         yPos = $("your rad bar id").scrollTop(); 
        }

        function ReturnPos() {
        $("your radID").scrollTop(yPos);
        }


  function OnResponseEnd(sender ,eventArgs)
        {
            ReturnPos();
        }

  function OnRequestStart(sender ,eventArgs)
        {

            SaveScrollPosition();
        }

这是你的HTML

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" OnAjaxRequest="RadAjaxManager1_AjaxRequest" ClientEvents-OnRequestStart="OnRequestStart" ClientEvents-OnResponseEnd="OnResponseEnd">
// you code here
</telerik:RadAjaxManager>

我相信这会解决您的问题。谢谢 如果是解决方案,请将其标记为答案。