in

SDT Community Server

SDT Forums, Blogs, Photos server.

wego

June 2008 - Posts

  • window.history.go(-N) 及页面自动刷新数据的解决方案

    使用情况:

    1) 用户在 [查询页] 选取所需的 DropDown 项或填写查询的 TextBox 值

    2) 点击结果表格的一条记录链接进入 [编辑页] (不是弹出 Form 形式, 而是整个页面跳转)

    3) 在 [编辑页] 中修改有关项后保存 (保存后可能又进行修改后又保存)

    4) 按 [Close] button 回 [查询页]

    5) 要求 [查询页] 保留之前的查询状态 (即各DropDown, TextBox 项保留用户刚才选取或录入的值), 而表格数据则要求自动刷新 (因为刚才在 [编辑页] 中可能作了某些修改)

     

    注意事项:

    1) 因为 HTTP 的无状态性, 所以采用 Session 保留页面间的信息传递 (注意: 不能使用 URL 传递方式, 如: window.location.href = 'Search.aspx?serverRefreshed=yes', 因为这样页面原状态将不可还原 )

    2) 由于 window.history.go(-N)  后页面的 cs 代码不会执行, 故采用 Ajax 方式调用服务端代码

     

    代码片断:

    ----------------------------------------------------

    Edit.aspx:

    <asp:HiddenField ID="hfPostCount" runat="server"/>
    <asp:button id="btnSave" runat="server" Text="Save" OnClientClick="document.getElementById(hfDataSaved).value = 'Yes'" OnClick="btnSave_Click"></asp:button>
    <input type=button id="btnClose" value="Close" onclick="FormClose();" />

    function FormClose()
    {  
        if (document.getElementById(hfDataSaved).value == 'Yes') RRSCore.SessionInfo.SetServerRefreshed("No");       
        var postCount = document.getElementById(hfPostCount).value;
        postCount++;
        window.history.go(-postCount);
    }       

    ----------------------------------------------------

    Edit.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
       if (!this.IsPostBack)
           hfPostCount.Value = "0";
       else
           hfPostCount.Value = (int.Parse(hfPostCount.Value) + 1).ToString(); 

       Page.ClientScript.RegisterStartupScript(Page.GetType(), "SetClientVariable", "hfPostCount='" + this.hfPostCount.ClientID.ToString() + "';", true);  
    }

    ----------------------------------------------------

    Search.aspx  

    window.onload = function () {
        if (RRSCore.SessionInfo.GetServerRefreshed().value == 'No') document.forms[0].submit();              
        RRSCore.SessionInfo.SetServerRefreshed("Yes");
    }

    ----------------------------------------------------

    SessionInfo.cs (Ajax Source)

    namespace RRSCore

        public class SessionInfo
        { 

             [AjaxPro.AjaxMethod]
             public static void SetServerRefreshed(string serverRefreshed)
             {
                  HttpContext.Current.Session["ServerRefreshed"] = serverRefreshed;
             }

             [AjaxPro.AjaxMethod]
             public static string GetServerRefreshed()
             {
                   return HttpContext.Current.Session["ServerRefreshed"];
             }

        }

    }

    ----------------------------------------------------

Copyright SDT, 2006-2009. All rights reserved.