一、使用DataGrid的ButtonColumn,动态添加DataGrid列,实现不定列n个文件的下载功能
在aspx页面中的代码如下:
<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>
前台页面只是一般的DataGrid定义,定义了LoginName、LoginPassword和UserName3列,以及FileName隐藏列。
在aspx.vb后台程序中的主要代码如下:
注意在DataGrid中使用ButtonColumn,动态添加DataGrid列,在DataGrid中使用ItemCommand事件实现此功能
''' -----------------------------------------------------------------------------
''' <summary>
''' 在Datagrid中动态创建5列,使用ButtonColumn来实现LinkButton
''' </summary>
''' <remarks>
''' 这个方法在Page_Init过程中调用,Page_Init过程一般情况下是隐藏在#Region " Web 窗体设计器生成的代码 中,点开#Region " Web 窗体设计器生成的代码 "前面的+号,就可以看到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为LinkButton
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过程中的调用代码如下:
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
'调用动态添加列过程
Call InitAddDataGridColumn()
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 点击每行的download the file链接,实现文件下载
''' </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
'判断是点击download the file时才触发下载文件程序
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)
'客户端下载时显示的文件名
OutPutFileName = "Outputfile.xls"
Context.Response.ContentType = "application/octet-stream"
'注意后面必须后System.Text.Encoding.UTF8进行编码
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
程序文件的下载地址:https://files.cnblogs.com/tawny/NETExample20060116.rar
将rar中的目录和文件copy到NETExample目录下,在Microsoft Visual Studio .NET 2003编辑器中将目录和文件包括到解决方案中,然后就可以运行了。
二、关于Dailog的问题
调用window.open("test.aspx#goto?id=01")的话,子窗口的.vb取不到id的值的问题。
答:不是window.open("test.aspx#goto?id=01"),正确的写法应该是window.open("test.aspx?id=01#goto")