GridAttachmentColumn column does not work while insert/Update in grid

2 posts, 0 answers
  1. Tejas
    Tejas avatar
    31 posts
    Member since:
    Mar 2011

    Posted 28 Apr 2011 Link to this post

    Hi,

    I have a GridAttachmentColumn in my grid which allows user to upload files. Unfortunately, In my Insert and Update routing, I'm ending up with null UploadedFileCollection collection even If user uploads a file. Note that, I don't use allowFileExtension and MaxFileSize properties in my GridAttachmentColumn columns property.
    <telerik:RadGrid ID="grdFiles" runat="server" AutoGenerateColumns="False" OnInsertCommand="grdFiles_Insert"
            OnUpdateCommand="grdFiles_Update" OnEditCommand="grdFiles_Command" AllowSorting="True"
            OnDeleteCommand="grdFiles_Command"  OnNeedDataSource="grdFiles_NeedDataSource"
            OnCancelCommand="grdFiles_Command" OnItemDataBound="grdFiles_ItemDataBound" PageSize="10"
            OnItemCommand="grdFiles_ItemCommand" GridLines="None">
        <MasterTableView DataKeyNames="CollectionFileID"
            CommandItemDisplay="Top" EditMode="EditForms" >
        <CommandItemSettings   ShowAddNewRecordButton="true" AddNewRecordImageUrl="~/Images/AddRecord.gif"
                     AddNewRecordText="Upload New Collection File" >
        </CommandItemSettings>
        <RowIndicatorColumn  FilterControlAltText="Filter RowIndicator column">
        <HeaderStyle Width="20px"></HeaderStyle>
        </RowIndicatorColumn>
     
        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
        <HeaderStyle Width="20px"></HeaderStyle>
        </ExpandCollapseColumn>
        <NoRecordsTemplate>
            <div> No files available.</div>
        </NoRecordsTemplate>
        <Columns>
            
            <telerik:GridBoundColumn DataField="CollectionFileID" DataType="System.Int32"
                FilterControlAltText="Filter CollectionFileID column" HeaderText="ID"
                ReadOnly="True" SortExpression="CollectionFileID"
                UniqueName="CollectionFileID" >
            </telerik:GridBoundColumn>
     
            <telerik:GridBoundColumn DataField="OriginalFileName" FilterControlAltText="Filter OriginalFileName column"
                HeaderText="Original Name" UniqueName="OriginalFileName" ReadOnly="true"
                SortExpression="OriginalFileName" >
            </telerik:GridBoundColumn>
     
            <telerik:GridBoundColumn DataField="AssignedFileName"
                FilterControlAltText="Filter AssignedFileName column" HeaderText="Assigned Name"
                SortExpression="AssignedFileName" Visible="false" UniqueName="AssignedFileName"  ReadOnly="true">
            </telerik:GridBoundColumn>
     
            <telerik:GridDateTimeColumn DataField="UploadDate" 
                FilterControlAltText="Filter UploadDate column" HeaderText="Date Uploaded"
                SortExpression="UploadDate" UniqueName="UploadDate"  ReadOnly="true"  DataFormatString="{0:d}">
            </telerik:GridDateTimeColumn>  
            <telerik:GridDateTimeColumn DataField="Modified" 
                FilterControlAltText="Filter Modified column" HeaderText="Date Modified"
                SortExpression="Modified" UniqueName="Modified" ReadOnly="true"  DataFormatString="{0:d}">
            </telerik:GridDateTimeColumn>
     
            <telerik:GridTemplateColumn DataField="Description"
                FilterControlAltText="Filter Description column"
                HeaderText="Description/Instruction" SortExpression="Description"
                UniqueName="Description">
                 
               <EditItemTemplate>
                    <telerik:RadTextBox ID="DescriptionTextBox" runat="server"
                        Text='<%# Bind("Description") %>'
                         Width="300px" Height="150px" TextMode="MultiLine"></telerik:RadTextBox>
                                     
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="DescriptionLabel" runat="server"
                        Text='<%# Eval("Description") %>'></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridAttachmentColumn
                EditFormHeaderTextFormat="Upload File:" HeaderText="Download" 
                AttachmentDataField="BinaryData" AttachmentKeyFields="CollectionFileID"
                FileNameTextField="OriginalFileName"  DataTextField="OriginalFileName" UniqueName="AttachmentColumn">
            </telerik:GridAttachmentColumn>
            <telerik:GridEditCommandColumn HeaderText="Edit" ButtonType="LinkButton">
                <HeaderStyle Width="2%" />
            </telerik:GridEditCommandColumn>
            <telerik:GridButtonColumn Text="Delete" HeaderText="Delete" CommandName="Delete" ButtonType="LinkButton">
                            <HeaderStyle Width="2%" />
            </telerik:GridButtonColumn>
        </Columns>
        <EditFormSettings>
        <EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
        </EditFormSettings>
        </MasterTableView>
        <FilterMenu EnableImageSprites="False"></FilterMenu>
     
        <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default"></HeaderContextMenu>
        </telerik:RadGrid>
    Here is a code for insertion:
    protected void grdFiles_Insert(object sender, GridCommandEventArgs e)
            {
                //Insertion Logic
                if (e.Item is GridEditableItem && e.Item.IsInEditMode)
                {
                    GridEditableItem item = e.Item as GridEditableItem;
                     
                    RadTextBox txtDesc = item.FindControl("DescriptionTextBox") as RadTextBox;
     
                    RadUpload upload = (item.EditManager.GetColumnEditor("AttachmentColumn") as GridAttachmentColumnEditor).RadUploadControl;
                    if (upload != null)
                    {
                        UploadedFileCollection upFiles = upload.UploadedFiles;
     
                        if (upFiles.Count == 0)
                        {
                            //throw new Exception("No file uploaded");
                            string script = string.Format("alert('No file uploaded');");
                            ScriptManager.RegisterStartupScript(Page, typeof(Page), "myNoFileScript", script, true);
                             
                            return;
                        }
     
                        if (upload.InvalidFiles.Count > 0)
                        {
                            //throw new Exception("Invalid file type or file size is greater than maximum allowed.");
                            string script = string.Format("alert('Invalid file type or file size is greater than maximum allowed.');");
                            ScriptManager.RegisterStartupScript(Page, typeof(Page), "myInvalidFileScript", script, true);
                            return;
                        }
     
                        CollectionFile oFile = new CollectionFile();
     
                        if (txtDesc != null)
                        {
                            oFile.Description = txtDesc.Text.Trim();
                        }
     
                         
     
                        DateTime uploadTime = DateTime.Now;
     
                        oFile.UploadDate = uploadTime;
     
                         
                        upload.OverwriteExistingFiles = true;
                         
                        foreach (UploadedFile upfile in upFiles)
                        {
                            oFile.OriginalFileName = upfile.GetName();
                            oFile.FileSize = upfile.ContentLength;
                            oFile.FileExtension = upfile.GetExtension();
                            string assignedSystemFileName = System.IO.Path.GetFileNameWithoutExtension(upfile.GetName()) + "_" + System.Guid.NewGuid().ToString() + upfile.GetExtension();
                            oFile.AssignedFileName = assignedSystemFileName;
                            //Save File to server's file system
                            upfile.SaveAs(Server.MapPath("~/CollectionUploads/" + assignedSystemFileName), true);
                        }
     
                        oFile.Modified = uploadTime;
     
                         
     
                        CollectionFile.Insert(oFile);
                                             
                        oFile = null;
                        grdFiles.Rebind();
                         
                    }
                }
         
            }

    Here is my Update Method:
    protected void grdFiles_Update(object sender, GridCommandEventArgs e)
            {
                if (e.Item is GridEditableItem && e.Item.IsInEditMode)
                {
                    GridEditableItem itm = e.Item as GridEditableItem;
     
                    string collFileId = Convert.ToString(itm.GetDataKeyValue("CollectionFileID"));
                    //Edit Files
                    RadTextBox txtDesc = itm.FindControl("DescriptionTextBox") as RadTextBox;
     
                    RadUpload upload = (itm.EditManager.GetColumnEditor("AttachmentColumn") as GridAttachmentColumnEditor).RadUploadControl;
     
                    CollectionFile oFile = CollectionFile.GetEntity(collFileId);
     
                    if (oFile != null)
                    {
                        DateTime ModifyTime = DateTime.Now;
     
                        if (txtDesc != null)
                            oFile.Description = txtDesc.Text.Trim();
     
                        if (upload != null)
                        {
                            upload.OverwriteExistingFiles = true;
                            UploadedFileCollection upFiles = upload.UploadedFiles;
     
                            //If new File is uploaded then delete old file
                            if (upFiles.Count != 0)
                            {
                                //New File is selected to upload
                                if (File.Exists(Server.MapPath("~/CollectionUploads/" + oFile.AssignedFileName)))
                                {
                                    File.Delete(Server.MapPath("~/CollectionUploads/" + oFile.AssignedFileName));
                                }
                            }
     
                            foreach (UploadedFile upfile in upFiles)
                            {
                                oFile.OriginalFileName = upfile.GetName();
                                oFile.FileSize = upfile.ContentLength;
                                oFile.FileExtension = upfile.GetExtension();
                                string assignedSystemFileName = System.IO.Path.GetFileNameWithoutExtension(upfile.GetName()) + "_" + System.Guid.NewGuid().ToString() + upfile.GetExtension();
                                oFile.AssignedFileName = assignedSystemFileName;
                                //Save File to server's file system
                                upfile.SaveAs(Server.MapPath("~/CollectionUploads/" + assignedSystemFileName), true);
                            }
     
                        }
                        oFile.Modified = ModifyTime;
     
                        CollectionFile.Update(oFile);
                         
                        oFile = null;
                        grdFiles.Rebind();
                    }
                }
                 
            }

    what am I doing wrong here ?

    Just To Update you that my Grid is in RadAjaxPanel which in turn, uses loading panel. Anytime when I set EnableAJAX="false" for RadAjaxPanel then Upload control works well, but It stop working as soon as I set EnableAJAX="true".

    Thanks

    Tejas
  2. Pavlina
    Admin
    Pavlina avatar
    6084 posts

    Posted 03 May 2011 Link to this post

    Hi Tejas,

    Please refer to this help article for details on the matter.

    All the best,
    Pavlina
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top