Click here to Skip to main content
15,887,434 members
Articles / Web Development / ASP.NET

Save Changes on Close of Browser or When Exiting the Page

Rate me:
Please Sign up or sign in to vote.
4.36/5 (23 votes)
24 Sep 2007CPOL4 min read 266K   5.2K   115  
This article will show how to save changes on the close of the browser or when exiting a page.
Imports System.Data
Imports System.Data.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling

Partial Class Default2
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack Then
                mytxt.Text = ""
                LinkButton1.Attributes.Add("tag", "DonotCallSaveonLoad")
                LinkButton1.Attributes.Add("onClick", "currentElement = event.srcElement;")
                Button1.Attributes.Add("tag", "DonotCallSaveonLoad")
                Button1.Attributes.Add("onClick", "currentElement = event.srcElement;")
                ImageButton1.Attributes.Add("tag", "DonotCallSaveonLoad")
                ImageButton1.Attributes.Add("onClick", "currentElement = event.srcElement;")
                DropDownList2.Attributes.Add("tag", "DonotCallSaveonLoad")
                DropDownList2.Attributes.Add("onClick", "currentElement = event.srcElement;")
                myhref1.Attributes.Add("tag", "DonotCallSaveonLoad")
                myhref2.Attributes.Add("tag", "DonotCallSaveonLoad")
                myhref1.Attributes.Add("onClick", "currentElement = event.srcElement;")
                myhref2.Attributes.Add("onClick", "currentElement = event.srcElement;")
                grdData.DataSource = GetDatafromTable("Table")
                grdData.DataBind()
                grdError.DataSource = GetDatafromTable("Error")
                grdError.DataBind()
            End If
            If hdtoSaveData.Value = "PageClosed" Then
                If drpError.SelectedValue = "Yes" Then
                    Dim a As Integer = 1
                    Dim b As Integer = 0
                    Dim x As Integer = CInt(a / b)
                End If
                SaveDataonPage()
            End If
        Catch ex As Exception
            ExceptionPolicy.HandleException(ex, "SamplePolicy")
            GetDatafromTable("Error")
        End Try
    End Sub

    ''' <summary>
    ''' The functionality to save data on the Page.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub SaveDataonPage()
        Try
            Dim db As Database = DatabaseFactory.CreateDatabase
            Using dbcomm As DbCommand = db.GetSqlStringCommand("Insert into temp values('" & mytxt.Text & "')")
                db.ExecuteNonQuery(dbcomm)
            End Using
            grdData.DataSource = GetDatafromTable("Table")
            grdData.DataBind()
        Catch ex As Exception
            ExceptionPolicy.HandleException(ex, "SamplePolicy")
            GetDatafromTable("Error")
        End Try
    End Sub

    ''' <summary>
    ''' Click Event for Link button
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
        Try
            If drpError.SelectedValue = "Yes" Then
                Dim a As Integer = 1
                Dim b As Integer = 0
                Dim x As Integer = CInt(a / b)
            End If
            SaveDataonPage()
        Catch ex As Exception
            ExceptionPolicy.HandleException(ex, "SamplePolicy")
            GetDatafromTable("Error")
        End Try
    End Sub

    ''' <summary>
    ''' Click Event for  Button Click
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            If drpError.SelectedValue = "Yes" Then
                Dim a As Integer = 1
                Dim b As Integer = 0
                Dim x As Integer = CInt(a / b)
            End If
            SaveDataonPage()
        Catch ex As Exception
            ExceptionPolicy.HandleException(ex, "SamplePolicy")
            GetDatafromTable("Error")
        End Try
    End Sub

    ''' <summary>
    ''' Click Event for Image Button
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Try
            If drpError.SelectedValue = "Yes" Then
                Dim a As Integer = 1
                Dim b As Integer = 0
                Dim x As Integer = CInt(a / b)
            End If
            SaveDataonPage()
        Catch ex As Exception
            ExceptionPolicy.HandleException(ex, "SamplePolicy")
            GetDatafromTable("Error")
        End Try
    End Sub

    ''' <summary>
    ''' Change event for DropDownList
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
        Try
            If drpError.SelectedValue = "Yes" Then
                Dim a As Integer = 1
                Dim b As Integer = 0
                Dim x As Integer = CInt(a / b)
            End If
            SaveDataonPage()
        Catch ex As Exception
            ExceptionPolicy.HandleException(ex, "SamplePolicy")
            GetDatafromTable("Error")
        End Try
    End Sub

    ''' <summary>
    ''' Function to get the data from database
    ''' </summary>
    ''' <param name="Type"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetDatafromTable(ByVal Type As String) As DataTable
        Dim db As Database = DatabaseFactory.CreateDatabase
        Dim dsobj As DataSet
        Try
            If Type = "Error" Then
                Using dbcomm As DbCommand = db.GetSqlStringCommand("SELECT [FormattedMessage] as name FROM [log]")
                    dsobj = db.ExecuteDataSet(dbcomm)
                    Return dsobj.Tables(0)
                End Using
            Else
                Using dbcomm As DbCommand = db.GetSqlStringCommand("SELECT * FROM temp")
                    dsobj = db.ExecuteDataSet(dbcomm)
                    Return dsobj.Tables(0)
                End Using
            End If
        Catch ex As Exception
            ExceptionPolicy.HandleException(ex, "SamplePolicy")
        End Try
    End Function
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Spozzle
India India
A highly experienced and influential Technical Manager / Team Leader and Architect, an inspirational leader who has effectively managed, developed and motivated internal and external teams. Strong ability to quickly understand and analyze business strategies ensuring technology solutions meet business requirement, to quickly adapt to all new technologies and challenges.

My attention to detail, high technical aptitude and hands on approach together with a high degree of motivation and professionalism would make me an asset to any progressive organization or demanding position within IT industry.

What do I do?

I work at Spozzle(www.spozzle.me) as a Software Architect and currently working on the following technologies.

Web Technologies using

Java
PHP
XCode 4 and IPhone Development
HTML 5 and CSS
Android Development

My Experience

I have spent my last 7 years working on .NET Technology building application both on the Web and Windows.

I provide Technical consultations to people on

- .NET Development, Best Practices and Issues
- Best Practices on Web based Architecture and Hosting applications on Cloud.

I have not moved on to PHP, Java and Mobile Development. I now work at Spozzle developing really cool application for the Sports Fanatics on Facebook. Checkout http://apps.facebook.com/spozzle

Comments and Discussions