GridAttachmentColumn: File Upload Button Works Local, Not Remote

2 posts, 0 answers
  1. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 10 Oct 2011 Link to this post

    Hi,

    I have a RadGrid with a GridAttachmentColumn. I created a test page and everything is working fine locally.

    When I publish to a remote server, everything appears ok (attachments are retrieved, displayed and downloadable) but when I click "Add New Record" and click the file upload browse button, nothing happens. No script or server error, no UI response, nothing.

    What can be going wrong?

    Here's some code showing what I have. It works fine debugging using VS2010 built-in web server. I've necessarilly had to cut some of the code out.

    Master Page
    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="SiteMaster.Master.cs" Inherits="MyNamespace.MyMasterPages.SiteMaster" %>
    <%@ 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 id="Header" runat="server" lang="en-au">
        <title>Test</title>
        <asp:ContentPlaceHolder ID="HeadContentPlaceHolder" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form id="MainForm" runat="server">
      
            <telerik:RadScriptManager ID="TelerikScriptManager" runat="server">
                <CompositeScript>
                    <Scripts>
                <%--Needed for JavaScript IntelliSense in VS2010--%>
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
                <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
            </Scripts>
                </CompositeScript>
            </telerik:RadScriptManager>
      
            <asp:ContentPlaceHolder ID="ScriptContentPlaceHolder" runat="server">
            </asp:ContentPlaceHolder>
      
            <telerik:RadAjaxManager ID="TelerikAjaxManager" runat="server">
                <AjaxSettings>
                    <telerik:AjaxSetting AjaxControlID="MainUpdatePanel">
                        <UpdatedControls>
                            <telerik:AjaxUpdatedControl ControlID="MainUpdatePanel" LoadingPanelID="TelerikAjaxLoadingPanel" />
                        </UpdatedControls>
                    </telerik:AjaxSetting>
                </AjaxSettings>
            </telerik:RadAjaxManager>
      
            <telerik:RadWindowManager ID="TelerikWindowManager" runat="server">
            </telerik:RadWindowManager>
      
        <telerik:RadSkinManager ID="TelerikSkinManager" Runat="server" Skin="Telerik">
            </telerik:RadSkinManager>
      
            <telerik:RadFormDecorator ID="TelerikFormDecorator" Runat="server" Skin="Telerik" DecoratedControls="All" />
      
            <telerik:RadAjaxPanel ID="MainUpdatePanel" runat="server">
      
                <asp:ContentPlaceHolder ID="MainContentPlaceHolder" runat="server">
                </asp:ContentPlaceHolder>
      
            </telerik:RadAjaxPanel>
      
            <telerik:RadAjaxLoadingPanel ID="TelerikAjaxLoadingPanel" runat="server">
            </telerik:RadAjaxLoadingPanel>
      
        </form>
    </body>
    </html>

    Content Page
    <%@ Page Title="Attachments Test" Language="C#" MasterPageFile="~/MyMasterPages/SiteMaster.Master" AutoEventWireup="true" CodeBehind="Attachments.aspx.cs" Inherits="MyNamespace.Attachments" %>
    <%@ MasterType VirtualPath="~/MyMasterPages/SiteMaster.Master" %>
      
    <asp:Content ID="ScriptContent" ContentPlaceHolderID="ScriptContentPlaceHolder" runat="server">
      
        <telerik:RadCodeBlock ID="TelerikCodeBlock" runat="server">
        <script type="text/javascript">
        function ConditionalPostback(sender, args)
        {
            var eventTarget = args.get_eventTarget();
      
            if (eventTarget == "<%=this.AttachmentsGrid.UniqueID %>")
            {
                args.set_enableAjax(false);
            }
        }
      
        function GridOnCommand(sender, args)
        {
            if (args.get_commandName() == "DownloadAttachment")
            {
                var manager = $find('<%= RadAjaxManager.GetCurrent(Page).ClientID %>');
      
                manager.set_enableAJAX(false);
      
                setTimeout(function ()
                {
                    manager.set_enableAJAX(true);
                }, 0);
            }
        }
        </script>
        </telerik:RadCodeBlock>
      
    </asp:Content>
    <asp:Content ID="Content3" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
      
        <telerik:RadGrid 
            AutoGenerateColumns="False" 
            AutoGenerateDeleteColumn="True" 
            AutoGenerateEditColumn="True" 
            DataSourceID="AttachmentsDataSource" 
            ID="AttachmentsGrid" 
            OnDeleteCommand="AttachmentsGrid_OnCommand" 
            OnInsertCommand="AttachmentsGrid_OnCommand" 
            OnItemCommand="AttachmentsGrid_OnItemCommand"
            OnUpdateCommand="AttachmentsGrid_OnCommand" 
            runat="server">
            <MasterTableView 
                DataKeyNames="Id" 
                DataSourceID="AttachmentsDataSource">
                <Columns>
                    <telerik:GridBoundColumn 
                        DataField="Id" 
                        DataType="System.Int32" 
                        HeaderText="Id" 
                        ReadOnly="True" 
                        UniqueName="Id">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn 
                        DataField="FileName" 
                        HeaderText="FileName" 
                        UniqueName="FileName" 
                        ReadOnly="true" 
                        DataType="System.String">
                    </telerik:GridBoundColumn>
                    <telerik:GridAttachmentColumn 
                        DataSourceID="AttachmentDataSource"
                        MaxFileSize="1048576" 
                        EditFormHeaderTextFormat="Upload Attachment:"
                        HeaderText="Attachment"
                        HeaderTooltip="Download Attachment"
                        AttachmentDataField="Attachment" 
                        AttachmentKeyFields="Id"
                        FileNameTextField="FileName" 
                        DataTextField="FileName"
                        UniqueName="Attachment">
                    </telerik:GridAttachmentColumn>
                </Columns>
            </MasterTableView>
            <ClientSettings>
                <ClientEvents OnCommand="GridOnCommand" />
            </ClientSettings>
        </telerik:RadGrid>
      
        <asp:EntityDataSource 
            ConnectionString="name=MyEntities" 
            DefaultContainerName="MyEntities" 
            EnableFlattening="False" 
            EntitySetName="Attachments" 
            EntityTypeFilter="Attachment" 
            ID="AttachmentsDataSource" 
            runat="server">
        </asp:EntityDataSource>
      
        <asp:EntityDataSource 
            ConnectionString="name=MyEntities" 
            DefaultContainerName="MyEntities" 
            EnableFlattening="False" 
            EntitySetName="Attachments" 
            EntityTypeFilter="Attachment" 
            ID="AttachmentDataSource" 
            runat="server" 
            Where="it.[Id] = @Id"
            Select="it.[Attachment]">
            <SelectParameters>
                <asp:Parameter Name="Id" Type="Int32" />
            </SelectParameters>
        </asp:EntityDataSource>
      
    </asp:Content>

    Content Page Code-Behind
    namespace MyNamespace
    {
        using System;
        using MyMasterPages;
        using Telerik.Web.UI;
      
        public partial class Attachments : Page
        {
            protected void Page_Init(object sender, EventArgs e)
            {
                if (this.Master != null)
                {
                    var ajaxManager = (RadAjaxManager)this.Master.FindControl("TelerikAjaxManager");
      
                    if (ajaxManager != null)
                    {
                        ajaxManager.ClientEvents.OnRequestStart += "ConditionalPostback";
                    }
                }
            }
      
            protected void AttachmentsGrid_OnCommand(object sender, GridCommandEventArgs e)
            {
                var editedItem = e.Item as GridEditableItem;
      
                if (editedItem != null)
                {
                    switch (e.CommandName)
                    {
                        case RadGrid.PerformInsertCommandName:
                        case RadGrid.UpdateCommandName:
      
                            // Insert or Update
      
                            break;
      
                        case RadGrid.DeleteCommandName:
      
                            // Delete
      
                            break;
                    }
                }
            }
      
            protected void AttachmentsGrid_OnItemCommand(object sender, GridCommandEventArgs e)
            {
                if (e.CommandName == RadGrid.DownloadAttachmentCommandName)
                {
                    e.Canceled = true;
      
                    var args = e as GridDownloadAttachmentCommandEventArgs;
      
                    if (args != null)
                    {
                        var attachmentId = (int)args.AttachmentKeyValues["Id"];
      
                        byte[] attachment = GetAttachment();
      
                        if (attachment != null)
                        {
                            Response.Clear();
                            Response.ContentType = "application/octet-stream";
                            Response.AddHeader("content-disposition", "attachment; filename=" + args.FileName);
                            Response.BinaryWrite(attachment);
                            Response.End();
                        }
                    }
                }
            }
        }
    }

    That's all the important stuff. Like I said, works fine locally.

    Richard
  2. Richard Weeks
    Richard Weeks avatar
    95 posts
    Member since:
    Oct 2009

    Posted 11 Oct 2011 Link to this post

    So far, have found that the select button works on the latest Firefox but not in MSIE 8 (8.0.7601.17514). OS is Win7.

    The browse dialog will show in Firefox and upload procedes as normal but in MSIE, nothing happens when clicking the select button. No dialog shows nor any message.

    Richard

    Edit: Figured it out! The following sneaked through to production:

    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

    Taking that out fixed IE. Would be interesting to know which meta-equiv is best suited to working with XHTML and MSIE now that IE9 is out and (inevitably) giving us issues with regard to Telerik controls - which version should we be pushing IE to?

    More info: http://msdn.microsoft.com/en-us/library/cc288325(v=vs.85).aspx
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top