Click here to Skip to main content
13,256,446 members (52,027 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have a function in javascript and a Datagrid. I have used an event 'onunload' in javascript. Below is my ASP.NET code.
<script language="javascript" type="text/javascript">
    window.onunload = unloadPage;  
    var isClose = false;
    function somefunction()
        isClose = false;
    function unloadPage()
    function setUser()
        isClose = true;

And the datagrid is:
<asp:DataGrid ID="<b>dgttumfileview</b>" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                                                    BorderColor="Black" CssClass="maintext" ForeColor="Black" HorizontalAlign="Center" CellPadding="1" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" BorderWidth="1px" CellSpacing="1" Width="796px">
                                                    <ItemStyle HorizontalAlign="Center" BorderColor="#404040" BorderStyle="Solid" />
                                                    &lt;HeaderStyle Font-Bold="True" HorizontalAlign="Center" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
                                                        <asp:BoundColumn DataField="sr_no" HeaderText="Sr No."></asp:BoundColumn>
                                                    <PagerStyle Font-Bold="True" HorizontalAlign="Right" Mode="NumericPages" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
                                                    &lt;SelectedItemStyle BorderColor="#404040" BorderStyle="Solid" />

And the VB.NET code for pageselectedindexchanged event is:
Protected Sub dgttumfileview_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgttumfileview.PageIndexChanged
           If (Not ClientScript.IsStartupScriptRegistered("alert")) Then
    Page.ClientScript.RegisterStartupScript(Me.GetType(), "alert", "setUser();", True)
            End If            
            dgttumfileview.CurrentPageIndex = e.NewPageIndex
        Catch ex As Exception
            Trace.Warn("dgUnPaidScroll_PageIndexChanged", ex.Message)
        End Try
    End Sub

Now the issue is the page event calls the javascript function setUser() only when window.onunload is not used. But if I am using window.onunload function, the Javascript function SetUser is not called.
I have spend my whole day but could not get out of it.
Posted 8-Nov-12 2:52am
ryanb31 8-Nov-12 7:55am
Did you finish the question?
Dave Kreskowiak 8-Nov-12 11:28am
That's not ASP.NET code. THat's not ANY code. YOu may want to look at what you posted and fix your question so people can understand it.
aspnet_regiis -i 8-Nov-12 12:50pm
Whereeeeeeeeeeeeeee is the code? Working toooooooo late is dangerous for the brain....
swatiiiiiiiiii 8-Nov-12 23:21pm
ya.. How can I call the javascript function 'SetUser' from page event of Datagrid in codebehind when window.onunload is used ?? Page gets unloaded before the javascript function gets executed.
ryanb31 9-Nov-12 8:02am
Why is the page being unloaded? What are you trying to accomplish with the unload event?
swatiiiiiiiiii 12-Nov-12 0:20am
I have to implement functionality on page unload to logout the user when he closes the browser
ryanb31 12-Nov-12 7:42am
First of all, there is no guaranteed code that will log out a user because there are many ways a browser can close and you won't know about it (machine crashes for example) because the web is a disconnected app. You can try some things listed at but you may want to rethink why you want to know when they are logged out.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 8 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100