locked
Custom Delete RRS feed

  • Question

  • User1852019361 posted

    Hey all;

    i have this table "Projects" with fields

    ProjectId,

    CityId,

    ProjectTitle,

    Photo....

    now i want to clear photo field for specfic record in my table..

    but the delete button would delete the entire record .

    so any idea on how to add delete button for the photo field???

    Saturday, February 18, 2012 6:19 AM

Answers

  • User-330204900 posted

    OK I woudl add a checkbox to the Field_Edit.ascx file

    <asp:CheckBox ID="DeleteCheckBox" runat="server" test="Delete" />
    

     then in the ExtractValues method check the checkbox if set set the dictionary to null this will null the value

    protected override void ExtractValues(IOrderedDictionary dictionary)
    {
        if (FileUpload1.HasFile)
        {
            string fileAttributeFullPath = Utility.FrontendPhysicalPath + GetFileAttributePath();
            string fileName = Utility.GetRand() + "-" + FileUpload1.FileName;
    
            FileUpload1.SaveAs(fileAttributeFullPath + fileName);
            if (!String.IsNullOrEmpty(ViewState["FieldValueString"].ToString()))
                try
                {
                    File.Delete(fileAttributeFullPath + ViewState["FieldValueString"].ToString());
                }
                catch { }
    
            dictionary[Column.Name] = fileName;
        }
        if (DeleteCheckBox.Checked)
            dictionary[Column.Name] = null;
    }
    

    give that a try.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 19, 2012 1:06 PM
  • User-330204900 posted

    I would just add an image control and then have a set of jpegs named like this

    png.jpeg

    gif.jpeg

    jpeg.jpeg

    etc.

    then set the image to the one that matches the file extension.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 20, 2012 12:35 PM

