Telerik is a leading vendor of ASP.NET AJAX, ASP.NET MVC, Silverlight, WinForms and WPF controls and components, as well as .NET Reporting, .NET ORM , .NET CMS, Code Analysis, Mocking, Team Productivity and Automated Testing Tools. Building on its expertise in interface development and Microsoft technologies, Telerik helps customers build applications with unparalleled richness, responsiveness and interactivity. Telerik products help thousands of companies to be more productive and deliver reliable applications under budget and on time.
Version Q1 2012 released 04/11/2012
select

Grid / Attachment Column

Next database reset in 0 hours, 37 minutes, 45 seconds
Example Configuration



select
Add new recordRefresh
 IDFileNameUploadDateUploadedByDownload
Edit1ViewState.gif9/16/2009 2:00:00 PMJohn SmithViewState.gif
Edit2Appointments.xml9/16/2009 7:00:00 PMMaria TrujilloAppointments.xml
Edit3Sushi.pdf9/17/2009 5:00:00 PMYamada TaroSushi.pdf
Edit4Demo.png9/19/2009 9:00:00 AMRobert De BoronDemo.png
Edit5Customers.doc9/21/2009 6:00:00 PMSergio BatistaCustomers.doc
Edit6Customers.xls9/21/2009 7:00:00 PMSergio BatistaCustomers.xls
Edit7Description.htm9/22/2009 10:00:00 AMJean FrancoisDescription.htm
Note: Maximum file size allowed: 1MB

  • Choose an option and download attachment from a RadGrid data item or choose a file and press "Download" in the configuration section, to fire the event from code.

Source Code

