Example #1
0
        private void ProcessingNodes(XmlNode node, ref Dictionary <id, MetadataAttributes> metadataXml, MetadataAttributes attributes)
        {
            foreach (XmlNode child in node)
            {
                attributes = new MetadataAttributes();
                string metadataKey = string.Empty;
                XmlAttributeCollection attrList = child.Attributes;
                foreach (XmlAttribute attr in attrList)
                {
                    if (attr.Name == "id")
                    {
                        metadataKey = attr.Value;
                    }
                    else
                    {
                        attributes.Add(new MetadataAttribute(attr.Name, attr.Value));
                    }
                }

                if (metadataKey != string.Empty)
                {
                    metadataXml.Add(metadataKey, attributes);
                }
            }
        }
Example #2
0
    protected override void OnDataBinding(EventArgs e)
    {
        base.OnDataBinding(e);

        //check if field has a value
        if (FieldValue == null)
        {
            return;
        }

        // get the file extension
        String extension = FieldValueString.Substring(
            FieldValueString.LastIndexOf(".") + 1,
            FieldValueString.Length - (FieldValueString.LastIndexOf(".") + 1));

        // get attributes
        var    fileUploadAttributes = MetadataAttributes.OfType <FileUploadAttribute>().FirstOrDefault();
        String fileUrl          = fileUploadAttributes.FileUrl;
        String displayImageUrl  = fileUploadAttributes.DisplayImageUrl;
        String displayImageType = fileUploadAttributes.DisplayImageType;

        // check the file exists else throw validation error
        String filePath;

        if (fileUploadAttributes != null)
        {
            filePath = String.Format(fileUrl, FieldValueString);
        }
        else
        {
            // if attribute not set use default
            filePath = String.Format("~/files/{0}", FieldValueString);
        }

        // show the relavent control depending on metadata
        if (fileUploadAttributes.HyperlinkRoles != null)
        {
            // if there are roles then check:
            // if user is in one of the roles supplied
            // or if the hyperlinks are disabled
            // or if the file does not exist
            // then hide the link
            if (!fileUploadAttributes.HasRole(Roles.GetRolesForUser()) || fileUploadAttributes.DisableHyperlink || !File.Exists(Server.MapPath(filePath)))
            {
                Label1.Text        = FieldValueString;
                HyperLink1.Visible = false;
            }
            else
            {
                Label1.Visible         = false;
                HyperLink1.Text        = FieldValueString;
                HyperLink1.NavigateUrl = filePath;
            }
        }
        else
        {
            // if either hyperlinks are disabled or the
            // file does not exist then hide the link
            if (fileUploadAttributes.DisableHyperlink || !File.Exists(Server.MapPath(filePath)))
            {
                Label1.Text        = FieldValueString;
                HyperLink1.Visible = false;
            }
            else
            {
                Label1.Visible         = false;
                HyperLink1.Text        = FieldValueString;
                HyperLink1.NavigateUrl = filePath;
            }
        }

        // check file exists on file system
        if (!File.Exists(Server.MapPath(filePath)))
        {
            CustomValidator1.ErrorMessage = String.Format("{0} does not exist", FieldValueString);
            CustomValidator1.IsValid      = false;
        }

        // show the icon
        if (!String.IsNullOrEmpty(extension))
        {
            // set the file type image
            if (!String.IsNullOrEmpty(displayImageUrl))
            {
                Image1.ImageUrl = String.Format(displayImageUrl, extension + "." + displayImageType);
            }
            else
            {
                // if attribute not set the use default
                Image1.ImageUrl = String.Format("~/images/{0}", extension + "." + displayImageType);
            }

            Image1.AlternateText = extension + " file";

            // if you apply dimentions from DD Futures
            var imageFormat = MetadataAttributes.OfType <ImageFormatAttribute>().FirstOrDefault();
            if (imageFormat != null)
            {
                // if either of the dims is 0 don't set it
                // this will mean that the aspect will remain locked
                if (imageFormat.DisplayWidth != 0)
                {
                    Image1.Width = imageFormat.DisplayWidth;
                }
                if (imageFormat.DisplayHeight != 0)
                {
                    Image1.Height = imageFormat.DisplayHeight;
                }
            }
        }
        else
        {
            // if file has no extension then hide image
            Image1.Visible = false;
        }
    }
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);

            if (!IsPostBack)
            {
                HasImage = FieldValue != null;
            }

            //get metadata
            int  width         = 100;
            int  height        = 100;
            bool displayinedit = true;
            var  metadata      = MetadataAttributes.OfType <ImageFormatAttribute>().FirstOrDefault();

            if (metadata != null)
            {
                width         = metadata.DisplayWidth;
                height        = metadata.DisplayHeight;
                displayinedit = metadata.DisplayInEdit;
            }

            //Build and store cache key
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < Column.Table.PrimaryKeyColumns.Count; i++)
            {
                if (sb.Length > 0)
                {
                    sb.Append(',');
                }
                sb.Append(DataBinder.GetPropertyValue(Row, Column.Table.PrimaryKeyColumns[i].Name));
            }
            string cachekey = string.Format(
                "{0}:{1}:{2}",
                Column.Table.Name,
                Column.Name,
                sb.ToString()
                );

            ViewState["CacheKey"] = cachekey;

            //display picture if in edit mode,
            if (Mode == DataBoundControlMode.Edit && displayinedit)
            {
                //set image properties
                PlaceHolderImage.Visible = true;
                sb = new StringBuilder();
                sb.AppendFormat(
                    "~/ImageHandler.axd?table={0}&column={1}&width={2}&height={3}",
                    HttpUtility.UrlEncode(Column.Table.Name),
                    HttpUtility.UrlEncode(Column.Name),
                    width,
                    height
                    );
                for (int i = 0; i < Column.Table.PrimaryKeyColumns.Count; i++)
                {
                    sb.AppendFormat("&pk{0}={1}", i, DataBinder.GetPropertyValue(Row, Column.Table.PrimaryKeyColumns[i].Name));
                }
                ImageEdit.ImageUrl = sb.ToString();
            }
        }
    protected override void ExtractValues(IOrderedDictionary dictionary)
    {
        // get attributes
        var fileUploadAttributes = MetadataAttributes.OfType <FileUploadAttribute>().FirstOrDefault();

        String fileUrl;

        String[] extensions;
        if (fileUploadAttributes != null)
        {
            fileUrl    = fileUploadAttributes.FileUrl;
            extensions = fileUploadAttributes.FileTypes;

            if (FileUpload1.HasFile)
            {
                // get the files folder
                String filesDir = fileUrl.Substring(0, fileUrl.LastIndexOf("/") + 1);

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

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


                // Get the name of the file to upload.
                string fileName = FileUpload1.FileName;
                // Create the path and file name to check for duplicates.
                string pathToCheck = path + "\\" + FileUpload1.FileName;
                // Create a temporary file name to use for checking duplicates.
                string tempfileName = "";

                // Check to see if a file already exists with the
                // same name as the file to upload.
                if (System.IO.File.Exists(pathToCheck))
                {
                    int counter = 2;
                    while (System.IO.File.Exists(pathToCheck))
                    {
                        // if a file with this name already exists,
                        // prefix the filename with a number.
                        tempfileName = "Copy_No_" + counter.ToString() + "_of_" + FileUpload1.FileName;
                        pathToCheck  = path + "\\" + tempfileName;
                        counter++;
                    }

                    fileName = tempfileName;
                }
                // try to upload the file showing error if it fails
                try
                {
                    FileUpload1.PostedFile.SaveAs(path + "\\" + fileName);
                    dictionary[Column.Name] = fileName;
                }
                catch (Exception ex)
                {
                    CustomValidator1.IsValid      = false;
                    CustomValidator1.ErrorMessage = ex.Message;
                }
            }
        }
    }