This is a migrated thread and some comments may be shown as answers.

Add new record triggers onbeforeload

1 Answer 63 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aash
Top achievements
Rank 1
Aash asked on 23 Jul 2012, 03:08 PM
Hi,

For some reason when I press the + button on the RadGrid it doesnt trigger onbeforeload. However when I click the "Add new record" LinkButton is does, which is not desired. Below i have attached sample code to reproduce this behavior. Is there valid Telerik solution to this problem, other then the one I have provided at the end of this post?

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="OnBeforeUnloadRadGridIssue._Default" %>
  
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title>OnBeforeUnload RadGrid Issue</title>
    <script type="text/javascript">
  
        var newSourceFilesAdded;
  
        window.onbeforeunload = function (event) {
  
            if (newSourceFilesAdded) {
                return 'Are you sure?';
            }
  
        }
  
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1"
                        UpdatePanelRenderMode="Inline" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <div>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Default">
        </telerik:RadAjaxLoadingPanel>
    </div>
    <telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0" GridLines="None">
        <MasterTableView ClientDataKeyNames="DocumentId, FileName" Width="100%" CommandItemDisplay="Top">
            <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
            <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
            </RowIndicatorColumn>
            <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
            </ExpandCollapseColumn>
            <Columns>
                <telerik:GridBoundColumn DataField="FileName" HeaderText="Filename" SortExpression="FileName"
                    UniqueName="FileName" ReadOnly="true">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="WordCount" HeaderText="Word Count" SortExpression="WordCount"
                    UniqueName="WordCount" ItemStyle-Width="50px">
                    <ItemStyle Width="50px"></ItemStyle>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="PageCount" HeaderText="Page Count" SortExpression="PageCount"
                    UniqueName="PageCount" ItemStyle-Width="50px">
                    <ItemStyle Width="50px"></ItemStyle>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="CharacterCount" DataType="System.Int32" FilterControlAltText="Filter CharacterCount column"
                    HeaderText="Character Count" UniqueName="CharacterCount">
                </telerik:GridBoundColumn>
                <telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete" Text="Delete"
                    UniqueName="Delete">
                </telerik:GridButtonColumn>
            </Columns>
            <EditFormSettings>
                <EditColumn ButtonType="ImageButton">
                </EditColumn>
            </EditFormSettings>
        </MasterTableView>
        <FilterMenu EnableImageSprites="False">
        </FilterMenu>
    </telerik:RadGrid>
    </form>
</body>
</html>

Imports Telerik.Web.UI
  
Public Class _Default
    Inherits System.Web.UI.Page
  
  
    Public Property SourceFiles As List(Of SourceFile)
        Get
            Return Session("SourceFiles")
        End Get
        Set(value As List(Of SourceFile))
            Session("SourceFiles") = value
        End Set
    End Property
  
  
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
        If Not IsPostBack Then
  
  
            Dim count As Byte = 0
            Dim rnd As New Random
            Dim sfs As New List(Of SourceFile)
  
            While count < 10
  
                Dim id As String = Guid.NewGuid.ToString
                sfs.Add(New SourceFile With {.DocumentId = id,
                                             .FileName = id,
                                             .CharacterCount = rnd.Next(0, Integer.MaxValue),
                                             .PageCount = rnd.Next(0, Integer.MaxValue),
                                             .WordCount = rnd.Next(0, Integer.MaxValue),
                                             .IsNew = True})
  
                count = count + 1
  
            End While
  
            SourceFiles = sfs
  
            RadGrid1.DataSource = SourceFiles
            RadGrid1.DataBind()
  
        Else
  
            Dim context As System.Web.HttpContext = System.Web.HttpContext.Current
            If context IsNot Nothing Then
                Dim page As System.Web.UI.Page = CType(context.Handler, System.Web.UI.Page)
                Telerik.Web.UI.RadAjaxManager.GetCurrent(page).ResponseScripts.Add(" newSourceFilesAdded = " & If(SourceFiles.Any, "true; ", "false; "))
            End If
  
        End If
  
    End Sub
  
    Private Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
  
        RadGrid1.DataSource = SourceFiles
  
    End Sub
  
End Class
  
Public Class SourceFile
  
    Public Property DocumentId As String
    Public Property FileName As String
    Public Property WordCount As Integer
    Public Property PageCount As Integer
    Public Property CharacterCount As Integer
    Public Property IsNew As Boolean
  
End Class

As i mentioned before, I have found a workaround by doing the following:

<ClientEvents OnCommand="RaiseCommand" />

where the function itself has no code:

function RaiseCommand(sender, eventArgs) {
    //By adding this, it prevents onbeforeunload from firing as a redirect,
    //When Add New Record is clicked insead of + button.
}

Thanks,
Aash.

1 Answer, 1 is accepted

Sort by
0
Antonio Stoilkov
Telerik team
answered on 26 Jul 2012, 08:04 AM
Hi Aash,

The experienced behavior is expected. The issue could be observed only in IE and it is browser behavior. IE assumes that when you click HTML "a" element with href that a postback will occur which is not true when ajaxifying is in place.

The behavior is changed when RadGrid OnCommand is set because the href attributes is changed with onclick event attachment. The current approach is appropriate for handling the scenario.

Greetings,
Antonio Stoilkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Aash
Top achievements
Rank 1
Answers by
Antonio Stoilkov
Telerik team
Share this question
or