C# DetailsView中的文件上载:HasFile始终为false
首先也是最重要的一点,我没有使用UpdatePanel——这似乎是一个常见的问题,但在我搜索这个问题之前,我甚至不知道它是什么 我有一个试图上传文件并将文件名插入数据库的详细视图。问题是,HasFile总是显示为false!知道我做错了什么吗 C#: } PageLoad中唯一的一件事是检查用户是否正确登录,在此之前不应运行任何其他操作 表格本身:C# DetailsView中的文件上载:HasFile始终为false,c#,.net,asp.net,visual-studio-2010,C#,.net,Asp.net,Visual Studio 2010,首先也是最重要的一点,我没有使用UpdatePanel——这似乎是一个常见的问题,但在我搜索这个问题之前,我甚至不知道它是什么 我有一个试图上传文件并将文件名插入数据库的详细视图。问题是,HasFile总是显示为false!知道我做错了什么吗 C#: } PageLoad中唯一的一件事是检查用户是否正确登录,在此之前不应运行任何其他操作 表格本身: <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="
<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">
埃塔:我发现了问题。我一直在使用一个空的文本文件作为测试——读取的是0字节,让文件上传程序误以为它没有文件。向文件中添加一些文本可以使其完美工作
谢谢大家的帮助 我发现了问题。我一直在使用一个空的文本文件作为测试——读取的是0字节,让文件上传程序误以为它没有文件。向文件中添加一些文本可以使其完美工作
咆哮为什么一个大小为0字节的文件与其他地方根本没有文件是两码事 这不应该是个问题。我已经深入了解了您的代码,但我看不出代码有任何问题。你能发布完整的表单设计和完整的页面代码吗。我认为问题是另一面。+1虽然这是您关于Stackoverflow的第一个问题,但您必须清楚地发布您的问题并解释您的问题。页面中的表单元素是什么样子的?您构建的.net版本是什么?我无法根据您的示例复制您的问题。我必须做一些更改:
Docs
作为ObjectDataSource
,删除除FileName
以外的字段,从GridView
中删除样式。我是在vs2010中根据.net 3.5构建的。我突然想到——如果我使用一个空文件进行测试,会不会抛出一个假阴性?看看FileUpload组件的源代码,很容易解释:public bool HasFile{get{HttpPostedFile postedFile=this.postedFile;if(postedFile!=null)return postedFile.ContentLength>0;否则返回false;}}
,尽管这与实际情况不一致
<body style="background-color: rgb(231, 231, 255);height:100%;margin:0px;padding:0px">
<form id="form1" runat="server">