does anybody has an example how to add an upload image inside a radgrid in edit and insert template and when edited to save the filename in database and image in a specified folder?
I just installed the trial version and I'm beginner in .NET .. sorry if there was an easy way and i didn't found it ,
thank you
12 Answers, 1 is accepted
You can check this code library. Although it is for the "classic" RadControls for ASP.NET, it can be easily ported for RadControls for ASP.NET AJAX.
All the best,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
but because I'm beginner there's no other easy er way to accomplish this with less code?
Just wondering,
thank you
Teodor
You can use RadUpload's TargetFolder property to set where the files should be uploaded and then you may use UploadedFiles to get the ImageFile's name and update the appropriate record in the database. I have attached a small sample which demonstrates a possible implementation.
Sincerely yours,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
based on your sample i tried to do this:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> |
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
<html xmlns="http://www.w3.org/1999/xhtml"> |
<head runat="server"> |
<title>Untitled Page</title> |
</head> |
<body> |
<form id="form1" runat="server"> |
<div> |
<telerik:RadScriptManager ID="RadScriptManager1" Runat="server"> |
</telerik:RadScriptManager> |
<telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="False" AutoGenerateEditColumn="True" |
DataSourceID="pictures" GridLines="None" AllowAutomaticInserts="True" |
AllowAutomaticUpdates="True"> |
<MasterTableView DataKeyNames="Id" ShowHeadersWhenNoRecords="true" CommandItemDisplay="Top" |
DataSourceID="pictures"> |
<Columns> |
<telerik:GridBoundColumn HeaderText="ID" ReadOnly="true" DataField="Id" /> |
<telerik:GridBoundColumn HeaderText="Brand" DataField="Brand" /> |
<telerik:GridTemplateColumn HeaderText="Logo"> |
<ItemTemplate> |
<asp:Image ID="Image1" ImageUrl='<%#string.Format("~/images/{0}", Eval("Logo")) %>' runat="server" |
Width="100px" Height="100px" /> |
</ItemTemplate> |
<EditItemTemplate> |
<telerik:RadUpload runat="server" ID="RadUpload1" TargetFolder="~/Images" OverwriteExistingFiles="false" |
ControlObjectsVisibility="None" /> |
</EditItemTemplate> |
</telerik:GridTemplateColumn> |
</Columns> |
</MasterTableView> |
</telerik:RadGrid> |
<asp:SqlDataSource ID="pictures" runat="server" |
ConnectionString="<%$ ConnectionStrings:sunglassesConnectionString %>" |
OnInserting="pictures_Inserting" OnUpdating="pictures_Updating"> |
<DeleteParameters> |
<asp:Parameter Name="id" Type="Int32" /> |
</DeleteParameters> |
<UpdateParameters> |
<asp:Parameter Name="brand" Type="String" /> |
<asp:Parameter Name="logo" Type="String" /> |
<asp:Parameter Name="id" Type="Int32" /> |
</UpdateParameters> |
<InsertParameters> |
<asp:Parameter Name="brand" Type="String" /> |
<asp:Parameter Name="logo" Type="String" /> |
</InsertParameters> |
</asp:SqlDataSource> |
</div> |
</form> |
</body> |
</html> |
Imports System |
Imports System.Web.UI |
Imports System.Web.UI.WebControls |
Imports Telerik.Web.UI |
Imports System.Data |
Partial Class _Default |
Inherits System.Web.UI.Page |
Protected Sub pictures_Inserting(ByVal sender As Object, ByVal e As ObjectDataSourceMethodEventArgs) |
e.InputParameters("logo") = ExtractImageName(RadGrid1.MasterTableView.GetInsertItem()) |
End Sub |
Protected Sub pictures_Updating(ByVal sender As Object, ByVal e As ObjectDataSourceMethodEventArgs) |
e.InputParameters("logo") = ExtractImageName(DirectCast(RadGrid1.EditItems(0), GridDataItem).EditFormItem) |
End Sub |
''' <summary> |
''' Extracts uploaded file's name |
''' </summary> |
''' <param name="gridItem">GridItem which holds the RadUpload control</param> |
''' <returns>Name of the file which has been uploaded or empty string otherwise</returns> |
Private Shared Function ExtractImageName(ByVal gridItem As GridItem) As String |
Dim logo As String = [String].Empty |
Dim radUpload = TryCast(gridItem.FindControl("RadUpload1"), RadUpload) |
If radUpload IsNot Nothing AndAlso radUpload.UploadedFiles.Count > 0 Then |
logo = radUpload.UploadedFiles(0).GetName() |
End If |
Return logo |
End Function |
End Class |
and it upload the image but won't add the file name in the db for the logo column just uploaded..
Could you please look into maybe you could solve my problem?
If you see where's the mistake here i won't bother anymore thank you very much
Teodor
When using SqlDataSource control you should specify SelectCommand and in your case also UpdateCommand and InsertCommand.
I have also noticed that SqlDataSource's Updating and Inserting event handlers are not correctly attached (you have AutoEventWireup set to false) nor declared (method signatures are for ObjectDataSource).
I hope this helps.
All the best,
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
<%@ Page Language="VB" AutoEventWireup="True" CodeFile="Default.aspx.vb" Inherits="_Default" %> |
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %> |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
<html xmlns="http://www.w3.org/1999/xhtml"> |
<head runat="server"> |
<title>Untitled Page</title> |
</head> |
<body> |
<form id="form1" runat="server"> |
<div> |
<telerik:RadScriptManager ID="RadScriptManager1" Runat="server"> |
</telerik:RadScriptManager> |
<telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="False" AutoGenerateEditColumn="True" |
DataSourceID="pictures" GridLines="None" AllowAutomaticInserts="True" |
AllowAutomaticUpdates="True"> |
<MasterTableView DataKeyNames="id" ShowHeadersWhenNoRecords="true" CommandItemDisplay="Top" |
DataSourceID="pictures"> |
<Columns> |
<telerik:GridBoundColumn HeaderText="ID" ReadOnly="true" DataField="Id" /> |
<telerik:GridBoundColumn HeaderText="Brand" DataField="Brand" /> |
<telerik:GridTemplateColumn HeaderText="Logo"> |
<ItemTemplate> |
<asp:Image ID="Image1" ImageUrl='<%#string.Format("~/images/{0}", Eval("Logo")) %>' runat="server" |
Width="100px" Height="100px" /> |
</ItemTemplate> |
<EditItemTemplate> |
<telerik:RadUpload runat="server" ID="RadUpload1" TargetFolder="~/Images" OverwriteExistingFiles="false" |
ControlObjectsVisibility="None" /> |
</EditItemTemplate> |
</telerik:GridTemplateColumn> |
</Columns> |
</MasterTableView> |
</telerik:RadGrid> |
<asp:SqlDataSource ID="pictures" runat="server" |
ConnectionString="<%$ ConnectionStrings:sunglassesConnectionString %>" |
DeleteCommand="DELETE FROM [brands] WHERE [id] = @id" |
InsertCommand="INSERT INTO [brands] ([brand], [logo]) VALUES (@brand, @logo)" |
SelectCommand="SELECT * FROM [brands]" |
UpdateCommand="UPDATE [brands] SET [brand] = @brand, [logo] = @logo WHERE [id] = @id"> |
<DeleteParameters> |
<asp:Parameter Name="id" Type="Int32" /> |
</DeleteParameters> |
<UpdateParameters> |
<asp:Parameter Name="brand" Type="String" /> |
<asp:Parameter Name="logo" Type="String" /> |
<asp:Parameter Name="id" Type="Int32" /> |
</UpdateParameters> |
<InsertParameters> |
<asp:Parameter Name="brand" Type="String" /> |
<asp:Parameter Name="logo" Type="String" /> |
</InsertParameters> |
</asp:SqlDataSource> |
</div> |
</form> |
</body> |
</html> |
Imports System |
Imports System.Web.UI |
Imports System.Web.UI.WebControls |
Imports Telerik.Web.UI |
Imports System.Data |
Partial Class _Default |
Inherits System.Web.UI.Page |
Protected Sub pictures_Inserting(ByVal sender As Object, ByVal e As ObjectDataSourceMethodEventArgs) |
e.InputParameters("logo") = ExtractImageName(RadGrid1.MasterTableView.GetInsertItem()) |
End Sub |
Protected Sub pictures_Updating(ByVal sender As Object, ByVal e As ObjectDataSourceMethodEventArgs) |
e.InputParameters("logo") = ExtractImageName(DirectCast(RadGrid1.EditItems(0), GridDataItem).EditFormItem) |
End Sub |
''' <summary> |
''' Extracts uploaded file's name |
''' </summary> |
''' <param name="gridItem">GridItem which holds the RadUpload control</param> |
''' <returns>Name of the file which has been uploaded or empty string otherwise</returns> |
Private Shared Function ExtractImageName(ByVal gridItem As GridItem) As String |
Dim logo As String = [String].Empty |
Dim radUpload = TryCast(gridItem.FindControl("RadUpload1"), RadUpload) |
If radUpload IsNot Nothing AndAlso radUpload.UploadedFiles.Count > 0 Then |
logo = radUpload.UploadedFiles(0).GetName() |
End If |
Return logo |
End Function |
End Class |
If you look at the code, you may noticed that SqlDataSource's Inserting and Updating event handlers are still not attached.
Rosen
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
i suppose is just 2 lines what you should write to attach the handlers .. and i'm sure this will help others ... like me ..
For example:
If I upload an image called test the image file will be called test.jpg and "Name" will get changed to test.jpg instead of test.
I don't want it to be test.jpg in the Name field of the database. JUST test with no extension
Is there a way to implement this? I'm stumped since there is now examples on this.
Thanks.
Bill.
I tried to reproduce the described behavior with the latest version of the RadControls, but to no avail. I am sending you the mentioned example with the latest version of the RadControls. Also on the following link I attached a small video which demonstrates how the application works on my end. Please check it out and let me know if it helps you.
Looking forward for your reply.
Regards,
Radoslav
the Telerik team
I download the example.zip and test it was the closer solution what i need, but the example using the ObjectDataSource, I'll like to using the SqlDataSource, try to modify the code behind just cannot make it, pls help....Thanks
protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
e.Command.Parameters["@Imagename"].Value = ExtractImagename(RadGrid1.MasterTableView.GetInsertItem());
}
protected void SqlDataSource1_Updated(object sender, SqlDataSourceStatusEventArgs e)
{
e.Command.Parameters["@Imagename"].Value = ExtractImagename(((GridDataItem)RadGrid1.EditItems[0]).EditFormItem);
}
private static string ExtractImagename(GridItem gridItem)
{
string Imagename = String.Empty;
var radUpload = (gridItem.FindControl("RadAsyncUpload1") as RadUpload);
if (radUpload != null && radUpload.UploadedFiles.Count > 0)
{
Imagename = radUpload.UploadedFiles[0].GetName();
}
return Imagename;
}
From the provided code snippet I saw that you handle OnInserted and OnUpdated events of an SqlDataSource. However to achieve the desired functionality you need to handle the OnInserting, and OnUpdating events, like into the provided example.
Please give it try and let me know if you experience any problems.
Regards,
Radoslav
Telerik