Friday, 6 December 2013

How to check internet connection using ASP.NET with JavaScript?



In this article I will explain how to check internet connection using ASP.NET with JavaScript

I have declared one javascript function for checking the internet connection.

Below JavaScript code is useful for check internet connection online or offline.

function checkNetconnection() {
            var status = navigator.onLine;
            if (status) {
                alert("online");
            } else {
                alert("offline");
            }
        }

Below is the code for aspx page design. We are using master page also. In this master page we have two content place holders. In first content place holder we placed JavaScript code and second content placed holder we placed one ASP Button.



In button  OnClientClick event we call JavaScript function.

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script type="text/javascript">
        function checkNetconnection() {
            var status = navigator.onLine;
            if (status) {
                alert("online");
            } else {
                alert("offline");
            }
        }
    </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <table>
        <tr>
            <td>
                Check Internet Connection using JavaScript
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button runat="server" ID="btnSubmit" Text="Check Online" OnClientClick="checkNetconnection()" />
            </td>
        </tr>
    </table>
</asp:Content>

Below is the page final design.




If you click the “Check Online” button it displays whether you have online or not. 

No comments:

Post a Comment