关于使用DataGrid的ButtonColumn,动态添加DataGrid列,实现不定列n个文件的下载功能...
一、使用DataGrid的ButtonColumn,動(dòng)態(tài)添加DataGrid列,實(shí)現(xiàn)不定列n個(gè)文件的下載功能
在aspx頁(yè)面中的代碼如下:
<asp:datagrid id="UserInfoList" runat="server" CellPadding="0" CellSpacing="1" Width="90%" HorizontalAlign="Center" BackColor="#E1E1E1" BorderWidth="0px" AutoGenerateColumns="False" DataKeyField="UserID" AllowPaging="True" OnPageIndexChanged="page_change">
?<AlternatingItemStyle HorizontalAlign="Center" Height="30px" VerticalAlign="Middle" BackColor="#EEF1F7"></AlternatingItemStyle>
?<ItemStyle HorizontalAlign="Center" Height="30px" VerticalAlign="Middle" BackColor="#F8F8F8"></ItemStyle>
?<HeaderStyle Font-Bold="True" HorizontalAlign="Center" Height="30px" ForeColor="#000099" BackColor="#FFFFEE"></HeaderStyle>
?<Columns>
??<asp:TemplateColumn HeaderText="LoginName">
???<HeaderStyle Width="20%"></HeaderStyle>
???<ItemStyle HorizontalAlign="Center" Height="30px"></ItemStyle>
???<ItemTemplate>
????<%# databinder.eval(container.dataitem,"LoginName")%>
???</ItemTemplate>
??</asp:TemplateColumn>
??<asp:BoundColumn Visible="False" DataField="FileName" ReadOnly="True"></asp:BoundColumn>
??<asp:TemplateColumn HeaderText="LoginPassword">
???<HeaderStyle Width="20%"></HeaderStyle>
???<ItemStyle HorizontalAlign="Center"></ItemStyle>
???<ItemTemplate>
????<%# databinder.eval(container.dataitem,"LoginPassword")%>
???</ItemTemplate>
??</asp:TemplateColumn>
??<asp:TemplateColumn HeaderText="UserName">
???<HeaderStyle Width="20%"></HeaderStyle>
???<ItemStyle HorizontalAlign="Center"></ItemStyle>
???<ItemTemplate>
????<%# databinder.eval(container.dataitem,"UserName")%>
???</ItemTemplate>
??</asp:TemplateColumn>
?</Columns>
?<PagerStyle Height="30px" Font-Bold="True" HorizontalAlign="Right" BackColor="#FFFFEE" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
前臺(tái)頁(yè)面只是一般的DataGrid定義,定義了LoginName、LoginPassword和UserName3列,以及FileName隱藏列。
在aspx.vb后臺(tái)程序中的主要代碼如下:
注意在DataGrid中使用ButtonColumn,動(dòng)態(tài)添加DataGrid列,在DataGrid中使用ItemCommand事件實(shí)現(xiàn)此功能
''' -----------------------------------------------------------------------------
''' <summary>
''' 在Datagrid中動(dòng)態(tài)創(chuàng)建5列,使用ButtonColumn來實(shí)現(xiàn)LinkButton
''' </summary>
''' <remarks>
''' 這個(gè)方法在Page_Init過程中調(diào)用,Page_Init過程一般情況下是隱藏在#Region " Web 窗體設(shè)計(jì)器生成的代碼 中,點(diǎn)開#Region " Web 窗體設(shè)計(jì)器生成的代碼 "前面的+號(hào),就可以看到Page_Init過程的代碼。
''' </remarks>
''' <history>
''' ?[nijun]?2006-1-16?Created
''' </history>
''' -----------------------------------------------------------------------------
Private Sub InitAddDataGridColumn()
??? Dim MyBtnCol As ButtonColumn
??? Dim loopColumn As Integer
??? Dim temp As ITemplate
??? For loopColumn = 1 To 5
????????????MyBtnCol = New ButtonColumn
????????????'定義ButtonColumn的ButtonType為L(zhǎng)inkButton
??????????? MyBtnCol.ButtonType = ButtonColumnType.LinkButton
??????????? MyBtnCol.HeaderText = "File" & loopColumn.ToString
??????????? MyBtnCol.ItemStyle.HorizontalAlign = HorizontalAlign.Center
????????????'定義ButtonColumn的CommandName
??????????? MyBtnCol.CommandName = "FileDownload" & loopColumn.ToString
????????????'定義ButtonColumn的DataField綁定字段名
??????????? MyBtnCol.DataTextField = "FileName" & loopColumn.ToString
????????????'將ButtonColumn列添加到DataGrid中
??????????? UserInfoList.Columns.Add(MyBtnCol)
??? Next
End Sub
'在Page_Init過程中的調(diào)用代碼如下:
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
??????? 'CODEGEN: 此方法調(diào)用是 Web 窗體設(shè)計(jì)器所必需的
??????? '不要使用代碼編輯器修改它。
??????? InitializeComponent()
????????'調(diào)用動(dòng)態(tài)添加列過程
??????? Call InitAddDataGridColumn()
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 點(diǎn)擊每行的download the file鏈接,實(shí)現(xiàn)文件下載
''' </summary>
''' <param name="source"></param>
''' <param name="e"></param>
''' <remarks>
''' </remarks>
''' <history>
''' ?[nijun]?2006-1-13?Created
''' </history>
''' -----------------------------------------------------------------------------
Private Sub UserInfoList_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles UserInfoList.ItemCommand
??????? Dim FileName As String
??????? Dim fileStream As FileStream
??????? Dim fileSize As Long
??????? Dim inta As Integer
??????? Dim OutPutFileName As String
??????? Dim loopFileColumn As Integer
??????? Dim BtnCommandName As String
??????? '判斷是點(diǎn)擊download the file時(shí)才觸發(fā)下載文件程序
??????? For loopFileColumn = 1 To 5
??????????? BtnCommandName = "FileDownload" & loopFileColumn
??????????? If (e.CommandName = BtnCommandName) Then
??????????????? '下載的文件名(包括路徑)
??????????????? 'CType(e.Item.Cells(3 + loopFileColumn).Controls(0), LinkButton).Text為獲得該LinkButton的Text的值(Text的值為文件名)
??????????????? FileName = Server.MapPath("") & "\" & CType(e.Item.Cells(3 + loopFileColumn).Controls(0), LinkButton).Text
??????????????? fileStream = New FileStream(FileName, FileMode.Open)
??????????????? fileSize = fileStream.Length
??????????????? inta = CInt(fileSize)
??????????????? '客戶端下載時(shí)顯示的文件名
??????????????? OutPutFileName = "Outputfile.xls"
??????????????? Context.Response.ContentType = "application/octet-stream"
??????????????? '注意后面必須后System.Text.Encoding.UTF8進(jìn)行編碼
??????????????? Context.Response.AddHeader("Content-Disposition", "attachment; filename=" & HttpUtility.UrlEncode(OutPutFileName, System.Text.Encoding.UTF8))
??????????????? Context.Response.AddHeader("Content-Length", fileSize.ToString())
??????????????? Dim fileBuffer(inta) As Byte
??????????????? fileStream.Read(fileBuffer, 0, inta)
??????????????? fileStream.Close()
??????????????? Context.Response.BinaryWrite(fileBuffer)
??????????????? Context.Response.End()
??????????? End If
??????? Next
End Sub
程序文件的下載地址:http://files.cnblogs.com/tawny/NETExample20060116.rar
將rar中的目錄和文件copy到NETExample目錄下,在Microsoft Visual Studio .NET 2003編輯器中將目錄和文件包括到解決方案中,然后就可以運(yùn)行了。
二、關(guān)于Dailog的問題
調(diào)用window.open("test.aspx#goto?id=01")的話,子窗口的.vb取不到id的值的問題。
答:不是window.open("test.aspx#goto?id=01"),正確的寫法應(yīng)該是window.open("test.aspx?id=01#goto")
?
轉(zhuǎn)載于:https://www.cnblogs.com/tawny/archive/2006/01/16/318147.html
總結(jié)
以上是生活随笔為你收集整理的关于使用DataGrid的ButtonColumn,动态添加DataGrid列,实现不定列n个文件的下载功能...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .NET新手系列(五)
- 下一篇: Enterprise Library学习