This is a migrated thread and some comments may be shown as answers.

Detect if item is being created

1 Answer 14 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Felice
Top achievements
Rank 1
Felice asked on 11 Sep 2013, 05:57 AM
I have the following code to save the attachments in a folder outside the DB.
Line 5 is giving me troubles during the creation of new records because the value to assign to "recordID" at the time of creation of a new record does not yet exist.



protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            GridEditableItem edititem = (GridEditableItem)e.Item;
            GridEditableItem editedItem = e.Item as GridEditableItem;
                string recordID = editedItem.GetDataKeyValue("TransazioneID").ToString();
                string subPath = "Allegati\\" + recordID;
                bool isExists = System.IO.Directory.Exists(Server.MapPath(subPath));
                if (!isExists)
                    System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
                RadUpload upload = (edititem.EditManager.GetColumnEditor("columnAllegati") as GridAttachmentColumnEditor).RadUploadControl;
                upload.TargetFolder = "Allegati\\" + recordID;
 
            }
        }
So my idea is to add a condition on top of this method that would execute it only if a new record is not being created. Something along those lines.....
How can I do something like that? 

1 Answer, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 11 Sep 2013, 12:00 PM
Hello,

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)
        {
            if (e.Item is GridDataInsertItem)
            {
                GridEditableItem edititem = (GridEditableItem)e.Item;
                GridEditableItem editedItem = e.Item as GridEditableItem;
                string recordID = Guid.NewGuid().ToString();
 
                string subPath = "Allegati\\" + recordID; // Set here Tempory Folder name
 
                bool isExists = System.IO.Directory.Exists(Server.MapPath(subPath));
                if (!isExists)
 
                    System.IO.Directory.CreateDirectory(Server.MapPath(subPath));
                RadUpload upload = (edititem.EditManager.GetColumnEditor("columnAllegati") as GridAttachmentColumnEditor).RadUploadControl;
                upload.TargetFolder = "Allegati\\" + recordID; // Set here Tempory Folder name
 
            }
        }
    }
    protected void RadGrid1_InsertCommand(object sender, GridCommandEventArgs e)
    {
        GridEditableItem edititem = (GridEditableItem)e.Item;
        GridEditableItem editedItem = e.Item as GridEditableItem;
        RadUpload upload = (edititem.EditManager.GetColumnEditor("columnAllegati") as GridAttachmentColumnEditor).RadUploadControl;
        DirectoryInfo info = new DirectoryInfo(Server.MapPath(upload.TargetFolder));
 
        // Perfomr Insert here and get recordID
        string recordID = "1";
        string subPath = "Allegati\\" + recordID;
 
        DirectoryInfo targetinfo = new DirectoryInfo(Server.MapPath(subPath));
        if (!targetinfo.Exists)
            targetinfo.Create();
 
        // Copy file from temporary location and then delete this file
        foreach (FileInfo file in info.GetFiles())
        {
            file.CopyTo(Server.MapPath(subPath) + "\\" + file.Name);
            file.Delete();
        }
 
        // Delete temp directory
        info.Delete();
    }

Note :
If you set Temporary folder path in above code snippet. then in some interval you can able to delete all files from there.

This things needs when i upload one file in insert mode then i am click on CANCEL button in-place of INSERT button. At  that time file is saved in my server but this file is not needed. So i have to remove this file from this folder. Because this file is not needed any where then it is useless to save in my server.

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Felice
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Share this question
or