All replies

  • User-1618234021 posted

    You can set it to null using the following query:

    update projects 
    set Photo=null 
    where ProjectId=1
    

    Above is just a sample query, you have to pass corresponding project id to set its photo to null.

    Saturday, February 18, 2012 7:51 AM
  • User1852019361 posted

    how to do it in dynamicdata page ... not in sql

    Saturday, February 18, 2012 2:05 PM
  • User-330204900 posted

    you need to add a button to your field template that setts the

    dictionary[Column.Name] = null;

    and is you store the fiel on disk also delete the file.

    Saturday, February 18, 2012 6:43 PM
  • User1852019361 posted

    can you give steps on how to code this button ...

    also how to pass file name so i can delete it??

    Sunday, February 19, 2012 7:22 AM
  • User-330204900 posted

    What file upload field template are you using?

    Sunday, February 19, 2012 11:11 AM
  • User1852019361 posted

    File.ascx and File_Edit.ascx ...... the default ones

    Sunday, February 19, 2012 11:58 AM
  • User-330204900 posted

    There is no default File upload fiel;d template, where did you get it, coudl you post the code and markup then I can modify that and post back.

    Sunday, February 19, 2012 12:12 PM
  • User1852019361 posted

    File.ascx

    <%@ Control Language="C#" CodeFile="File.ascx.cs" Inherits="FileField" %>
    <asp:HyperLink ID="HyperLink1" Visible="false" Target="_blank" runat="server">Download</asp:HyperLink>

    File.ascx.cs

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Xml.Linq;
    using System.Web.DynamicData;

    public partial class FileField : System.Web.DynamicData.FieldTemplateUserControl
    {
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);

            if (!String.IsNullOrEmpty(FieldValueString))
            {
                var metadata = MetadataAttributes.OfType<FileAttribute>().FirstOrDefault();

                if (metadata != null)
                {
                    HyperLink1.NavigateUrl = Utility.FrontendVirtualPath + metadata.Path + FieldValueString;
                    HyperLink1.Visible = true;
                }
            }
        }

        public override Control DataControl
        {
            get
            {
                return HyperLink1;
            }
        }
    }

    File_Edit.ascx

    <%@ Control Language="C#" CodeFile="File_Edit.ascx.cs" Inherits="File_EditField" %>
    <asp:HyperLink ID="HyperLink1" Visible="false" Target="_blank" runat="server">Download</asp:HyperLink>
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" CssClass="droplist"
        ControlToValidate="FileUpload1" Display="Dynamic" Enabled="false" />
    <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator1" CssClass="droplist"
        ControlToValidate="FileUpload1" Display="Dynamic" Enabled="false" />
    <asp:DynamicValidator runat="server" ID="DynamicValidator1" CssClass="droplist" ControlToValidate="FileUpload1"
        Display="Dynamic" />


    File_Edit.ascx.cs

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Xml.Linq;
    using System.Web.DynamicData;
    using System.IO;

    public partial class File_EditField : System.Web.DynamicData.FieldTemplateUserControl
    {
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);

            if (!String.IsNullOrEmpty(FieldValueString))
            {
                string fileAttributePath = GetFileAttributePath();

                HyperLink1.NavigateUrl = Utility.FrontendVirtualPath + fileAttributePath + FieldValueString;
                HyperLink1.Visible = true;
            }
            else
            {
                SetUpValidator(RequiredFieldValidator1);
            }

            ViewState["FieldValueString"] = FieldValueString;

            SetUpValidator(RegularExpressionValidator1);
            SetUpValidator(DynamicValidator1);
        }

        protected override void ExtractValues(IOrderedDictionary dictionary)
        {
            if (FileUpload1.HasFile)
            {
                string fileAttributeFullPath = Utility.FrontendPhysicalPath + GetFileAttributePath();
                string fileName = Utility.GetRand() + "-" + FileUpload1.FileName;

                FileUpload1.SaveAs(fileAttributeFullPath + fileName);
                if (!String.IsNullOrEmpty(ViewState["FieldValueString"].ToString()))
                    try
                    {
                        File.Delete(fileAttributeFullPath + ViewState["FieldValueString"].ToString());
                    }
                    catch { }

                dictionary[Column.Name] = fileName;
            }
        }

        public override Control DataControl
        {
            get
            {
                return FileUpload1;
            }
        }

        private string GetFileAttributePath()
        {
            var metadata = MetadataAttributes.OfType<FileAttribute>().FirstOrDefault();

            if (metadata != null)
                return metadata.Path;
            else
                return String.Empty;
        }
    }



    Sunday, February 19, 2012 12:36 PM
  • User-330204900 posted

    OK I woudl add a checkbox to the Field_Edit.ascx file

    <asp:CheckBox ID="DeleteCheckBox" runat="server" test="Delete" />
    

     then in the ExtractValues method check the checkbox if set set the dictionary to null this will null the value

    protected override void ExtractValues(IOrderedDictionary dictionary)
    {
        if (FileUpload1.HasFile)
        {
            string fileAttributeFullPath = Utility.FrontendPhysicalPath + GetFileAttributePath();
            string fileName = Utility.GetRand() + "-" + FileUpload1.FileName;
    
            FileUpload1.SaveAs(fileAttributeFullPath + fileName);
            if (!String.IsNullOrEmpty(ViewState["FieldValueString"].ToString()))
                try
                {
                    File.Delete(fileAttributeFullPath + ViewState["FieldValueString"].ToString());
                }
                catch { }
    
            dictionary[Column.Name] = fileName;
        }
        if (DeleteCheckBox.Checked)
            dictionary[Column.Name] = null;
    }
    

    give that a try.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 19, 2012 1:06 PM
  • User1852019361 posted

    thanks it works fine ..

    one thing else .. how to display file name or file icon next to file upload control  .. so that i can see it in edit mode and in display mode(grid view)

    Sunday, February 19, 2012 1:51 PM
  • User-330204900 posted

    do you mean a file type icon?

    Sunday, February 19, 2012 2:20 PM
  • User1852019361 posted

    what i mean is that in the display mode (gridview) i only see "Download" word but don't see file name or any icon about it...

    also in edit mode i see the "FileUpload" control with empty string ...

    so i need to tell users that there is a file already uploaded (if exist)

    Sunday, February 19, 2012 2:54 PM
  • User-330204900 posted

    Then change the field template to use the FiedlValueString instead of the word Download.

    Sunday, February 19, 2012 6:44 PM
  • User1852019361 posted

    how to display file type icon??

    Monday, February 20, 2012 12:13 PM
  • User-330204900 posted

    I would just add an image control and then have a set of jpegs named like this

    png.jpeg

    gif.jpeg

    jpeg.jpeg

    etc.

    then set the image to the one that matches the file extension.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 20, 2012 12:35 PM
  • User-1678569790 posted

    Hi,

    It worked for me, thanks.

    ___________________

    Oscar Gómez

    Engineer at PSL

    http://www.pslcorp.com/

    Wednesday, February 22, 2012 11:47 PM