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

Attachment in Grid

1 Answer 106 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 21 Nov 2013, 02:33 AM
Hi, i want to provide a upload function for user in Grid edit mode and then provide a download link in grid view.

the attachment file will save in another table (different linqdatasource with grid), how can i databind the download link ?  (will use "UserID" as key in both table)

moreover, my upload function not works, it cannot save the bindary file to the table. please help.

<telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0"
DataSourceID="ODS_attendance" width="1200"  PageSize="15"
        GridLines="None" style="margin-top: 0px" AllowFilteringByColumn="True"
        AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False"
        ShowStatusBar="True" AllowAutomaticDeletes="false" AllowAutomaticInserts="false"
AllowAutomaticUpdates="True">
        <GroupingSettings CaseSensitive="false" />
    <MasterTableView AutoGenerateColumns="False"
        DataKeyNames="UserID" DataSourceID="ODS_attendance"
        AllowFilteringByColumn="False" AllowPaging="False"
        CommandItemDisplay="Top" OverrideDataSourceControlSorting="true" >
 
        <CommandItemSettings ExportToPdfText="Export to PDF" ShowAddNewRecordButton="false"  />
        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column"
            Visible="True">
        </RowIndicatorColumn>
        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column"
            Visible="True">
        </ExpandCollapseColumn>
        <Columns>
        <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" HeaderStyle-Width="50">
            </telerik:GridEditCommandColumn>
        <telerik:GridBoundColumn UniqueName="UserID" DataField="UserID" HeaderText="UserID"
                    ReadOnly="True" HeaderStyle-Width="5%" Visible="false">
                </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Employee_No" DataType="System.String"
            FilterControlAltText="Filter Employee column" HeaderText="Employee No" ReadOnly="True"
            SortExpression="Employee_No" UniqueName="Employee_No" >
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Employee_Display_Name" DataType="System.String"
            FilterControlAltText="Filter Employee column" HeaderText="Employee" ReadOnly="True"
            SortExpression="Employee_Display_Name" UniqueName="Employee_Display_Name" >
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Department" DataType="System.String"
            FilterControlAltText="Filter Department column" HeaderText="Reporting office" ReadOnly="True"
            SortExpression="Department" UniqueName="Department" >
        </telerik:GridBoundColumn>                                                                   
        <telerik:GridBoundColumn DataField="Start_Time"
            FilterControlAltText="Filter Start Time column" HeaderText="Start Date"
            SortExpression="Start_Time" UniqueName="Start_Date" DataFormatString="{0: dd-MMM-yyyy}" AllowFiltering="true"  AllowSorting="true">
        </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="End_Time"
            FilterControlAltText="Filter End Time column" HeaderText="End Date" 
            SortExpression="End_Time" UniqueName="End_Date"  DataFormatString="{0: dd-MMM-yyyy}" AllowFiltering="true"  AllowSorting="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Start_Time"
            FilterControlAltText="Filter Start Time column" HeaderText="Start Time" 
            SortExpression="Start_Time" UniqueName="Start_Time" DataFormatString="{0: hh:mm tt}" AllowFiltering="true"  AllowSorting="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="End_Time"
            FilterControlAltText="Filter End Time column" HeaderText="End Time"
            SortExpression="End_Time" UniqueName="End_Time"  DataFormatString="{0: hh:mm tt}" AllowFiltering="true"  AllowSorting="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="TotalDays"
            FilterControlAltText="Filter TotalDays column" HeaderText="Total Day" 
            SortExpression="TotalDays" UniqueName="TotalDays" AllowFiltering="true"  AllowSorting="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="TotalHours"
            FilterControlAltText="Filter TotalDTotalHoursays column" HeaderText="Total Hour" 
            SortExpression="TotalHours" UniqueName="TotalHours" AllowFiltering="true"  AllowSorting="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Type"
            FilterControlAltText="Filter Type column" HeaderText="Type" 
            SortExpression="Type" UniqueName="Type" AllowFiltering="true"  AllowSorting="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="pre_test_score"
            FilterControlAltText="Filter pre_test_score column" HeaderText="Pre test score" 
            SortExpression="pre_test_score" UniqueName="pre_test_score" AllowFiltering="true"  AllowSorting="true">
        </telerik:GridBoundColumn>                                                                   
        <telerik:GridBoundColumn DataField="post_test_score"
            FilterControlAltText="Filter post_test_score column" HeaderText="Post test score" 
            SortExpression="post_test_score" UniqueName="post_test_score" AllowFiltering="true"  AllowSorting="true">
        </telerik:GridBoundColumn>                                                                   
        <telerik:GridBoundColumn DataField="Result"
            FilterControlAltText="Filter Result column" HeaderText="Result" 
            SortExpression="Result" UniqueName="Result" AllowFiltering="true"  AllowSorting="true">
        </telerik:GridBoundColumn>                                                                   
        <telerik:GridBoundColumn DataField="Cost"
            FilterControlAltText="Filter Cost column" HeaderText="Cost" 
            SortExpression="Cost" UniqueName="Cost" AllowFiltering="true"  AllowSorting="true">
        </telerik:GridBoundColumn
        <telerik:GridBoundColumn DataField="Remarks"
            FilterControlAltText="Filter Remarks column" HeaderText="Remarks" 
            SortExpression="Remarks" UniqueName="Remarks" AllowFiltering="true"  AllowSorting="true">
        </telerik:GridBoundColumn
     
 
 
        <telerik:GridTemplateColumn>
            <ItemTemplate>
                <asp:LinkButton ID="DownloadButton" runat="server"
                    CommandName="DownloadAttachment" Text="Download">
                </asp:LinkButton>
            </ItemTemplate>
           <EditItemTemplate>
                <telerik:RadUpload ID="AttachmentUpload" runat="server">
                </telerik:RadUpload>
            </EditItemTemplate>
        </telerik:GridTemplateColumn>
 
        </Columns>
        <EditFormSettings EditFormType="Template">
            <EditColumn FilterControlAltText="Filter EditCommandColumn1 column"
                UniqueName="EditCommandColumn1">
            </EditColumn>
 
        <FormTemplate>
 
        <table id="Table2" cellspacing="2" cellpadding="1" width="50%" border="1" rules="none"
            style="border-collapse: collapse;">
            <tr class="EditFormHeader">
            <td style="width:150px">
            <asp:Label ID="Label3" Text="Employee No" runat="server"></asp:Label>                                                           
            </td>
            <td>
            <asp:label ID="tb_emp_no" runat="server" Text='<%# Bind("Employee_No") %>' ReadOnly="true"></asp:label>                                                           
            </td>
            </tr>
            <tr>
            <td style="width:150px">
            <asp:Label ID="Label2" Text="Employee Name" runat="server"></asp:Label>                                                           
            </td>
            <td>
            <asp:label ID="Label4" runat="server" Text='<%# Bind("Employee_Display_Name") %>' ReadOnly="true"></asp:label>                                                           
            </td>
            </tr>
            <tr>
            <td style="width:150px">
            <asp:Label ID="Label9" Text="Reporting office" runat="server"></asp:Label>                                                           
            </td>
            <td>
            <asp:label ID="Label10" runat="server" Text='<%# Bind("Department") %>' ReadOnly="true"></asp:label>                                                           
            </td>
            </tr>
 
            <tr>
            <td style="width:150px">
            <asp:Label ID="Label5" Text="Start Date & Time" runat="server"></asp:Label>                                                           
            </td>
            <td>
            <asp:label ID="Label6" runat="server" Text='<%# Bind("Start_Time","{0: dd-MMM-yyyy hh:mm tt}") %>' ReadOnly="true"></asp:label>                                                           
            </td>
            </tr>
            <tr>
            <td style="width:150px">
            <asp:Label ID="Label7" Text="End Date & Time" runat="server"></asp:Label>                                                           
            </td>
            <td>
            <asp:label ID="Label8" runat="server" Text='<%# Bind("End_Time","{0: dd-MMM-yyyy hh:mm tt}") %>' ReadOnly="true"></asp:label>                                                           
            </td>
            </tr>
            <tr>
            <td style="width:150px">
            <asp:Label ID="Label19" Text="Total Days" runat="server"></asp:Label>                                                           
            </td>
            <td>
            <asp:label ID="Label20" runat="server" Text='<%# Bind("TotalDays") %>' ReadOnly="true"></asp:label>                                                           
            </td>
            </tr>
            <tr>
            <td style="width:150px">
            <asp:Label ID="Label21" Text="Total Hours" runat="server"></asp:Label>                                                           
            </td>
            <td>
            <asp:label ID="Label22" runat="server" Text='<%# Bind("TotalHours") %>' ReadOnly="true"></asp:label>                                                           
            </td>
            </tr>
            <tr>
            <td style="width:150px">
            <asp:Label ID="Label11" Text="Training Type" runat="server"></asp:Label>                                                           
            </td>
            <td>
            <asp:label ID="Label12" runat="server" Text='<%# Bind("type") %>' ReadOnly="true"></asp:label>                                                           
            </td>
            </tr>
            <tr>
            <td style="width:150px">
            <asp:Label ID="Label13" Text="Pre test score" runat="server"></asp:Label>                                                           
            </td>
            <td>
            <asp:textbox ID="tb_pre_test_score" runat="server" Text='<%# Bind("pre_test_score") %>' ></asp:textbox>                                                           
            </td>
            </tr>
            <tr>
            <td style="width:150px">
            <asp:Label ID="Label15" Text="Post test score" runat="server"></asp:Label>                                                           
            </td>
            <td>
            <asp:textbox ID="tb_post_test_score" runat="server" Text='<%# Bind("post_test_score") %>' ></asp:textbox>                                                           
            </td>
            </tr>
            <tr>
            <td style="width:150px">
            <asp:Label ID="Label18" Text="Result" runat="server"></asp:Label>                                                           
            </td>
            <td>
                <asp:DropDownList runat="Server" SelectedValue='<%# Bind("Result") %>'
                     DataTextField="Result" DataValueField="Result" 
                    ID="comboResult">
                    <asp:ListItem Text="" Value=""></asp:ListItem>
                    <asp:ListItem Text="Pass" Value="Pass"></asp:ListItem>
                    <asp:ListItem Text="Fail" Value="Fail"></asp:ListItem>
                    <asp:ListItem Text="Defer" Value="Defer"></asp:ListItem>
                </asp:DropDownList>                                                       
            </td>
            </tr>
            <tr>
            <td style="width:150px">
            <asp:Label ID="Label16" Text="Cost" runat="server"></asp:Label>                                                           
            </td>
            <td>
            <asp:textbox ID="tb_cost" runat="server" Text='<%# Bind("cost") %>' ></asp:textbox>                                                           
            </td>
            </tr>
            <tr>
            <td style="width:150px">
            <asp:Label ID="Label17" Text="Remarks" runat="server"></asp:Label>                                                           
            </td>
            <td>
            <asp:textbox ID="tb_remarks" runat="server" Text='<%# Bind("remarks") %>' ></asp:textbox>                                                           
            </td>
            </tr>
            <tr>
            <td>
            <asp:Label ID="Label14" Text="Certificate" runat="server"></asp:Label>  
            </td>
            <td>
             
                <telerik:RadUpload ID="AttachmentUpload" runat="server">
                </telerik:RadUpload>
             
            </td>
            </tr>
 
 
               <tr>
                <td align="right" colspan="2">
                   <asp:Button ID="Button1" Text="Update"
                        runat="server" CommandName="Update"></asp:Button
                    <asp:Button ID="btnDelete" Text="Delete" runat="server" CausesValidation="False"
                        CommandName="Delete" OnClientClick="return conformbox();" visible="false"  ></asp:Button>
                    <asp:Button ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                        CommandName="Cancel"></asp:Button>
                </td>
            </tr>
        </table>
    </FormTemplate>
