locked
FileUpload FieldTemplate not working. RRS feed

  • Question

  • User-1759624489 posted

    Helloo I need to upload files and save the docuemnt path only.

     

    I created File.ascx and File.ascx.cs

    I also Created File_edit.ascx and File_Edit.ascx.cs

     

    In GridView Mode, i want to show an hyperlink so they can see the file, in edit mode, it has to show the fileupload control.

     

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="File.ascx.cs" Inherits="File" %>
    <asp:HyperLink ID="HyperLink1" runat="server"  NavigateUrl="<%# GetContract(FieldValueString) %>"></asp:HyperLink>

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class File : System.Web.DynamicData.FieldTemplateUserControl
    {
        public override Control DataControl
        {
            get
            {
                return HyperLink1;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
          
        }

        protected  string  GetContract(string filename)
        {
            HyperLink1.NavigateUrl = filename;
            HyperLink1.Text = "Ver Archivo";
            return HyperLink1.NavigateUrl;
        }
    }

     

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="File_Edit.ascx.cs" Inherits="FileEdit" %>
    <asp:FileUpload ID="FileUploadEdit" runat="server" Visible="false" /><asp:CustomValidator
        ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator"></asp:CustomValidator>

     

    using System;
    using System.Collections.Specialized;
    using System.IO;
    using System.Linq;
    using System.Web.DynamicData;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.Web.DynamicData;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;


    public partial class FileEdit : FieldTemplateUserControl

    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
            //CustomValidator1.ServerValidate += new ServerValidateEventHandler(CustomValidator1_ServerValidate);
           
        }

        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            // get Edit type Select or Upload
           
            string    imagesDir = "~/Contracts";

            // resolve full path c:\... etc
            String path = Server.MapPath(imagesDir);

            // get files extension without the dot
            String fileExtension = FileUploadEdit.FileName.Substring(FileUploadEdit.FileName.LastIndexOf(".") + 1).ToLower();

            // get allowed extensions
            String[] allowedExtensions = {"pdf"};

                if (allowedExtensions.Contains(fileExtension))
                {
                    // try to upload the file showing error if it fails
                    try
                    {
                        FileUploadEdit.PostedFile.SaveAs(path + "\\" + FileUploadEdit.FileName);
                        //ImageEdit.ImageUrl = imagesDir + "/" + FileUploadEdit.FileName;
                        //ImageEdit.AlternateText = FileUploadEdit.FileName;
                        dictionary[Column.Name] = FileUploadEdit.FileName;
                    }
                    catch (Exception ex)
                    {
                        // display error
                        CustomValidator1.IsValid = false;
                        CustomValidator1.ErrorMessage = ex.Message;
                        dictionary[Column.Name] = null;
                    }
                }
            }
        }

     

    Tuesday, September 9, 2008 11:13 AM

Answers

  • User-1759624489 posted

    Hello. It worked after putting this code

     

        public static void DisablePartialRenderingForUpload(Page page, MetaTable table)
        {
            foreach (var column in table.Columns)
            {
                // TODO this depends on the name of the field template, need to fix
                if (String.Equals(column.UIHint, "DBImage", StringComparison.OrdinalIgnoreCase)
                    || String.Equals(column.UIHint, "FileImage", StringComparison.OrdinalIgnoreCase))
                {
                    var sm = ScriptManager.GetCurrent(page);
                    if (sm != null)
                    {
                        sm.EnablePartialRendering = false;
                    }
                    break;
                }
            }
        }
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 9, 2008 2:52 PM