C# VB.NET
Show code in new window Demo isolation steps
  • <%@ Page Language="VB" AutoEventWireup="false" CodeFile="DefaultVB.aspx.vb" Inherits="Telerik.GridExamplesVBNET.GeneralFeatures.GridAttachmentColumn.DefaultVB" %>

    <%@ Register TagPrefix="telerik" Namespace="Telerik.QuickStart" %>
    <%@ Register TagPrefix="telerik" TagName="Header" Src="~/Common/Header.ascx" %>
    <%@ Register TagPrefix="telerik" TagName="HeadTag" Src="~/Common/HeadTag.ascx" %>
    <%@ Register TagPrefix="telerik" TagName="Footer" Src="~/Common/Footer.ascx" %>
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <%@ Register TagPrefix="sds" Namespace="Telerik.Web.SessionDS" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <telerik:HeadTag runat="server" ID="Headtag2"></telerik:HeadTag>
        <!-- custom head section -->
        <!-- end of custom head section -->
    </head>
    <body class="BODY">
        <form id="mainForm" runat="server">
            <telerik:Header runat="server" ID="Header1" NavigationLanguage="VB" ShowDbResetTimer="true">
            </telerik:Header>
       <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
            <!-- content start -->
            <telerik:ConfiguratorPanel ID="ConfiguratorPanel1" runat="server" Expanded="true">
                    
                    <asp:RadioButtonList ID="DownloadOptionsRBL" runat="server">
                        <asp:ListItem Text="Automatically download attachment from <b>GridAttachmentColumn's</b> data source"
                            Value="Automatic" Selected="True">
                        </asp:ListItem>
                        <asp:ListItem Text="Manually download attachment using RadGrid's <b>ItemCommand</b> event" Value="Manual">
                        </asp:ListItem>
                    </asp:RadioButtonList>
                    <br /><br />
                    <div style="margin-bottom:5px;">
    <asp:RadioButtonList ID="FireCommandOptionsRBL" runat="server"
    style="display:inline; vertical-align:bottom; margin-right:10px"
    RepeatDirection="Horizontal">

                            <asp:ListItem Text="Fire from server-side" Value="Server" Selected="True">
                            </asp:ListItem>
                            <asp:ListItem Text="Fire from client-side" Value="Client">
                            </asp:ListItem>
                        </asp:RadioButtonList>
                        
                        <telerik:RadComboBox ID="FileNamesComboBox" runat="server"
                            DataSourceID="SqlDataSource1" DataTextField="FileName" DataValueField="ID">
                        </telerik:RadComboBox>
                        
                        <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
                            <script type="text/javascript">
                            <!--
                                function download(args)
                                {
                                    if ($get("FireCommandOptionsRBL_0").checked)
                                    {
                                        return true;
                                    }

                                    var grid = $find('<%= RadGrid1.ClientID %>');
                                    var masterTable = grid.get_masterTableView();
                                    var combo = $find('<%= FileNamesComboBox.ClientID %>');
                                    
                                    var parameters = {};
                                    parameters.ColumnUniqueName = "AttachmentColumn";
                                    parameters.FileName = combo.get_text();
                                    parameters.ID = +combo.get_value();

                                    var json = Sys.Serialization.JavaScriptSerializer.serialize(parameters);
                                    masterTable.fireCommand("DownloadAttachment", json);
                                    
                                    return false;
                                }
                            -->
                            </script>
                        </telerik:RadCodeBlock>
                        
    <asp:Button ID="DownloadButton" runat="server"
    Text="Download" CssClass="button"
    style="vertical-align:middle; margin-left:10px;"
    OnClientClick="return download(event);"
    OnClick="DownloadButton_Click"/>

                    </div>
                </telerik:ConfiguratorPanel>
            
            <telerik:RadCodeBlock ID="RadCodeBlock2" runat="server">
                <script type="text/javascript">
                <!--
                    function gridCommand(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);
                        }
                    }

                    function conditionalPostback(sender, eventArgs)
                    {
                        var eventArgument = eventArgs.get_eventArgument();
                        
                        if (eventArgument.indexOf("Update") > -1 || eventArgument.indexOf("PerformInsert") > -1)
                        {
                            if (upload && upload.getFileInputs()[0].value != "")
                            {
                                eventArgs.set_enableAjax(false);
                            }
                        }
                    }

                    var upload = null;

                    function uploadFileSelected(sender, args)
                    {
                        upload = sender;
                        var uploadContainer = sender.get_element();
                        var editTable = uploadContainer.parentNode.parentNode.parentNode.parentNode;
                        var fileNameTextBox = editTable.rows[0].cells[1].getElementsByTagName('input')[0];

                        fileNameTextBox.value = args.get_fileInputField().title;
                    }
                    -->
                </script>
            </telerik:RadCodeBlock>
            
            <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" EnableAJAX="true"
                DefaultLoadingPanelID="RadAjaxLoadingPanel1">
                <ClientEvents OnRequestStart="conditionalPostback" />
                <AjaxSettings>
                    <telerik:AjaxSetting AjaxControlID="RadGrid1">
                        <UpdatedControls>
                            <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                        </UpdatedControls>
                    </telerik:AjaxSetting>
                </AjaxSettings>
            </telerik:RadAjaxManager>
            
            <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
            </telerik:RadAjaxLoadingPanel>
            
            <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1"
                AutoGenerateColumns="false" AllowSorting="true"
                AllowAutomaticUpdates="true" AllowAutomaticInserts="true"
                OnItemCommand="RadGrid1_ItemCommand"
                OnItemCreated="RadGrid1_ItemCreated">
                <MasterTableView DataKeyNames="ID" CommandItemDisplay="Top" Width="100%">
                    <Columns>
                        <telerik:GridEditCommandColumn HeaderStyle-Width="30px">
                        </telerik:GridEditCommandColumn>
                        <telerik:GridBoundColumn DataField="ID" HeaderText="ID" ReadOnly="true">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="FileName" HeaderText="FileName">
                        </telerik:GridBoundColumn>
                        <telerik:GridDateTimeColumn DataField="UploadDate" HeaderText="UploadDate"
                            PickerType="DatePicker">
                        </telerik:GridDateTimeColumn>
                        <telerik:GridBoundColumn DataField="UploadedBy" HeaderText="UploadedBy">
                        </telerik:GridBoundColumn>
                        <telerik:GridAttachmentColumn DataSourceID="SqlDataSource2"
                            MaxFileSize="1048576"
                            EditFormHeaderTextFormat="Upload File:"
                            HeaderText="Download"
                            AttachmentDataField="BinaryData"
                            AttachmentKeyFields="ID"
                            FileNameTextField="FileName"
                            DataTextField="FileName"
                            UniqueName="AttachmentColumn">
                        </telerik:GridAttachmentColumn>
                    </Columns>
                </MasterTableView>
                <ClientSettings>
                    <ClientEvents OnCommand="gridCommand" />
                </ClientSettings>
            </telerik:RadGrid>
            
            <div class="module" style="margin-top:10px;">
                <b>Note:</b> Maximum file size allowed: 1MB
            </div>
            
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:TelerikConnectionString %>"

                InsertCommand="INSERT INTO [Files] ([FileName], [UploadDate], [UploadedBy]) VALUES (@FileName, @UploadDate, @UploadedBy) SET @InsertedID = SCOPE_IDENTITY()"
                SelectCommand="SELECT [ID], [FileName], [UploadDate], [UploadedBy] FROM [Files]"
                UpdateCommand="UPDATE [Files] SET [FileName] = @FileName, [UploadDate] = @UploadDate, [UploadedBy] = @UploadedBy WHERE [ID] = @ID"
                OnInserted="SqlDataSource1_Inserted"
                OnUpdated="SqlDataSource1_Updated">
                <UpdateParameters>
                    <asp:Parameter Name="FileName" Type="String" />
                    <asp:Parameter Name="UploadDate" Type="DateTime" />
                    <asp:Parameter Name="UploadedBy" Type="String" />
                    <asp:Parameter Name="ID" Type="Int32" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="FileName" Type="String" />
                    <asp:Parameter Name="UploadDate" Type="DateTime" />
                    <asp:Parameter Name="UploadedBy" Type="String" />
                    <asp:Parameter Name="InsertedID" Type="Int32" Direction="Output" />
                </InsertParameters>
            </asp:SqlDataSource>
            
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    ConnectionString="<%$ ConnectionStrings:TelerikConnectionString %>"

                SelectCommand="SELECT [ID], [BinaryData] FROM [FileData] WHERE [ID] = @ID">
                <SelectParameters>
                    <asp:Parameter Name="ID" Type="Int32" />
                </SelectParameters>
            </asp:SqlDataSource>
            <!-- content end -->
            <telerik:Footer runat="server" ID="Footer1"></telerik:Footer>
        </form>
    </body>
    </html>

Get more than expected!

Take your time to truly experience the power of RadControls for ASP.NET AJAX with a free 60-day trial backed up by Telerik’s unlimited dedicated support.

Download your RadControls for ASP.NET AJAX trial and jumpstart your development with the available Getting Started resources.

If you have any questions, do not hesitate to contact us at sales@telerik.com.

Copyright 2002-2012 © Telerik. All right reserved
Telerik Inc, 201 Jones Rd, Waltham, MA 02451