0

I'm developing a website in asp.net and c# which needs to catch if the user isn't connected when they press a button. So basically, if the user is connected, it will load up the GetList function, and if not a message will appear.

Code so far is...

protected void btnAlphabetical_Click(object sender, EventArgs e)
{
    Session["Online"] = 0;
    CheckConnect();
    if ((int)Session["Online"] == 1) { GetList(); }
    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.mywebsite.com/apicture.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;
    }
}

Basically, we set our Online session value to 0 and call the CheckConnect function. This gets a jpg that's already on the website and, if it can be loaded, sets our Online variable to 1. If it can't find it, it sets it to 0. When control returns to the main function (a button), we progress depending on what Online is - 1 or 0.

Trouble is, and I'm unsure whether this is more to do with my system settings than anything:

  • when we're online and getting something that DOES exist it works fine and GetList is fired
  • when we're online and getting something that DOESN'T exist (an invalid URL) it works fine and our message appears (GetList isn't fired)
  • HOWEVER, when we're offline and fire it, my browser (IE8) just goes to the regular "diagnose connection settings" screen

Is this my code, or part of IE8 in general? I can't use another browser as it's the one my company uses.

Thanks.

EDIT - the general purpose of this is to be used on mobile devices. The user will load up the page and make changes, then use the button. If connection is lost between the page being loaded and the user pressing the button, I don't want the user to lose their changes.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
user1560834
  • 151
  • 5
  • 16
  • 2
    If they are not connected, the page with the button would never come up... Are you sure you don't want to do this on the client-side? That would tell you if the *browser* can reach the image, not the *server*. – Mister Epic Nov 15 '13 at 15:34
  • I'm looking at using this to see if the connection has been lost. It's for a page on mobile devices and, if the mobile device has lost connection, this will (hopefully) stop the user from losing their work. Other than that, I guess the client-side makes more sense, but I don't quite know how to do that. – user1560834 Nov 15 '13 at 15:36
  • 1
    Your `CheckConnect()` method runs on the server and will not be executed without the user having a connection. Implement it in JavaScript. – CodeCaster Nov 15 '13 at 15:42
  • 2
    What value is there in knowing the server can't reach the image? I think you're looking to determine whether your mobile device can reach the server. Here is a great resource: http://diveintohtml5.info/ Pay attention to "Let's Take This Offline" and "A place to store your stuff." Let me know if you want a jQuery script to determine if your device is offline and I'll post it. – Mister Epic Nov 15 '13 at 15:43
  • A jQuery script would be great, may be a while before I get to implement it (away from office until mid-next week). Had a bit of trouble before with a jQuery script which worked, but didn't fire the c# events. – user1560834 Nov 15 '13 at 15:50

0 Answers0