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.