</EditFormSettings>
        <PagerStyle PageSizeControlType="RadComboBox" />
    </MasterTableView>
 
    <PagerStyle PageSizeControlType="RadComboBox" />
    <FilterMenu EnableImageSprites="False">
    </FilterMenu>
 
</telerik:RadGrid>
                                                         
<asp:ObjectDataSource ID="ODS_attendance" runat="server"
                   SelectMethod="GetTrainingRecordsArrayBySessionID" TypeName="LRDB">
                   <SelectParameters>
                       <asp:QueryStringParameter Name="id" QueryStringField="SessionID" Type="Int32" />
                   </SelectParameters>
                   <UpdateParameters>
                    <asp:Parameter Name="UserID" Type="Int16"></asp:Parameter>
                        <asp:Parameter Name="Remarks" Type="String" />
                        <%--<asp:Parameter Name="Status" Type="Boolean" />--%>
                   </UpdateParameters>
</asp:ObjectDataSource>
<asp:LinqDataSource ID="LDS_Certificate" runat="server" TableName="db_Certificates"
               ContextTypeName="dcLRDBDataContext" EntityTypeName="" Where="UserID==@UserID">
                       <SelectParameters>
                            <asp:Parameter Name="UserID" Type="Int32"></asp:Parameter>
                        </SelectParameters>
