locked
Get item index from data list RRS feed

  • Question

  • User639567535 posted

    <div class="post-text" itemprop="text">

    I try to download files from data list .

    Datalist html is

    <asp:DataList ID="dl_Options" runat="server" Width="100%" DataKeyField="HistoryIndex" BorderColor="Black" BorderStyle="Solid" Font-Bold="False" BackColor="White" BorderWidth="0px">
    <ItemTemplate>
     <table class="colordtd">
     <tr>
    <td width="100px">
    <asp:Label ID="docid" runat="server" Text='<%# Eval("docid")%>' /></td>
     <td width="100px">
     <asp:LinkButton ID="lnkDownload" runat="server" CausesValidation="False"
      CommandArgument='<%# Eval("FileName")%>'    OnClick="DownloadFile"
      CommandName="Download" Text='<%# Eval("FileName")%>' /> 
      </tr>
     </table>
     </ItemTemplate>
     </asp:DataList>

    files

    1014 abc.docx
    1015 dfe.pdf

    when i click on abc.docx then file name change to 234394qws.docx

    so how i get docid and how to download file from datalist

    Wednesday, January 18, 2017 10:00 AM

Answers

  • User-707554951 posted

    Hi Bakhtawar Ashiq,

    > i want to get docuid from datalist

    Please try to get the NamingContainer of the linkbutton then find the docid control and get the text.

      Dim label As Label = sender.NamingContainer.FindControl("docid")
      Dim itemIndex As Integer = label.Text

    > i hard code id and try to download file then file is download but file name change

    Because you have set the file name to:

    DocumentName = Now.Day & Now.Month & Now.Year * Now.Hour & Now.Minute & Now.Millisecond & DocExtension

    If you want the file name same as the format you provide, you could set the name to:

    DocumentName = itemIndex & " " & sender.Text & DocExtension

    Here is the complete code:

    Protected Sub DownloadFile(sender As Object, e As EventArgs)
            Dim strKeyName() As String = {"docid "}
    
            Dim label As Label = sender.NamingContainer.FindControl("docid")
    
            Dim itemIndex As Integer = label.Text
            Dim objKeyVal() As Object = {itemIndex}
            Dim DocExtension As String = ""
            Dim DocumentName As String = ""
            structDb = objDataSet.ExecSP("tbl", "getdoc", strKeyName, objKeyVal)
            If structDb.intCode = 0 Then
                Return
            Else
                Dim count As Integer = dl_Options.Items.Count
                For i As Integer = 0 To count - 1
                    Dim binary() As Byte = TryCast(structDb.dstResult.Tables(0).Rows(i).Item("document"), Byte())
                    Dim ms As MemoryStream = New MemoryStream(binary)
                    DocExtension = structDb.dstResult.Tables(0).Rows(i).Item("DocExtension")
                    DocumentName = itemIndex & " " & sender.Text & DocExtension
                    Response.Clear()
                    Response.Buffer = True
                    Response.Charset = ""
                    Response.Cache.SetCacheability(HttpCacheability.NoCache)
                    Response.ContentType = ContentType
                    Response.AppendHeader("Content-Disposition", Convert.ToString("attachment;filename=") & DocumentName)
                    Response.BinaryWrite(binary)
                    Response.Flush()
                    Response.[End]()
                Next
            End If
    
        End Sub
    

    Best Regards

    Cathy

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 19, 2017 6:18 AM