asp.net WebフォームにAsyncFileUploadコントロールとgridViewがありますファイルをアップロードした後、アップロードされたファイルのリストをグリッドビューにロードしようとしています
ただし、アップロードされたファイルのリストをグリッドビューに表示することはできません。デバッグすると、gridView にバインドする直前に期待されるデータが表示されます。しかし、アップロードされたファイルのリストでグリッドビューが表示されません。助けてください
以下に貼り付けたのは、私の Web フォームと私の 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);
}
}
}