模板列的用法(既可以用CommandArgument传参数又可以根据条件判断是否显示该列里面的内容)

典型的例子:下载附件列(有附件的显示下载链接,无附件的显示为空)

<asp:TemplateColumn HeaderText="附件">
<HeaderStyle Width="7%"></HeaderStyle>
<ItemTemplate>
<asp:LinkButton id="LinkButton1" CommandName="download" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "attached_file")%>' runat="server" Visible='<%# ((DataBinder.Eval(Container.DataItem, "attached_file").ToString()) != "") %>'>下载</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>

在dgHKStock_ItemCommand事件中:
            if ("download" == e.CommandName)
            {
                mybc.SystemOverTime0();          // 判断Session是否过期
                mybc.RightManage("16010500");    // 判断用户是否有打开此网页的权限

                mybc.HTTP_DownloadFile(e.CommandArgument.ToString());  // 取得当前存货信息的附件存放路径
            }

文件下载函数:
        /// <param name="strDownloadFile">绝对路径</param>
        public void HTTP_DownloadFile(string strDownloadFile)
        {
            // strDownloadFile 是绝对路径,可以通过 Server.MapPath 提供
            string strShortFileName = Path.GetFileNameWithoutExtension(strDownloadFile);
            string strClientFileName = strShortFileName.Substring(0,strShortFileName.Length - 18)
                + Path.GetExtension(strDownloadFile);
            strClientFileName = Server.UrlEncode(strClientFileName);
    
            pge_client.Response.Clear();
            pge_client.Response.ContentType = "application/octet-stream";
            pge_client.Response.AddHeader("Content-Disposition", "attachment;filename=" + strClientFileName);
            pge_client.Response.Flush();
            pge_client.Response.WriteFile(strDownloadFile, false); // 下载文件
            pge_client.Response.End(); // 此句犹为重要,否则会把本页的HTML源码写在文件里
        }

posted @ 2005-05-27 14:21  技术点亮未来  阅读(1146)  评论(0编辑  收藏  举报