Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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()
    {
        if(!isClose)
        {
             document.getElementById('btnimglogout').click();
        }
    }
    function setUser()
    {
        isClose = true;
    }
</script>
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" />
                                                    <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:
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 8-Nov-12 2:52am
Edited 8-Nov-12 8:11am
(no name)117.6K
v3
Comments
ryanb31 at 8-Nov-12 7:55am
   
Did you finish the question?
swatiiiiiiiiii at 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 at 9-Nov-12 8:02am
   
Why is the page being unloaded? What are you trying to accomplish with the unload event?
swatiiiiiiiiii at 12-Nov-12 0:20am
   
I have to implement functionality on page unload to logout the user when he closes the browser
ryanb31 at 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.
Dave Kreskowiak at 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 at 8-Nov-12 12:50pm
   
Whereeeeeeeeeeeeeee is the code? Working toooooooo late is dangerous for the brain....

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

  Print Answers RSS
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 8 Nov 2012
Copyright © CodeProject, 1999-2014
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