Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 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.
JavaScript
<script language="javascript" type="text/javascript">
    window.onunload = unloadPage;  
    var isClose = false;

    function somefunction()
    {
        isClose = false;
    }
    function unloadPage()
    {
        if(!isClose)
        {
             document.getElementById('btnimglogout').click();
        }
    }
    function setUser()
    {
        isClose = true;
    }
</script>

And the datagrid is:
ASP.NET
<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" />
                                                    <Columns>
                                                        <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" />
                                                </asp:DataGrid>

And the VB.NET code for pageselectedindexchanged event is:
VB
Protected Sub dgttumfileview_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgttumfileview.PageIndexChanged
        Try
           If (Not ClientScript.IsStartupScriptRegistered("alert")) Then
    Page.ClientScript.RegisterStartupScript(Me.GetType(), "alert", "setUser();", True)
            End If            
            dgttumfileview.CurrentPageIndex = e.NewPageIndex
            DataGridBind()
        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
v3
Comments
ZurdoDev 8-Nov-12 7:55am    
Did you finish the question?
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.
ZurdoDev 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
ZurdoDev 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 http://www.daniweb.com/web-development/aspnet/threads/368508/determine-whether-onunload-is-triggerred-by-refresh-event-or-close-browser-event 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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900