上传文件时无法在GridView中获取发布的文件名

时间:2016-05-19 06:45:23

标签: c# asp.net

上传文件时无法在GridView中获取发布的文件名。 这是我尝试过的。

ASPX文件内容如下所示:

<asp:GridView ID="GVProtocol" runat="server" AllowPaging="True"
                                HorizontalAlign="Center" CssClass="DGList" HeaderStyle-CssClass="DGHeader"
                                RowStyle-CssClass="DGRow" AlternatingRowStyle-CssClass="DGRowAlt" OnRowCommand="GVProtocol_RowCommand"
                                OnPageIndexChanging="GVProtocol_PageIndexChanging" Height="90px" Width="100%" AutoGenerateColumns="False">
                                <Columns>
                                    <asp:TemplateField HeaderText="SL NO" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="20">
                                        <ItemTemplate>
                                            <%# (( ( GridView ) ( ( GridViewRow ) Container ).Parent.Parent ).PageSize + Container.DataItemIndex + 1)- 15 %>
                                        </ItemTemplate>
                                        <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                                        <ItemStyle HorizontalAlign="Center"></ItemStyle>
                                    </asp:TemplateField>                                                                                                                     
                                    <asp:BoundField HeaderText="Name" DataField="NAME" />
                                    <asp:BoundField HeaderText="Unit" DataField="UNIT" />
                                    <asp:BoundField HeaderText="Standard Value" DataField="STANDARD_VALUE" />
                                    <asp:TemplateField HeaderText="Upload XML" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
                                        <ItemTemplate>
                                            <asp:FileUpload ID="FileUpload" runat="server" EnableViewState="true" />
                                            <asp:Button runat="server" ID="btnUpload" Text="upload" CommandArgument="<%# Container.DataItemIndex %>" CommandName="upload" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="FilePath" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="200">
                                        <ItemTemplate>
                                            <asp:Label runat="server" ID="lblFilePath">-</asp:Label>                                                 
                                            <asp:LinkButton ID="btnDownload" runat="server" CommandName="download" CommandArgument="<%# Container.DataItemIndex %>"></asp:LinkButton>
                                        </ItemTemplate>                                                                                                                                 
                                    </asp:TemplateField>
                                </Columns>

                                <PagerStyle CssClass="pagerStyle" />
                                <HeaderStyle Wrap="False" CssClass="DGHeader" />
                                <RowStyle CssClass="DGRow" />
                                <AlternatingRowStyle CssClass="DGRowAlt" HorizontalAlign="Center" />
                            </asp:GridView>

这是上述ASPX内容的CodeBehind文件:

protected void GVProtocol_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            int Index = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName == "upload")
            {
                string strFilePathTemp = Server.MapPath("~/ProtocolCommunication/");

                //int Index = ((GridViewRow)((sender as Control)).NamingContainer).RowIndex;



                LinkButton lbDownload = (LinkButton)GVProtocol.Rows[Index].FindControl("btnDownload");
                lbDownload.Text = "<span class='glyphicon glyphicon-download-alt'>";
                Label lblFilePathTemp = (Label)GVProtocol.Rows[Index].FindControl("lblFilePath");
                lblFilePathTemp.Text = strFilePathTemp;
                FileUpload fileUpload = (FileUpload)GVProtocol.Rows[Index].FindControl("FileUpload");

                if(fileUpload != null)
                {
                    string fileName = Path.GetFileName(fileUpload.PostedFile.FileName);
                    fileUpload.PostedFile.SaveAs(strFilePathTemp + fileName);
                }


            }
            if (e.CommandName == "download")
            {
                Label lblFilePathTemp = (Label)GVProtocol.Rows[Index].FindControl("lblFilePath");
                string fileNameAndPath = lblFilePathTemp.Text.ToString();


                string file = Path.GetFileName(fileNameAndPath);
                Response.ContentType = ContentType;
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(fileNameAndPath));
                Response.TransmitFile(Server.MapPath("~/ProtocolCommunication/"));                    
                Response.End();
            } 
        }
        catch(Exception ex)
        {
            throw ex;
        }              
    }

寻找上述问题的可能解决方案。

1 个答案:

答案 0 :(得分:0)

我得到了解决方案,通过使用  asp:updatePanel控件,带有postbacktrigger,用于保存在girdview中动态创建的控件ID。使用它,您可以上传和下载文件到任何gridview行。