I noticed in the Grid / Attachment Column demo from here - http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/gridattachmentcolumn/defaultcs.aspx - that if I select the 'Edit' link, it requires me to attach the original file again. But what if I only needed to update the other columns and not the actual file? Is there a way to do this?
Many thanks
11 Answers, 1 is accepted
The field for the upload is empty because the path to the previously uploaded file from the client machine is not known during edit and cannot be automatically set in the field. However you can modify the example to avoid update of the download field if a new file is not entered. This way you can modify and update the other fields without necessarily specifying a file path and another file will be uploaded only when a new file is specified. Here is a code snippet on the needed modifications:
protected
void
RadGrid1_ItemCommand(
object
source, GridCommandEventArgs e)
{
//...
if
(e.CommandName == RadGrid.UpdateCommandName ||
e.CommandName == RadGrid.PerformInsertCommandName)
{
GridEditableItem item = e.Item
as
GridEditableItem;
if
(!(item
is
GridEditFormInsertItem))
{
fileId = (
int
)item.GetDataKeyValue(
"ID"
);
}
string
fileName = (item.EditManager.GetColumnEditor(
"FileName"
)
as
GridTextBoxColumnEditor).Text;
fileData = (item.EditManager.GetColumnEditor(
"AttachmentColumn"
)
as
GridAttachmentColumnEditor).UploadedFileContent;
//if (fileData.Length == 0 || fileName.Trim() == string.Empty)
//{
// e.Canceled = true;
// RadGrid1.Controls.Add(new LiteralControl("<b style='color:red;'>No file uploaded. Action canceled.</b>"));
//}
}
}
protected
void
SqlDataSource1_Updated(
object
sender, SqlDataSourceStatusEventArgs e)
{
if
(fileData.Length > 0 || fileId>0)
{
UpdateInsertFileData(
"UPDATE [FileData] SET [BinaryData] = @BinaryData WHERE [ID] = @ID"
, fileId, fileData);
}
}
Regards,
Marin
the Telerik team
But my problem is when I update a field without uploading a new file, I get the message that the file can not be read when I click on the link to open it after the update.
Create new record works, but not to update without having to upload a new file.
You should also make sure you have modified the Update command as shown in the SqlDataSource1_Updated event, this way after update the SqlDataSource will not modify the data in the column that holds the file, and it will still be available after you click the link.
To resolve the issue you simple need to ensure that the update command of the SqlDatasource does not modify the contents of the field holding the file:
Protected
Sub
SqlDataSource1_Updated(sender
As
Object
, e
As
SqlDataSourceStatusEventArgs)
If
fileData.Length > 0
OrElse
fileId > 0
Then
UpdateInsertFileData(
"UPDATE [FileData] SET [BinaryData] = @BinaryData WHERE [ID] = @ID"
, fileId, fileData)
End
If
End
Sub
Regards,
Marin
Telerik
Private fileId As Integer
Private fileData As Byte()
Protected Sub SqlDataSource1_Updated(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs)
If fileData.Length > 0 OrElse fileId > 0 Then
UpdateInsertFileData("UPDATE [rapp_FileData] SET [BinaryData] = @BinaryData WHERE [ID] = @ID", fileId, fileData)
End If
End Sub
problem with fileData......
fileData is the third argument of the UpdateInsertFileData function and second parameter in the SQL query that will actually perform the update of the database. Depending on the custom logic in the procedure you may wish to use this argument or not if the user has not uploaded a new file and you do not wish to update the corresponding BinaryData field with new values.
Private
Function
UpdateInsertFileData(command
As
String
, fileId
As
Integer
, fileData
As
Byte
())
As
Integer
Using conn
As
New
SqlConnection(connStr)
Using comm
As
New
SqlCommand(command, conn)
comm.Parameters.Add(
New
SqlParameter(
"ID"
, fileId))
If
fileData.Length > 0
Then
'if the user has not attached a new file the SQL query will not have a BianryData parameter and it will not change the content of the uploaded file
comm.Parameters.Add(
New
SqlParameter(
"BinaryData"
, fileData))
End
If
conn.Open()
Return
comm.ExecuteNonQuery()
End
Using
End
Using
End
Function
Marin
Telerik
Still a small problem remains. When I save the edited form not collapse, its open. But the update was performed.
But I have to click cancel to collapse
This my code:
Imports Telerik.Web.UI
Partial Class admin_rapportartikel
Inherits System.Web.UI.Page
Private fileId As Integer
Private fileData As Byte()
'........................................
Protected Sub SqlDataSource1_Inserted(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs)
fileId = CType(e.Command.Parameters("@InsertedID").Value, Integer)
UpdateInsertFileData("INSERT INTO [test_FileData] ([ID], [BinaryData]) VALUES (@ID, @BinaryData)", fileId, fileData)
End Sub
'....................................
Protected Sub SqlDataSource1_Updated(ByVal sender As Object, ByVal e As SqlDataSourceStatusEventArgs)
'If fileData.Length > 0 OrElse fileId > 0 Then
UpdateInsertFileData("UPDATE [test_FileData] SET [BinaryData] = @BinaryData WHERE [ID] = @ID", fileId, fileData)
'End If
End Sub
'....................................
Protected Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs)
If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
Dim editItem As GridEditableItem = TryCast(e.Item, GridEditableItem)
Dim txtId As TextBox = DirectCast(editItem("ID").Controls(0), TextBox)
txtId.Visible = False
End If
If (TypeOf e.Item Is GridEditableItem) AndAlso (e.Item.IsInEditMode) Then
Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
Dim txtReportYear As TextBox = DirectCast(item("ReportYear").Controls(0), TextBox)
txtReportYear.Width = Unit.Pixel(180)
End If
If (TypeOf e.Item Is GridEditableItem) AndAlso (e.Item.IsInEditMode) Then
Dim item As GridEditableItem = DirectCast(e.Item, GridEditableItem)
Dim txtFileName As TextBox = DirectCast(item("FileName").Controls(0), TextBox)
txtFileName.Width = Unit.Pixel(180)
End If
If TypeOf e.Item Is GridEditableItem AndAlso TryCast(e.Item, GridEditableItem).IsInEditMode Then
Dim edititem As GridEditableItem = TryCast(e.Item, GridEditableItem)
Dim ddl As RadComboBox = DirectCast(edititem("GridDropDownColumn").Controls(0), RadComboBox)
ddl.Width = Unit.Pixel(180)
End If
End Sub
'....................................
Protected Sub RadGrid1_ItemCreated(ByVal sender As Object, ByVal e As GridItemEventArgs)
If TypeOf e.Item Is GridEditableItem AndAlso e.Item.IsInEditMode Then
Dim item As GridEditableItem = TryCast(e.Item, GridEditableItem)
Dim upload As RadUpload = (TryCast(item.EditManager.GetColumnEditor("AttachmentColumn"), GridAttachmentColumnEditor)).RadUploadControl
upload.OnClientFileSelected = "uploadFileSelected"
End If
End Sub
'....................................
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
If e.CommandName = RadGrid.DownloadAttachmentCommandName Then
Dim args As GridDownloadAttachmentCommandEventArgs = TryCast(e, GridDownloadAttachmentCommandEventArgs)
Dim fileName As String = args.FileName
Dim attachmentId As Integer = DirectCast(args.AttachmentKeyValues("ID"), Integer)
Dim MyConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("JunisConnectionString").ConnectionString)
Dim comm As New SqlCommand("SELECT [BinaryData] FROM [test_FileData] WHERE [ID] = @ID", MyConnection)
comm.Parameters.Add(New SqlParameter("@ID", attachmentId))
Dim adapter As New SqlDataAdapter(comm)
Dim data As New DataSet()
adapter.Fill(data)
Dim binaryData As Byte() = DirectCast(data.Tables(0).Rows(0)("BinaryData"), Byte())
Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("content-disposition", "attachment; filename=" + fileName)
Response.BinaryWrite(binaryData)
Response.[End]()
End If
'....................................
If e.CommandName = RadGrid.UpdateCommandName OrElse e.CommandName = RadGrid.PerformInsertCommandName Then
Dim item As GridEditableItem = TryCast(e.Item, GridEditableItem)
If Not (TypeOf item Is GridEditFormInsertItem) Then
fileId = DirectCast(item.GetDataKeyValue("ID"), Integer)
End If
Dim fileName As String = (TryCast(item.EditManager.GetColumnEditor("FileName"), GridTextBoxColumnEditor)).Text
fileData = (TryCast(item.EditManager.GetColumnEditor("AttachmentColumn"), GridAttachmentColumnEditor)).UploadedFileContent
Dim ReportYear As String = (TryCast(item.EditManager.GetColumnEditor("ReportYear"), GridTextBoxColumnEditor)).TextBoxControl.Text
'If fileData.Length = 0 OrElse fileName.Trim() = String.Empty Then
'e.Canceled = True
'RadGrid1.Controls.Add(New LiteralControl("<
b
style
=
'color:red;'
>Ingen fil vald. Uppladdning avbröts..</
b
>"))
'End If
End If
End Sub
'....................................
Private Function UpdateInsertFileData(command As String, fileId As Integer, fileData As Byte()) As Integer
Using MyConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("JunisConnectionString").ConnectionString)
Using comm As New SqlCommand(command, MyConnection)
comm.Parameters.Add(New SqlParameter("ID", fileId))
If fileData.Length > 0 Then
comm.Parameters.Add(New SqlParameter("BinaryData", fileData))
End If
MyConnection.Open()
Return comm.ExecuteNonQuery()
End Using
End Using
End Function
'....................................
Protected Sub Logout_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
Dim CommandName As String = CType(e, System.Web.UI.WebControls.CommandEventArgs).CommandName.ToString()
If CommandName = "Logout" Then
FormsAuthentication.SignOut()
Response.Write("<
script
>window.open('../default.aspx','_top');<" & Chr(47) & "script>")
End If
End Sub
End Class
You can try disabling the automatic operations for the grid to see if this helps:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowAutomaticUpdates="false" AllowAutomaticInserts="false"
Marin
Telerik
But the database is not update with insert or update.....
When using automatic operations in the grid (AllowAutomaticUpdates="true"), the grid automatically calls the update query / method of the relevant datasource control and closes the edit form.
The other option is manually handling the update / insert operations of the control (in this case AllowAutomaticUpdates / Inserts = "false") then in the grid's Update / Insert command event you perform the operation to the database and rebind the control (RadGrid1.Rebind()) - this will also close the edit form.
The above are the two recommended ways to save grid's data to the database and have the edit form properly closed.
Marin
Telerik