user2695786
user2695786

Reputation: 30

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

Upvotes: 0

Views: 2052

Answers (1)

ammcom
ammcom

Reputation: 1034

Please check that your master page doesn't contain update panels and doesn't use asp .net ajax as it is the most known reason for this problem.

Upvotes: 1

Related Questions