【问题标题】:check if website is online - problems with postback检查网站是否在线 - 回发问题
【发布时间】:2014-05-05 03:34:40
【问题描述】:

我有一个网页,我公司的人正在使用手机填写。唯一的问题是,如果他们移出信号区,那么当他们尝试更新他们的作品时,页面将转到“未找到页面”,他们将丢失他们已填写的作品。

我正在尝试解决这个问题,目前有这个解决方案:

protected void Button1_Click(object sender, EventArgs e)
{
    Session["Online"] = 0;
    CheckConnect();
    if ((int)Session["Online"] == 1) { Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "alertMessage", "alert('You are currently online')", true); }
    if ((int)Session["Online"] == 0) { Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "alertMessage", "alert('You are currently offline')", true); }
}

protected void CheckConnect()
{
    System.Uri Url = new System.Uri("http://www.mypage.com/pixel.jpg?" + DateTime.Now);
    System.Net.WebRequest WebReq;
    System.Net.WebResponse Resp;
    WebReq = System.Net.WebRequest.Create(Url);
    try
    {
        Resp = WebReq.GetResponse();
        Resp.Close();
        WebReq = null;
        Session["Online"] = 1;
    }
    catch
    {
        WebReq = null;
        Session["Online"] = 0;
    }
}

现在,这将检查 www.mypage.com 上的像素文件是否存在(不,这实际上不是我的页面,我已将其替换为此示例),如果存在,则返回 0,如果不是1. 这很好,花花公子。

但是,按下按钮会导致页面重新加载。然后,如果它处于离线状态,它会执行通常的“找不到页面”业务。我的按钮代码在这里:

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

基本上,如果我们离线(或者实际上如果我们在线,因为执行更新的代码无论如何都会处理该部分),我希望它不会重新加载页面。

编辑 - 好的,现在不同的方法。使用以下代码完全通过 javascript 执行此操作:

<asp:Button ID="Button1" runat="server" OnClientClick="ifServerOnline()" Text="Button" />

        <script type="text/javascript">
            function ifServerOnline(ifOnline, ifOffline)
            {
                var img = document.body.appendChild(document.createElement("img"));
                img.onload = function ()
                {
                    ifOnline && ifOnline.constructor == Function && ifOnline();
                };
                img.onerror = function ()
                {
                    ifOffline && ifOffline.constructor == Function && ifOffline();
                };
                img.src = "http://www.mypage.com/pixel.jpg?" + Date.now;
            }

            ifServerOnline(function ()
            {
                return confirm('Online');
            },
            function ()
            {
                return confirm('Offline');
            });
        </script>

不幸的是仍然导致页面刷新。

【问题讨论】:

    标签: c# javascript webforms


    【解决方案1】:

    在您页面的 javascript 分配表单 onsubmit 事件处理程序中,您可以取消默认提交。此外,在此事件处理程序中,向服务器发出一个带有非常简短响应的 ajax 请求。在此 ajax 请求的 onsuccess 事件处理程序中 - 重新提交表单,在 onerror 处理程序中 - 告诉用户他们失去了与服务器的连接。

    【讨论】:

      【解决方案2】:

      离线时不能回发到服务器。 没办法。。

      但也许你可以用 javascript 做到这一点。试试这种方式。

      【讨论】:

      • 所以当我调用我的 c# 代码隐藏时,它会自动在服务器上(我猜这将是 runat 服务器部分 - 哦)?我猜必须从服务器调用 c# 部分。
      【解决方案3】:

      用这个来管理它...

              <asp:Button ID="btnRouteC" runat="server" OnClientClick="return ifServerOnlineA(ifServerOnlineA1, ifServerOfflineA1);" Text="Route" Width="45%" />
      
              <script type="text/javascript">
                  function ifServerOnlineA(ifOnline, ifOffline)
                  {
                      var img = document.body.appendChild(document.createElement("img"));
                      img.onload = function ()
                      {
                          ifOnline && ifOnline.constructor == Function && ifOnline();
                      };
                      img.onerror = function ()
                      {
                          ifOffline && ifOffline.constructor == Function && ifOffline();
                      };
                      img.src = "http://www.myserver.com/pixel.jpg?" + Date.now;
                      return false;
                  }
      
                  function ifServerOnlineA1()
                  {
                      document.getElementById('btnRoute').click();
                      return false;
                  }
                  function ifServerOfflineA1()
                  {
                      alert("There is no connection at the moment. Please try again later.");
                      return false;
                  }
              </script>
      

      正如 Itiel 所说,它不会通过代码隐藏工作,而是通过 Javascript 工作。

      【讨论】:

        猜你喜欢
        • 2015-05-26
        • 2012-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-22
        相关资源
        最近更新 更多