PROJECT DESCRIPTION
An example code to get the file path to the uploaded file using Async Upload inside RadGrid.
Supports
Insert,
Update and
Delete features.
This scenario contains a RadGrid control that has data bound to it using
Declarative DataSource and is using Form
Template Edit Form. The FormTemplate contains a RadAsyncUpload control together with an Insert/Update button.
Markup
<
telerik:RadGrid
RenderMode
=
"Lightweight"
ID
=
"RadGrid1"
Width
=
"100%"
runat
=
"server"
AutoGenerateColumns
=
"False"
AllowSorting
=
"True"
PageSize
=
"3"
DataSourceID
=
"SqlDataSource1"
OnInsertCommand
=
"RadGrid1_InsertCommand"
OnUpdateCommand
=
"RadGrid1_UpdateCommand"
OnDeleteCommand
=
"RadGrid1_DeleteCommand"
>
<
MasterTableView
AutoGenerateColumns
=
"false"
CommandItemDisplay
=
"Top"
DataSourceID
=
"SqlDataSource1"
DataKeyNames
=
"FileID,FilePath,FileName"
>
<
Columns
>
<
telerik:GridEditCommandColumn
UniqueName
=
"EditColumn"
></
telerik:GridEditCommandColumn
>
<
telerik:GridBoundColumn
DataField
=
"FileName"
ReadOnly
=
"True"
FilterControlAltText
=
"Filter FileName column"
HeaderText
=
"FileName"
SortExpression
=
"FileName"
UniqueName
=
"FileName"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"FileID"
DataType
=
"System.Int32"
FilterControlAltText
=
"Filter FileID column"
HeaderText
=
"FileID"
ReadOnly
=
"True"
SortExpression
=
"FileID"
UniqueName
=
"FileID"
Display
=
"false"
>
</
telerik:GridBoundColumn
>
<
telerik:GridImageColumn
UniqueName
=
"ImageColumn"
HeaderText
=
"Image"
DataImageUrlFields
=
"FilePath"
>
</
telerik:GridImageColumn
>
<
telerik:GridHyperLinkColumn
SortExpression
=
"FilePath"
DataTextFormatString
=
"{0}"
DataNavigateUrlFields
=
"FilePath"
UniqueName
=
"FilePath"
HeaderText
=
"File Path"
DataTextField
=
"FilePath"
>
</
telerik:GridHyperLinkColumn
>
<
telerik:GridButtonColumn
CommandName
=
"Delete"
ButtonType
=
"FontIconButton"
></
telerik:GridButtonColumn
>
</
Columns
>
<
EditFormSettings
EditFormType
=
"Template"
>
<
FormTemplate
>
<
telerik:RadAsyncUpload
TargetFolder
=
"Files"
OnFileUploaded
=
"RadAsyncUpload1_FileUploaded"
ID
=
"RadAsyncUpload1"
runat
=
"server"
>
</
telerik:RadAsyncUpload
>
<
asp:Button
ID
=
"Button1"
runat
=
"server"
Text='<%# Container is IGridInsertItem ? "Insert" : "Update" %>' CommandName='<%# Container is IGridInsertItem ? "PerformInsert" : "Update" %>' />
</
FormTemplate
>
</
EditFormSettings
>
</
MasterTableView
>
<
ClientSettings
>
</
ClientSettings
>
</
telerik:RadGrid
>
<
asp:SqlDataSource
ID
=
"SqlDataSource1"
runat
=
"server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [PickupFiles]"></
asp:SqlDataSource
>
Code Behind (C#)
In order to prevent overwriting files with the same name, save the file with the name containing a timestamp. This can be done using the FileUploaded event of RadAsyncUplaod. The new file name has been saved in a property for future use.
InsertCommand event handler
UpdateCommand event handler
protected
void
RadGrid1_UpdateCommand(
object
sender, GridCommandEventArgs e)
{
// Get reference to the old filePath
string
oldFilePath = Server.MapPath((
string
)(e.Item
as
GridEditableItem).GetDataKeyValue(
"FilePath"
));
if
(!
string
.IsNullOrEmpty(newFileName))
{
string
updateQuery =
"UPDATE PickupFiles SET FilePath=@FilePath, FileName=@FileName WHERE FileID=@FileID"
;
using
(SqlConnection conn =
new
SqlConnection(ConfigurationManager.ConnectionStrings[
"ConnectionString"
].ConnectionString))
{
using
(SqlCommand comm =
new
SqlCommand(updateQuery, conn))
{
string
FullPathToApplication = Request.PhysicalApplicationPath;
string
FullPathToFiles =
"~/"
+ Server.MapPath(
"Files/"
+ newFileName).Replace(FullPathToApplication,
string
.Empty).Replace(
"\\", "
/");
comm.Parameters.Add(
new
SqlParameter(
"FileID"
, (e.Item
as
GridEditableItem).GetDataKeyValue(
"FileID"
)));
comm.Parameters.Add(
new
SqlParameter(
"FilePath"
, FullPathToFiles));
comm.Parameters.Add(
new
SqlParameter(
"FileName"
, newFileName));
conn.Open();
comm.ExecuteNonQuery();
}
}
// If update was successfull, delete the old file
if
(File.Exists(oldFilePath))
File.Delete(oldFilePath);
}
}
DeleteCommand event handler