Tuesday 10 June 2014

How to fix System.Web.UI.ViewStateException: Invalid viewstate or System.Web.HttpException: The client disconnected exception or HttpResponse.IsClientConnected Property

Hi friends, in this article I will explain about  How to fix System.Web.UI.ViewStateException: Invalid viewstate or System.Web.HttpException: The client disconnected exception or HttpResponse.IsClientConnected Property.
I already explained in the previous articles about How to change image opacity and Zoom on mouseover using jQueryHow to alter primary key column for existing table in sql server 2008 and How to set column width If ListItem in a CheckBoxList have big length in ASP.NET using CSS


We can fix System.Web.UI.ViewStateException: Invalid viewstate or System.Web.HttpException: The client disconnected exception using Response.IsClientConnected Property. 


What is HttpResponse.IsClientConnected Property:
HttpResponse.IsClientConnected used to gets a value indicating whether the client is still connected to the server.

The following example uses the IsClientConnected property to check whether the client that is requesting the page remains connected to the server. IfIsClientConnected is true, the code calls the  Redirect  method, and the client will view another page. If IsClientConnected is false, then the code calls the End method and all page processing is terminated.

ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to fix System.Web.UI.ViewStateException: Invalid viewstate 
or System.Web.HttpException: The client disconnected exception or 
HttpResponse.IsClientConnected Property
    </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>Welcome to Aspdotnet-Kishore
    </div>
    </form>
</body>
</html>

C#:
public partial class ClientDisconnected : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check whether the browser(client) remains connected to the server
        if (Response.IsClientConnected)
        {
            // if  browser connected, redirect to the Main.aspx page
            Response.Redirect("~/Main.aspx");
        }
        else
        {
            //if browser is not connected,terminate all response processing.
            Response.End();
        }
    }
}

VB.NET:
Public Partial Class ClientDisconnected
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        'Check whether the browser(client) remains connected to the server
        If Response.IsClientConnected Then
            ' if  browser connected, redirect to the Main.aspx page
            Response.Redirect("~/Main.aspx")
        Else
            'if browser is not connected,terminate all response processing.
            Response.[End]()
        End If
    End Sub
End Class

1 comment:

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.