简体   繁体   中英

FileUpload has no file when uploading image in c# asp.net

FileUpload shows null file when uploading image in ASP.NET C#. My page is connected with a master page so when i click on upload button FileUpload control shows null file but when i use it without master page file upload successfully, please tell me how to short out this problem. I am using this code.

<table>
        <tr>
    <td>Enrollment Id</td>
    <td>:</td>
    <td>
        <asp:TextBox ID="txtEnrollmentNo" runat="server" CssClass="text" AutoPostBack="true" OnTextChanged="txtEnrollmentNo_TextChanged"></asp:TextBox>
    </td>
    <td></td>
    </tr>
    <tr>
    <td colspan="4" style="font-weight:bold; text-align:center;">
    Student Details
    </td>
    </tr>
    <tr>
    <td colspan="3" style="background-color:#eee; padding:5px;">
    Upload Photo
    </td>
    </tr>
    <tr>
    <td colspan="3">
        <asp:Image ID="imgPhoto" runat="server" CssClass="imageUpload" />
    </td>
    <td></td>
    </tr>

    <tr>
    <td colspan="3">
        <asp:FileUpload ID="fileUploadPhoto" runat="server" onchange="readPhotoURL(this)" />

         </td>
    <td></td>
    </tr>
    </table>
    <div class="commonArea">
        <asp:Button ID="btnSubmit" runat="server" Text="Upload & Save" TabIndex="3"  onclick="btnSubmit_Click" />
    </div>

Code behind using

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            string extension = Path.GetExtension(fileUploadPhoto.PostedFile.FileName);
            fileUploadPhoto.PostedFile.SaveAs(Server.MapPath("~/StudentImages/Photo/") + txtEnrollmentNo.Text + extension);
            MessageBox("success", txtEnrollmentNo.Text); 

        }
        catch (Exception ex)
        {
            MessageBox("error",ex.Message);
        }
    }

请检查您的母版页是否不包含更新面板,并且不使用asp .net ajax,因为这是此问题的最常见原因。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM