1

我有用户控件,其中包含带有候选数据的网格。有一个带有模板字段链接按钮的列候选名称。我附加了一个我正在下载 word 文件的 rowcommand 事件。我有下载 doc 文件代码,它从简单的网页下载我的 doc 文件,但此代码不适用于用户控制。谁能帮我解决这个问题。它给出的错误响应不可用

  <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;

    }
4

1 回答 1

0

使用如下所示的UpdatePanel,

<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>
于 2015-11-06T16:55:04.070 回答