asp.net – 文件上传控件HasFile始终为false,name始终为Empty String,不使用更新面板

前端之家收集整理的这篇文章主要介绍了asp.net – 文件上传控件HasFile始终为false,name始终为Empty String,不使用更新面板前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个详细信息视图,其中包含文件上载字段.当我填写信息并上传文件(我已经尝试了9k到6.8MB的多个文件)时,所有信息(文本字段)提交都很好,但是当我检查HasFile时,上传文件总是返回false检查文件名时返回String.Empty.

难道我做错了什么?详细信息视图位于Panel而非Update Panel中

<asp:Panel ID="pnlUpdate" runat="server"
        Visible="false">
        <h4 runat="server" id="h2SubCaption">Person Details</h4>
        <asp:DetailsView ID="dvAssignment" 
            runat="server" 
            AutoGenerateRows="false" 
            Width="100%"
            SkinID="SampleDetailsView" 
            CssSelectorClass="PrettyDetailsView"
            DataKeyNames="guidMemberId"
            DefaultMode="Edit"
            OnItemUpdating="dvAssignment_ItemUpdating" 
            OnModeChanging="dvAssignment_ModeChanging"
            AutoGenerateEditButton="True" >  
<Fields>   
<asp:TemplateField HeaderText="Nomination Letter">
                        <EditItemTemplate>
                            <asp:FileUpload runat="server" ID="fileuploadNomination" />
                        </EditItemTemplate>
                    </asp:TemplateField> .....

代码背后:

FileUpload _nomination = (FileUpload)dv.FindControl("fileuploadNomination");
    byte[] nominationByte = null;
    if (_nomination.FileName != string.Empty)
        nominationByte = _nomination.FileBytes;
    //if(_nomination.HasFile)
    //nominationByte = _nomination.FileBytes;

编辑
添加了一个Page_Load调用,当我单击DetailsView的自动生成的更新按钮时,看起来好像该页面正在回发.这个回发可能正在清除我的FileUpload字段.关于如何绕过它的任何想法?

编辑#2我现在已经在DetailsView周围放置了一个更新面板并将回发触发器设置为DetailsView(见下文)并且它仍然无法正常工作,它似乎在提交之前清除了上传控件.

<asp:UpdatePanel ID="updatePnl" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Panel ID="pnlUpdate" runat="server"
            Visible="false">
            <h4 runat="server" id="h2SubCaption">Person Details</h4>
            <asp:DetailsView ID="dvAssignment" 
                runat="server" 
                AutoGenerateRows="false" 
                Width="100%"
                SkinID="SampleDetailsView" 
                CssSelectorClass="PrettyDetailsView"
                DataKeyNames="guidMemberId"
                DefaultMode="Edit"
                OnItemUpdating="dvAssignment_ItemUpdating" 
                OnModeChanging="dvAssignment_ModeChanging"
                AutoGenerateEditButton="True" >
                <FieldHeaderStyle Font-Bold="True" Width="150px" />
                <Fields>
<asp:FileUpload runat="server" ID="fileuploadNomination" />
                        </EditItemTemplate>
                    </asp:TemplateField>
 </Fields>
                </asp:DetailsView >       
            </asp:Panel>
         </ContentTemplate>
        <Triggers>
            <asp:PostBackTrigger ControlID="dvAssignment" /> 
        </Triggers>
    </asp:UpdatePanel>

Gridview代码按要求

<asp:GridView ID="gvQuality" 
        runat="server" 
        AutoGenerateColumns="False"
        Width="100%"
        DataKeyNames="guidMemberId"
        CssSelectorClass="PrettyGridView"
        SkinID="SampleGridView"
        OnSelectedIndexChanged="gvQuality_SelectedIndexChanged" 
        onrowdatabound="gvQuality_RowDataBound">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                   <asp:LinkButton ID="btnViewDetails" runat="server" Text="Edit" CommandName="Select" />
                                    </ItemTemplate>
            </asp:TemplateField>

一些绑定字段在此之后(名字,姓氏等)

protected void gvQuality_SelectedIndexChanged(object sender,EventArgs e)
{
    Guid guidMemberId = (Guid)gvQuality.SelectedDataKey.Values["guidMemberId"];
    PortalDataContext db = new PortalDataContext(AuthenticatedUser.ConnectionString);
    h2SubCaption.InnerText = "Update Person";
    dvAssignment.ChangeMode(Detailsviewmode.Edit);

    dvAssignment.DataSource = LINQ Query Here
    dvAssignment.DataBind();
}

解决方法

大家好,感谢所有的帮助,但我明白了.我必须设置Page.Form.Enctype =“multipart / form-data.

这是它的代码

protected void Page_Load(object sender,EventArgs e)
{
    this.Page.Form.Enctype = "multipart/form-data";
}

猜你在找的asp.Net相关文章