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

Attachment column - show only record related attachment links

2 Answers 186 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Felice
Top achievements
Rank 1
Felice asked on 06 Sep 2013, 01:11 PM
I have an attachment column that, in edit mode, allow to upload and save attachment in a folder named "Allegati".
<telerik:GridAttachmentColumn FileName="attachment" FilterControlAltText="Filter columnAllegati column" HeaderText="Allegati" UniqueName="columnAllegati" Visible="False">
               </telerik:GridAttachmentColumn>
With this snippet (thanks to Princy for it), when a file is uploaded a link is generated and it is visible inside the record when it is in edit mode. See picture attached.
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
        //Generate links of attachments inside folder "Allegati" visible when in edit mode
        if (e.Item is GridEditableItem && e.Item.IsInEditMode)// Check if the Grid is in EditMode
        {
            GridEditableItem edit = (GridEditableItem)e.Item;
            DirectoryInfo dir = new DirectoryInfo(@"C:\Users\Pink\Documents\Visual Studio 2012\Projects\ManagDoc_Framework\Test1_managDoc\Test1_managDoc\Allegati");// path of the target folder where your  files are stored
 
            DirectoryInfo[] subDirs = dir.GetDirectories();
            FileInfo[] files = dir.GetFiles(); //Getting the files inside the Directory
            foreach (FileInfo fi in files) //To loop through all files for setting each file as HyperLink
            {
                HyperLink lktest = new HyperLink(); //Add HyperLink Column
                lktest.ID = "lnk" + Guid.NewGuid(); //Setting Unique IDs
                lktest.Text = fi.Name.ToString(); //Get the File name
                lktest.NavigateUrl = "#";
                lktest.Attributes.Add("Onclick", "ViewCheck('" + fi.Name + "')"); // Calling the JS event
                //Adding the HyperLink to EditForm
                edit["columnAllegati"].Controls.Add(lktest);
                edit["columnAllegati"].Controls.Add(new LiteralControl("<br>"));
            }
        }
    }
The above feature works very well and I am happy of the result I got thanks to the help of this forum members.
Now what I would like to achieve is to show for each record only the links of the uploaded files relevant that particular record.
I mean If I load an attachment named "Mickey Mouse" from record A, and an attachment named "Donald duck" from record B, when I open record A I would like to see only the link to Mickey Mouse and when I open record B I would like to see only the link to Donald Duck.
Like in this forum, each thread shows links to the own attachments.
How can I achieve that?

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 09 Sep 2013, 06:55 AM
Hi Felice,

In-order to achieve the desired functionality, you need to save the entire FilePath of the file in corresponding row of the DataBase in a data column. Here is a sample code snippet I tried.

C#:
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem edit = (GridEditableItem)e.Item;
    RadUpload uplo = (RadUpload)edit["Attachment"].Controls[0]; //accessing the RadUpload
    string fullPath = Path.Combine(Server.MapPath("target"), uplo.UploadedFiles[0].FileName);//getting the fullpath of the file going to be uploaded
    string Id = edit.OwnerTableView.DataKeyValues[edit.ItemIndex]["DataKeyName"].ToString(); //getting the datakeyvalue of the row, to uniquely identify a row
    string updateQuery = "Update TableName set ColumnFileName='" + fullPath + "' where DataKeyFieldName='" + Id + "'"; //adding the filename of the uploaded file to db
    conn.Open();
    SqlCommand.CommandText = updateQuery;
    SqlCommand.Connection = conn;
    SqlCommand.ExecuteNonQuery();
    conn.Close();   
}
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode) //in edit mode to show the uploaded file
    {
             
        GridEditableItem edit = (GridEditableItem)e.Item;
        string Id = edit.OwnerTableView.DataKeyValues[edit.ItemIndex]["DataKeyName"].ToString();// getting the datakeyvalue of the row
        string selectFilterQuery = "select ColumnFileName from [TableName ] where DataKeyFieldName='" + Id + "'"; //query for retrieving the upoaded file path of the row
        SqlCommand.CommandText = selectFilterQuery;
        SqlDataAdapter adapter = new SqlDataAdapter(selectFilterQuery, conn);
        DataTable dt = new DataTable();
        conn.Open();
        adapter.Fill(dt);
        conn.Close();
        HyperLink lktest = new HyperLink(); //adding hyperlink
        lktest.ID = "lnk" + Guid.NewGuid();  
        string dbf_File = System.IO.Path.GetFileName( dt.Rows[0].ItemArray[0].ToString()); //retieving only the file name from the full path
        lktest.Text = dbf_File;
        lktest.NavigateUrl = "#";
        lktest.Attributes.Add("Onclick", "ViewCheck('" + dbf_File + "')");
        edit["Attachment"].Controls.Add(lktest);
        edit["Attachment"].Controls.Add(new LiteralControl("<br>"));     
    }
}

Javascript:
<script type="text/javascript">
        function ViewCheck(filename) {
            var targetfile = "target/" + filename;
            var openWnd = radopen(targetfile, "RadWindowDetails");
        }
</script>

Let me know if you have any concern.

Thanks,
Princy.
0
Felice
Top achievements
Rank 1
answered on 10 Sep 2013, 04:46 PM
Thanks a lot Princy.
I slightly changed the dynamic but along your indication it is now working fine.
Thanks for the great and professional support.
Felice
Tags
Grid
Asked by
Felice
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Felice
Top achievements
Rank 1
Share this question
or