doc文件没有从用户控件在asp.net中下载 [英] Doc file does not download from UserControl in asp.net

查看:78
本文介绍了doc文件没有从用户控件在asp.net中下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含与候选数据网格用户控件。有一列候选名模板字段链接按钮。我附上一个rowcommand事件上我下载一个word文件。我有下载doc文件code从简单的网页下载我的doc文件,但这个code未工作的用户控件。任何一个可以帮我出这个问题。它给人错误响应不可用

 < ASP:GridView控件ID =grdCandidate=服务器的AutoGenerateColumns =假
       OnRowDataBound =grdCandidate_RowDataBound
       onrowcommand =grdCandidate_RowCommand>
          <柱体和GT;
             < ASP:BoundField的数据字段=考生ID的HeaderText =考生ID/>
                 < ASP:的TemplateField>
                      <&HeaderTemplate中GT;
                            候选人名字
                      < / HeaderTemplate中>
                  <&ItemTemplate中GT;
                            < ASP:LinkBut​​ton的ID =lnkResume的CommandName =下载CommandArgument ='<%#的eval(考生ID)%>'
                                =服务器文本='<%#的eval(候选人姓名)%>'工具提示='<%#下载简历 - +的eval(候选人姓名)%>'>< / ASP:LinkBut​​ton的>
                  < / ItemTemplate中>
                < / ASP:的TemplateField>
             < /专栏>
 < / ASP:GridView的>保护无效grdCandidate_RowCommand(对象发件人,GridViewCommandEventArgs E)
{
    尝试
    {
        如果(e.CommandName ==下载)
        {
            字节]附件= NULL;
            字符串扩展=的String.Empty;
            字符串简历=恢复;
            ClsCandidateManager objCandidateManager =新ClsCandidateManager();
            ClsSecureManager objSecureManager =新ClsSecureManager();
            附件= objCandidateManager.GetCandidateAttachment(Convert.ToInt32(e.CommandArgument),出扩展);
            如果(附件= NULL&放大器;!&安培; Attachment.Length大于0)
            {
                尝试
                {
                    Response.Clear();
                    将Response.Buffer =真;
                    Response.Charset的=;
                    如果(扩展==.PDF)
                    {
                        Response.ContentType =应用程序/ PDF
                    }
                    其他
                    {
                        Response.ContentType =应用程序/ VSD-MSWORD
                    }
                    Response.AddHeader(内容处置,附件;文件名=+恢复+扩展);                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.BinaryWrite(附件);
                    Response.Flush();
                    到Response.End();
                }
                赶上(异常前)
                {
                    字符串str = ex.Message + ex.InnerException;
                }
            }
            其他
            {
                //ClientScript.RegisterStartupScript(typeof(Page),SymbolError,<脚本类型=文/ JavaScript的'>警报('简历是没有上传!');< / SCRIPT>中);
            }
        }
    }
    赶上(异常前)
    {
        字符串str = ex.Message + ex.InnerException;    }


解决方案

,如下图所示使用的UpdatePanel,

 < ASP:的UpdatePanel ID =UpdatePanel1=服务器>
                        <&的ContentTemplate GT;
                            < ASP:LinkBut​​ton的ID =lnkDownload=服务器文本=查看的OnClick =lnkDownload_Click
                                CommandArgument ='<%#的eval(ID)%GT;'>< / ASP:LinkBut​​ton的>
                        < /&的ContentTemplate GT;
                        <&触发器GT;
                            < ASP:PostBackTrigger控件ID =lnkDownload/>
                        < /触发器>
                    < / ASP:的UpdatePanel>

I have user control which contain a grid with candidate data. There a columns candidate name with template field link button. I have attached a rowcommand event on which I am downloading a word file. I have download doc file code which download my doc file from simple web page but this code is not working on user control. Can any one help me to out this problem. its giving the error response is not available

  <asp:GridView ID="grdCandidate" runat="server" AutoGenerateColumns="false" 
       OnRowDataBound="grdCandidate_RowDataBound" 
       onrowcommand="grdCandidate_RowCommand">
          <Columns>
             <asp:BoundField DataField="Candidate ID" HeaderText="Candidate ID" />
                 <asp:TemplateField>
                      <HeaderTemplate>
                            Candidate Name
                      </HeaderTemplate>
                  <ItemTemplate>
                            <asp:LinkButton ID="lnkResume" CommandName="Download" CommandArgument='<%#Eval("Candidate ID") %>'
                                runat="server" Text='<%#Eval("Candidate Name") %>' ToolTip='<%# "Download Resume - " + Eval("Candidate Name") %>'></asp:LinkButton>
                  </ItemTemplate>
                </asp:TemplateField>
             </Columns>
 </asp:GridView>

protected void grdCandidate_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        if (e.CommandName == "Download")
        {
            byte[] Attachment = null;
            string Extension = string.Empty;
            string Resume = "Resume";
            ClsCandidateManager objCandidateManager = new ClsCandidateManager();
            ClsSecureManager objSecureManager = new ClsSecureManager();
            Attachment = objCandidateManager.GetCandidateAttachment(Convert.ToInt32(e.CommandArgument), out Extension);
            if (Attachment != null && Attachment.Length > 0)
            {
                try
                {
                    Response.Clear();
                    Response.Buffer = true;
                    Response.Charset = "";
                    if (Extension == ".pdf")
                    {
                        Response.ContentType = "application/pdf";
                    }
                    else
                    {
                        Response.ContentType = "application/vsd-msword";
                    }
                    Response.AddHeader("content-disposition", "attachment;filename=" + Resume + Extension);

                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.BinaryWrite(Attachment);                        
                    Response.Flush();
                    Response.End();
                }
                catch (Exception ex)
                {
                    string str = ex.Message + ex.InnerException;
                }
            }
            else
            {
                //ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", "<script type='text/javascript'>alert('Resume is not Uploaded !');</script>");
            }
        }
    }
    catch (Exception ex)
    {
        string str = ex.Message + ex.InnerException;

    }

解决方案

Use the UpdatePanel as shown below,

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
                        <ContentTemplate>
                            <asp:LinkButton ID="lnkDownload" runat="server" Text="View" OnClick="lnkDownload_Click"
                                CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
                        </ContentTemplate>
                        <Triggers>
                            <asp:PostBackTrigger ControlID="lnkDownload" />
                        </Triggers>
                    </asp:UpdatePanel>

这篇关于doc文件没有从用户控件在asp.net中下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