</asp:LinqDataSource>

Code behind:

Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand
        RadGrid1.MasterTableView.ClearEditItems()
 
 
        If e.CommandName = RadGrid.InitInsertCommandName Then   'Click Add New Record button           
            Dim editColumn As GridEditCommandColumn = CType(RadGrid1.MasterTableView.GetColumn("EditCommandColumn"), GridEditCommandColumn)
            editColumn.Visible = False
        ElseIf (e.CommandName = RadGrid.RebindGridCommandName AndAlso e.Item.OwnerTableView.IsItemInserted) Then
            e.Canceled = True
        Else
            Dim editColumn As GridEditCommandColumn = CType(RadGrid1.MasterTableView.GetColumn("EditCommandColumn"), GridEditCommandColumn)
            If Not editColumn.Visible Then  'Click Cancel button (both Insert and Update mode)
                editColumn.Visible = True
            End If
 
            Dim txt_pre_test_score As TextBox = DirectCast(e.Item.FindControl("tb_pre_test_score"), TextBox)
            Dim txt_post_test_score As TextBox = DirectCast(e.Item.FindControl("tb_post_test_score"), TextBox)
            Dim txt_cost As TextBox = DirectCast(e.Item.FindControl("tb_cost"), TextBox)
            Dim txt_remarks As TextBox = DirectCast(e.Item.FindControl("tb_remarks"), TextBox)
            Dim cmb_Result As DropDownList = DirectCast(e.Item.FindControl("comboResult"), DropDownList)
 
            Dim dc As New dcLRDBDataContext
 
            If e.CommandName = "PerformInsert" Then
 
 
            ElseIf e.CommandName = "Update" Then
                Dim lid As Integer = e.Item.OwnerTableView.DataKeyValues(e.Item.ItemIndex)("UserID")
 
                HttpContext.Current.Session("EditUserID") = lid
 
 
                Dim rec = (From p In dc.db_Training_Records Where p.UserID = lid).FirstOrDefault
                If Not rec Is Nothing Then
                    rec.Cost = txt_cost.Text
                    rec.pre_test_score = txt_pre_test_score.Text
                    rec.post_test_score = txt_post_test_score.Text
                    rec.Remarks = txt_remarks.Text
                    rec.Result = cmb_Result.SelectedValue
 
                    rec.LastUpdateBy = HttpContext.Current.Session("UserID")
                    rec.LastUpdateDate = DateTime.Now
                    dc.SubmitChanges()
                End If
 
                Dim attachmentUpload As RadUpload = DirectCast(e.Item.FindControl("AttachmentUpload"), RadUpload)
 
                If attachmentUpload.UploadedFiles.Count > 0 Then
 
                    Dim file As UploadedFile = attachmentUpload.UploadedFiles(0)
                    Dim stream As System.IO.Stream = file.InputStream
                    Dim binaryData As Byte() = New Byte(stream.Length - 1) {}
                    stream.Read(binaryData, 0, CInt(stream.Length))
                    'binaryData contains your uploaded content
                    'you can save to your database
                    stream.Seek(0, System.IO.SeekOrigin.Begin)
 
                    Dim newRecord1 As New db_Certificate
                    dc.db_Certificates.InsertOnSubmit(newRecord1)
                    newRecord1.BinaryData = binaryData
                    newRecord1.FileName = file.FileName
                    newRecord1.FileSize = file.ContentLength / 1024
                    newRecord1.Description = file.GetFieldValue("Description")
                    newRecord1.UploadBy = HttpContext.Current.Session("UserID")
                    newRecord1.UploadDate = DateTime.Now
                    newRecord1.LastUpdatedOn = DateTime.Now
                    newRecord1.LastUpdatedBy = HttpContext.Current.Session("UserID")
                    newRecord1.UserID = lid
 
                    dc.SubmitChanges()
                End If
 
                RadGrid1.MasterTableView.ClearEditItems()
                RadGrid1.DataBind()
 
            End If
 
            dc.Dispose()
        End If
    End Sub



1 Answer, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 25 Nov 2013, 09:33 AM
Hi Joe,

I would recommend you to check out the following live example which illustrates the usage of RadAsyncUpload and BinaryImage and how to display and edit images. Please give it a try and let me know about the result.

Regards,
Kostadin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Joe
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Share this question
or