文件上传控制HasFile总是假的,名字始终是空字符串,没有使用更新面板 [英] File Upload control HasFile always false, name is always Empty String, no update panel used

查看:107
本文介绍了文件上传控制HasFile总是假的,名字始终是空字符串,没有使用更新面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个详细查看中有一个文件上传域。当我填写相关信息并上传一个文件(我已经试过多个文件,从9K到6.8MB)所有信息(文本字段)的提交罚款,但上传的文件总是返回一个错误,当我检查HasFile始终返回的String.Empty当我检查的文件名。

我是不是做错了什么?详细视图是一个面板和不是更新面板

 < ASP:面板ID =pnlUpdate=服务器
        可见=假>
        < H4 =服务器ID =h2SubCaption>个人详细资料及LT; / H4>
        < ASP:DetailsView控件ID =dvAssignment
            =服务器
            AutoGenerateRows =假
            WIDTH =100%
            SkinID =SampleDetailsView
            CssSelectorClass =prettyDetailsView
            的DataKeyNames =guidMemberId
            DefaultMode =编辑
            OnItemUpdating =dvAssignment_ItemUpdating
            OnModeChanging =dvAssignment_ModeChanging
            AutoGenerateEditButton属性=真>
LT&;&领域GT;
< ASP:的TemplateField的HeaderText =提名信>
                        <&EditItemTemplate的GT;
                            < ASP:文件上传=服务器ID =fileuploadNomination/>
                        < / EditItemTemplate中>
                    < / ASP:的TemplateField> .....

code背后:

 的FileUpload _nomination =(文件上传)dv.FindControl(fileuploadNomination);
    字节[] nominationByte = NULL;
    如果(_nomination.FileName!=的String.Empty)
        nominationByte = _nomination.FileBytes;
    //if(_nomination.HasFile)
    // nominationByte = _nomination.FileBytes;

修改
我添加了一个调用的Page_Load它看起来好像页面回发时,我点击了DetailsView控件自动生成的更新按钮。这回发可能清理出我的FileUpload领域。任何想法如何解决呢?

编辑#2 我现在已经把周围的DetailsView控件更新面板和设置回传触发DetailsView控件(见下文),它仍然不能正常工作,好像天要放晴上传控件之前在提交。

 < ASP:的UpdatePanel ID =updatePnl=服务器的UpdateMode =条件>
    <&的ContentTemplate GT;
        < ASP:面板ID =pnlUpdate=服务器
            可见=假>
            < H4 =服务器ID =h2SubCaption>个人详细资料及LT; / H4>
            < ASP:DetailsView控件ID =dvAssignment
                =服务器
                AutoGenerateRows =假
                WIDTH =100%
                SkinID =SampleDetailsView
                CssSelectorClass =prettyDetailsView
                的DataKeyNames =guidMemberId
                DefaultMode =编辑
                OnItemUpdating =dvAssignment_ItemUpdating
                OnModeChanging =dvAssignment_ModeChanging
                AutoGenerateEditButton属性=真>
                < FieldHeaderStyle字体粗体=真WIDTH =150像素/>
                LT&;&领域GT;


                            

 < ASP:文件上传=服务器ID =fileuploadNomination/>
                        < / EditItemTemplate中>
                    < / ASP:的TemplateField>
 < /场>
                < / ASP:DetailsView控件>
            < / ASP:面板>
         < /&的ContentTemplate GT;
        <&触发器GT;
            < ASP:PostBackTrigger控件ID =dvAssignment/>
        < /触发器>
    < / ASP:的UpdatePanel>

的要求的GridView code

 < ASP:GridView控件ID =gvQuality
        =服务器
        的AutoGenerateColumns =FALSE
        WIDTH =100%
        的DataKeyNames =guidMemberId
        CssSelectorClass =prettyGridView
        SkinID =SampleGridView
        OnSelectedIndexChanged =gvQuality_SelectedIndexChanged
        onrowdatabound =gvQuality_RowDataBound>
        <柱体和GT;
            < ASP:的TemplateField>
                <&ItemTemplate中GT;
                   < ASP:LinkBut​​ton的ID =btnViewDetails=服务器文本=编辑的CommandName =选择/>
                                    < / ItemTemplate中>
            < / ASP:的TemplateField>

几个绑定字段是这个(姓,名等)

 保护无效gvQuality_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
    GUID guidMemberId =(GUID)gvQuality.SelectedDataKey.Values​​ [guidMemberId];
    PortalDataContext DB =新PortalDataContext(AuthenticatedUser.ConnectionString);
    h2SubCaption.InnerText =更新人;
    dvAssignment.ChangeMode(DetailsViewMode.Edit);    dvAssignment.DataSource = LINQ查询这里
    dvAssignment.DataBind();
}


解决方案

每个人,感谢所有的帮助,但我想它了。我不得不设置 Page.Form.Enctype =的multipart / form-data的

下面是code,它的工作!

 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
    this.Page.Form.Enctype =的multipart / form-data的;
}

I have a Details View that has a file upload field in it. When I fill out the information and upload a file (I have tried multiple files ranging from 9k to 6.8MB) all of the information (text fields) submit fine, but the uploaded file is always returning a false when I check the HasFile and always returns String.Empty when I check the file name.

Am I doing something wrong? The details view is in a Panel and Not an 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> .....

Code Behind:

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

EDIT I added a Page_Load call and it looks as if the page is posting back when I click the Auto Generated Update Button for the DetailsView. This postback is probably clearing out my FileUpload field. Any ideas on how to get around it?

Edit #2 I have now put an update panel around the DetailsView and set the postback trigger the DetailsView (see below) and it still is not working, it seems to be clearing the upload control prior to submitting.

<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 Code as requested

 <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>

a few bound fields are after this (first name, last name, etc)

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();
}

解决方案

Everyone, Thanks for all of the help but I figured it out. I had to set the Page.Form.Enctype = "multipart/form-data.

Here is the code for it to work!

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

这篇关于文件上传控制HasFile总是假的,名字始终是空字符串,没有使用更新面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