0

我的 asp.net webform 上有一个 AsyncFileUpload 控件和一个 gridView 上传文件后,我试图用上传的文件列表加载我的 gridview

但是向 gridview 显示上传文件的列表不起作用。当我调试时,我可以在绑定到 gridView 之前看到预期的数据。但是我看不到带有上传文件列表的gridview。请帮忙

下面粘贴的是我的网络表单和我的cs文件

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="test.WebForm2" %>
<%@ Register Assembly="AjaxControlToolkit" 
    Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <cc1:AsyncFileUpload ID="AsyncFileUpload1" Width="400px" runat="server" 
OnClientUploadError="uploadError" OnClientUploadStarted="StartUpload" 
OnClientUploadComplete="UploadComplete" 
CompleteBackColor="Lime" UploaderStyle="Modern" 
ErrorBackColor="Red" ThrobberID="Throbber" 
onuploadedcomplete="AsyncFileUpload1_UploadedComplete" 
UploadingBackColor="#66CCFF" OnDataBinding="AsyncFileUpload1_DataBinding" />
    </div>
        <div>
            <asp:Label ID="lblStatus" runat="server" Style="font-family: Arial; 
    font-size: small;"></asp:Label>


        </div>
              <div class="width100pc floatLeft">
                <asp:GridView ID="grdAttachments" runat="server" ShowFooter="true" AutoGenerateColumns="false"
                    ForeColor="#333333" GridLines="None" CellPadding="2" CellSpacing="0" OnRowDeleting="grdAttachments_RowDeleting"
                    ShowHeaderWhenEmpty="true" EmptyDataText="No attachments to show" Width="100%" OnLoad="grdAttachments_Load" OnRowDataBound="grdAttachments_RowDataBound">
                    <Columns>
                        <asp:BoundField DataField="RowNumber" HeaderText="Row Number" ItemStyle-CssClass="displayNone"
                            HeaderStyle-CssClass="displayNone" FooterStyle-CssClass="displayNone" />
                        <asp:BoundField HeaderText="File name" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="width100pc"
                            ItemStyle-CssClass="width100pc" DataField="FileUpload" />
                        <asp:BoundField DataField="UploadedFullFileName" HeaderText="UploadedFullFileName"
                            ItemStyle-CssClass="displayNone" HeaderStyle-CssClass="displayNone" FooterStyle-CssClass="displayNone" />
                        <asp:CommandField ShowDeleteButton="true" DeleteText="Remove" ItemStyle-HorizontalAlign="Center" />
                    </Columns>
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
                </asp:GridView>
            </div>
    </form>
</body>
</html>
<script type="text/javascript" language="javascript">

    function uploadError(sender, args) {
        document.getElementById('lblStatus').innerText = args.get_fileName(),
            "<span style='color:red;'>" + args.get_errorMessage() + "</span>";
    }

    function StartUpload(sender, args) {
        document.getElementById('lblStatus').innerText = 'Uploading Started.';
    }

    function UploadComplete(sender, args) {
        var filename = args.get_fileName();
        var contentType = args.get_contentType();
        var text = "Size of " + filename + " is " + args.get_length() + " bytes";
        if (contentType.length > 0) {
            text += " and content type is '" + contentType + "'.";
        }
        document.getElementById('lblStatus').innerText = text;
    }

</script>

下面粘贴的是CS文件

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace test
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
            string uniquePath = Path.Combine(ConfigurationManager.AppSettings["fileUploadPath"].ToString(), Session.SessionID);

            if (AsyncFileUpload1.HasFile)
            {
                //string strPath = uniquePath + Path.GetFileName(e.FileName);
                //AsyncFileUpload1.SaveAs(strPath);
                AddNewARowToGrid();
            }
        }

        public void AddNewARowToGrid()
        {
            DataTable dtAttachments = new DataTable();
            int rowNumber = 1;
            string uploadedFullFileName = string.Empty;

            if (ViewState["CurrentATable"] != null)
            {
                dtAttachments = (DataTable)ViewState["CurrentATable"];
                rowNumber = dtAttachments.Rows.Count + 1;
            }
            else
            {
                dtAttachments.Columns.Add(new DataColumn("RowNumber", typeof(string)));
                dtAttachments.Columns.Add(new DataColumn("FileUpload", typeof(string)));
                // dtAttachments.Columns.Add(new DataColumn("Description", typeof(string)));
                dtAttachments.Columns.Add(new DataColumn("UploadedFullFileName", typeof(string)));
            }

            try
            {
                string uniquePath = Path.Combine(ConfigurationManager.AppSettings["fileUploadPath"].ToString(), Session.SessionID);
                if (!Directory.Exists(uniquePath))
                    Directory.CreateDirectory(uniquePath);
                uploadedFullFileName = Path.Combine(uniquePath, AsyncFileUpload1.FileName);
                AsyncFileUpload1.PostedFile.SaveAs(uploadedFullFileName);
            }
            catch (Exception ex)
            {
                               return;
            }

            DataRow drNewRow = dtAttachments.NewRow();

            drNewRow["RowNumber"] = rowNumber;
            drNewRow["FileUpload"] = AsyncFileUpload1.PostedFile.FileName;
            //drNewRow["Description"] = txtDescription.Text;
            drNewRow["UploadedFullFileName"] = uploadedFullFileName;

            dtAttachments.Rows.Add(drNewRow);

            // txtDescription.Text = string.Empty;

            ViewState["CurrentATable"] = dtAttachments;
            Session["CurrentATable"] = dtAttachments;

            BindDataGrid(grdAttachments, dtAttachments);
        }
        private void RemoveNewARowToGrid(string rowNumber)
        {
            if (ViewState["CurrentATable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentATable"];
                DataRow rowToDelete = null;

                if (dtCurrentTable.Rows.Count > 0)
                {
                    foreach (DataRow row in dtCurrentTable.Rows)
                    {
                        if (int.Parse(row["RowNumber"].ToString()) == int.Parse(rowNumber))
                        {
                            rowToDelete = row;
                            break;
                        }
                    }
                    if (rowToDelete != null)
                    {
                        dtCurrentTable.Rows.Remove(rowToDelete);

                        ViewState["CurrentATable"] = dtCurrentTable;

                        if (dtCurrentTable.Rows.Count == 0)
                            LoadEmptyAttachmentSection();
                        else
                            BindDataGrid(grdAttachments, dtCurrentTable);
                    }
                }
            }
        }



        private void BindDataGrid(GridView grd, DataTable dataTable)
        {
            if (null != Session["CurrentATable"])
            {
                dataTable = (DataTable)Session["CurrentATable"];
            }
            grdAttachments.DataSource = dataTable;
            grdAttachments.DataBind();

        }
        private void LoadEmptyAttachmentSection()
        {
            try
            {
                DataTable dtAttachments = new DataTable();
                BindDataGrid(grdAttachments, dtAttachments);
            }
            catch (Exception ex)
            {

                return;
            }
        }
        protected void grdAttachments_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            RemoveNewARowToGrid(grdAttachments.Rows[e.RowIndex].Cells[0].Text);
        }




    }
}
4

1 回答 1

0

AysncFile 上传执行异步,并且工作正常。您正在做的是绑定到未更新的网格,因为它无法重新渲染。它不在更新面板中。您基本上在后台绑定了网格,来自一个完全不同的请求,因此是 ASYNC。

将网格放到更新面板中,并在文件完成并重新绑定网格后对其调用更新。

于 2012-11-09T04:13:07.587 回答