All replies

  • User-1759624489 posted

    Now, its showing the fileupload conttrol.

     

    But I got another problem here

     

      protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            string    imagesDir = "~/Contracts";

            // resolve full path c:\... etc
            String path = Server.MapPath(imagesDir);

            // get files extension without the dot
            String fileExtension = FileUploadEdit.FileName.Substring(FileUploadEdit.FileName.LastIndexOf(".") + 1).ToLower();

    FileUploadEdit is empty, even if I select a file.

     

    Why?

    Tuesday, September 9, 2008 11:28 AM
  • User188291263 posted

     Because of partial rendering in Ajax,turn it off for this page

    Tuesday, September 9, 2008 11:36 AM
  • User-1759624489 posted

    How? The Ajax controls are on a master page not on this page

     

    Thx

    Tuesday, September 9, 2008 12:00 PM
  • User188291263 posted

     you can turn off partial rendering per page.

    Take a look at this solution,it uses iframe
    http://geekswithblogs.net/rashid/archive/2007/08/01/Create-An-Ajax-Style-File-Upload.aspx

    Tuesday, September 9, 2008 12:08 PM
  • User-1759624489 posted

    It doesnt say how to disable partial rendering in each page.

    I dont use iframes ever.

    Tuesday, September 9, 2008 12:14 PM
  • User-1759624489 posted

    I already disabled partial rendering on the page but it doesnt seem to work

     

    PostedFile is null.

     

       protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            string    imagesDir = "~/Contracts";

            // resolve full path c:\... etc
            String path = Server.MapPath(imagesDir);

            // get files extension without the dot
            String fileExtension = FileUploadEdit.FileName.Substring(FileUploadEdit.FileName.LastIndexOf(".") + 1).ToLower();

            // get allowed extensions
            String[] allowedExtensions = {"pdf"};

                if (allowedExtensions.Contains(fileExtension))
                {
                    // try to upload the file showing error if it fails
                    try
                    {
                        FileUploadEdit.PostedFile.SaveAs(path + "\\" + FileUploadEdit.FileName);
                        //ImageEdit.ImageUrl = imagesDir + "/" + FileUploadEdit.FileName;
                        //ImageEdit.AlternateText = FileUploadEdit.FileName;
                        dictionary[Column.Name] = FileUploadEdit.FileName;
                    }
                    catch (Exception ex)
                    {
                        // display error
                        CustomValidator1.IsValid = false;
                        CustomValidator1.ErrorMessage = ex.Message;
                        dictionary[Column.Name] = null;
                    }
                }
            }
     

    Tuesday, September 9, 2008 12:17 PM
  • User188291263 posted

     Try to put control inside IFrame

    Tuesday, September 9, 2008 12:31 PM
  • User-1759624489 posted

     That wont work.

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="File_Edit.ascx.cs" Inherits="FileEdit" %>
    <asp:FileUpload ID="myFile" runat="server" Visible="true"  /><asp:CustomValidator
        ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator"></asp:CustomValidator>

     

    It doesnt show

    Tuesday, September 9, 2008 12:52 PM
  • User-330204900 posted

    Hi Lavalencia, Have you had a look at this article Dynamic Data: Part 2 - FileImage_Edit FieldTemplate of mine it does everything you want but works with images I'm sure it could be modified to do files and just show filename and a fieltype image Big Smile

    Tuesday, September 9, 2008 1:53 PM
  • User-1759624489 posted

    I used yours as an example. But I didnt copy all the code.

     

    Just the extractvalues method

     

      protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            string    imagesDir = "~/Contracts";

            // resolve full path c:\... etc
            String path = Server.MapPath(imagesDir);

            if (myFile.HasFile)
            {
                String fileExtension = myFile.FileName.Substring(myFile.FileName.LastIndexOf(".") + 1).ToLower();

                // get allowed extensions
                String[] allowedExtensions = { "pdf" };

                if (allowedExtensions.Contains(fileExtension))
                {
                    // try to upload the file showing error if it fails
                    try
                    {
                        myFile.PostedFile.SaveAs(path + "\\" + myFile.FileName);
                        //ImageEdit.ImageUrl = imagesDir + "/" + FileUploadEdit.FileName;
                        //ImageEdit.AlternateText = FileUploadEdit.FileName;
                        dictionary[Column.Name] = myFile.FileName;
                    }
                    catch (Exception ex)
                    {
                        // display error
                        CustomValidator1.IsValid = false;
                        CustomValidator1.ErrorMessage = ex.Message;
                        dictionary[Column.Name] = null;
                    }
                }
            }
            }
        }

     

    The problem is that its always returnin null in the PostedFile.

    Tuesday, September 9, 2008 2:04 PM
  • User-330204900 posted

    I'm going to be busy earning some money for the next few days, but I will have alook at making a FileUpload FieldTemplate and posting it on my blog as soon as possible [:D]

    Hope this helps [:D]

    Tuesday, September 9, 2008 2:16 PM
  • User-1759624489 posted

    Hello. It worked after putting this code

     

        public static void DisablePartialRenderingForUpload(Page page, MetaTable table)
        {
            foreach (var column in table.Columns)
            {
                // TODO this depends on the name of the field template, need to fix
                if (String.Equals(column.UIHint, "DBImage", StringComparison.OrdinalIgnoreCase)
                    || String.Equals(column.UIHint, "FileImage", StringComparison.OrdinalIgnoreCase))
                {
                    var sm = ScriptManager.GetCurrent(page);
                    if (sm != null)
                    {
                        sm.EnablePartialRendering = false;
                    }
                    break;
                }
            }
        }
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 9, 2008 2:52 PM
  • User-330204900 posted

    Hi Levalencia, I've just posted a feature complete FileUpload FieldTemplate here on my blog :Dynamic Data: Part 3-FileUpload FieldTemplates

    Hope this helps [:D]

    Monday, September 15, 2008 9:47 AM