【发布时间】:2011-04-21 14:56:31
【问题描述】:
首先,我没有使用 UpdatePanel —— 看起来这是一个常见问题,但在我搜索这个问题之前,我什至不知道是什么。
我有一个 DetailsView 试图上传文件并将文件名插入数据库。问题是,HasFile 总是错误的!知道我做错了什么吗?
C#:
public partial class DocManager : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!(System.Convert.ToBoolean(Session["Admin"])))
Response.Redirect("Index.aspx");
}
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
FileUpload fu1 = (FileUpload) DetailsView1.FindControl("FileUpload1");
if (fu1 == null)
{
e.Cancel = true;
StatusLabel.Text = "Could not find file upload";
}
if (fu1.HasFile)
{
try
{
string filename = Path.GetFileName(fu1.FileName);
fu1.SaveAs(Server.MapPath("~/Docs/") + filename);
StatusLabel.Text = "Upload status: File uploaded!";
e.Values["FileName"] = filename;
}
catch (Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
else
{
e.Cancel = true;
StatusLabel.Text = "No file uploaded";
return;
}
DropDownList dd1 = (DropDownList)DetailsView1.FindControl("DropDownList2");
DropDownList dd2 = (DropDownList)DetailsView1.FindControl("DropDownList4");
e.Values["Type"] = dd1.SelectedValue;
e.Values["MeetingID"] = dd2.SelectedValue;
}
protected void DetailsView1_ItemEditing(object sender, DetailsViewInsertEventArgs e)
{
FileUpload fu1 = (FileUpload)DetailsView1.FindControl("FileUpload2");
if (fu1 == null)
e.Cancel = true;
if (fu1.HasFile) {
try
{
string filename = Path.GetFileName(fu1.FileName);
fu1.SaveAs(Server.MapPath("~/Docs/") + filename);
StatusLabel.Text = "Upload status: File uploaded!";
e.Values["FileName"] = filename;
}
catch (Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
else
e.Cancel = true;
DropDownList dd1 = (DropDownList)DetailsView1.FindControl("DropDownList1");
DropDownList dd2 = (DropDownList)DetailsView1.FindControl("DropDownList3");
e.Values["Type"] = dd1.SelectedValue;
e.Values["MeetingID"] = dd2.SelectedValue;
}
}
PageLoad 中唯一的事情是检查以确保用户正确登录,在此之前不应运行任何其他内容。
表单本身:
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"
AutoGenerateRows="False" DataSourceID="Docs" DefaultMode="Insert"
OnItemInserting="DetailsView1_ItemInserting"
OnItemEditing="DetailsView1_ItemEditing"
>
<Fields>
<asp:BoundField DataField="Documents.Title" HeaderText="Title"
SortExpression="Documents.Title" />
<asp:TemplateField HeaderText="Type" SortExpression="Type">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Selected="True" Value="Presentation">Presentation</asp:ListItem>
<asp:ListItem Value="Handout">Handout</asp:ListItem>
<asp:ListItem Value="Minutes">Minutes</asp:ListItem>
<asp:ListItem Value="Agenda">Agenda</asp:ListItem>
<asp:ListItem Value="Other">Other</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Selected="True" Value="Presentation">Presentation</asp:ListItem>
<asp:ListItem Value="Handout">Handout</asp:ListItem>
<asp:ListItem Value="Minutes">Minutes</asp:ListItem>
<asp:ListItem Value="Agenda">Agenda</asp:ListItem>
<asp:ListItem Value="Other">Other</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Type") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MeetingID" SortExpression="MeetingID">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="Meetings"
DataTextField="Title" DataValueField="MeetingID">
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="Meetings"
DataTextField="Title" DataValueField="MeetingID">
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("MeetingID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FileName" SortExpression="FileName">
<EditItemTemplate>
<asp:FileUpload ID="FileUpload2" runat="server" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("FileName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
ETA:整个表格:
<asp:Content ID="Content2" ContentPlaceHolderID="mainContent" Runat="Server">
<h1>Document Manager</h1>
<h2>Existing Documents</h2>
<asp:GridView ID="GridView1" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="Docs"
OnRowDeleting="GridView1_RowDeleting"
DataKeyNames="DocID" BackColor="White" BorderColor="#E7E7FF"
BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal">
<AlternatingRowStyle BackColor="#F7F7F7" />
<Columns>
<asp:BoundField DataField="Documents.Title" HeaderText="Title"
SortExpression="Documents.Title" />
<asp:BoundField DataField="FileName" HeaderText="FileName"
SortExpression="FileName" />
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<asp:BoundField DataField="Meetings.Title" HeaderText="Meeting"
SortExpression="Meetings.Title" />
<asp:CommandField ShowDeleteButton="True" ShowSelectButton="True" />
<asp:HyperLinkField DataNavigateUrlFields="FileName" Text="Download" />
</Columns>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F4F4FD" />
<SortedAscendingHeaderStyle BackColor="#5A4C9D" />
<SortedDescendingCellStyle BackColor="#D8D8F0" />
<SortedDescendingHeaderStyle BackColor="#3E3277" />
</asp:GridView>
<h2>Add New</h2>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"
AutoGenerateRows="False" DataSourceID="Docs" DefaultMode="Insert"
OnItemInserting="DetailsView1_ItemInserting"
OnItemEditing="DetailsView1_ItemEditing"
>
<Fields>
<asp:BoundField DataField="Documents.Title" HeaderText="Title"
SortExpression="Documents.Title" />
<asp:TemplateField HeaderText="Type" SortExpression="Type">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Selected="True" Value="Presentation">Presentation</asp:ListItem>
<asp:ListItem Value="Handout">Handout</asp:ListItem>
<asp:ListItem Value="Minutes">Minutes</asp:ListItem>
<asp:ListItem Value="Agenda">Agenda</asp:ListItem>
<asp:ListItem Value="Other">Other</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Selected="True" Value="Presentation">Presentation</asp:ListItem>
<asp:ListItem Value="Handout">Handout</asp:ListItem>
<asp:ListItem Value="Minutes">Minutes</asp:ListItem>
<asp:ListItem Value="Agenda">Agenda</asp:ListItem>
<asp:ListItem Value="Other">Other</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Type") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MeetingID" SortExpression="MeetingID">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="Meetings"
DataTextField="Title" DataValueField="MeetingID">
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList4" runat="server" DataSourceID="Meetings"
DataTextField="Title" DataValueField="MeetingID">
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("MeetingID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FileName" SortExpression="FileName">
<EditItemTemplate>
<asp:FileUpload ID="FileUpload2" runat="server" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("FileName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:AccessDataSource ID="Docs" runat="server"
DataFile="~/App_Data/Database1.accdb"
DeleteCommand="DELETE FROM [Documents] WHERE [DocID] = ?"
InsertCommand="INSERT INTO [Documents] ([Title], [Type], [FileName], [MeetingID]) VALUES (@Title, @Type, @FileName, @MeetingID)"
SelectCommand="SELECT Documents.DocID, Documents.Title, Documents.FileName, Documents.Type, Meetings.Title, Documents.MeetingID FROM
(Documents LEFT OUTER JOIN Meetings ON Documents.MeetingID = Meetings.MeetingID)"
UpdateCommand= "UPDATE [Documents] SET [Title] = ?, [Type] = ?, [FileName] = ?, [MeetingID] = ? WHERE [DocID] = ?">
<DeleteParameters>
<asp:Parameter Name="DocID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Documents.Title" Type="String" />
<asp:Parameter Name="Type" Type="String" />
<asp:Parameter Name="FileName" Type="String" />
<asp:Parameter Name="MeetingID" Type="Int32" />
</InsertParameters>
<SelectParameters>
<asp:SessionParameter Name="?" SessionField="UserID" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Type" Type="String" />
<asp:Parameter Name="FileName" Type="String" />
<asp:Parameter Name="MeetingID" Type="Int32" />
<asp:Parameter Name="DocID" Type="Int32" />
</UpdateParameters>
</asp:AccessDataSource>
<asp:Label ID="StatusLabel" runat="server" Text="Label"></asp:Label>
<asp:AccessDataSource ID="Meetings" runat="server"
DataFile="~/App_Data/Database1.accdb"
SelectCommand="SELECT [MeetingID], [Title] FROM [Meetings] WHERE ([AdminID] = ?)">
<SelectParameters>
<asp:SessionParameter Name="AdminID" SessionField="UserID" Type="Int32" />
</SelectParameters>
</asp:AccessDataSource>
我还在上面的 c# 代码的其余部分进行了编辑,它没有那么长
母版页中的表单元素:
<body style="background-color: rgb(231, 231, 255);height:100%;margin:0px;padding:0px">
<form id="form1" runat="server">
ETA:我发现了问题。我一直在使用一个空的文本文件作为测试——它读取为 0 字节,使文件上传者误以为它没有文件。在文件中添加一些文本使其完美运行。
感谢大家的帮助!
【问题讨论】:
-
应该没问题。我已经深入了解了您的代码,但我看不出代码有任何问题。您可以发布完整的表单设计和完整的页面代码。我认为问题是另一面。
-
+1 虽然这是您在 Stackoverflow 上的第一个问题,但您已经清楚地发布了您的问题并解释了您的问题。
-
页面中的表单元素长什么样子?
-
您构建的 .net 版本是什么?根据您的示例,我无法复制您的问题。我必须做出一些改变:
Docs改成ObjectDataSource,删除FileName以外的字段,从GridView中删除样式。我在 vs2010 中针对 .net 3.5 构建。 -
它突然打动了我——如果我使用空文件进行测试,会不会抛出假阴性?
标签: c# .net asp.net visual-studio-2010