当我将FileUpload控件放入更新面板时,为什么我得到HttpPostedFile对象引用null

时间:2011-06-03 20:39:37

标签: asp.net sql-server asp.net-ajax

即使我通过文件上传控件选择某个文件,仍然控制永远不会进入受保护的void btnUpldRes_Click(对象发送者,EventArgs e)的else部分。是因为,我把fileupload控件放到更新面板???

                                                                                                                                                                    
                                                                                 
                                                                                                                  

                                    <br />
                                    <br />
                                    <asp:Button ID="btnUpldRes" runat="server" Font-Bold="True" Font-Names="Arial" 
                                        ForeColor="#0033CC" onclick="btnUpldRes_Click" Text="Upload Resume" 
                                        Width="111px" />
                                </td>
                                <td valign="top">
                                    <asp:FileUpload ID="FileUpload1" runat="server" />
                                </td>
                            </tr>
                        </table>
                    </ContentTemplate>



                </asp:UpdatePanel>

protected void btnUpldRes_Click(object sender,EventArgs e)     {

    if (FileUpload1.PostedFile == null)
    {

        LblErrorupload.Visible = true;
    }
    else
    { 

        string connStringUploadResume = ConfigurationManager.ConnectionStrings["myconnstring"].ConnectionString;
        SqlConnection conUploadResume = new SqlConnection(connStringUploadResume);

        string emailAddLogin = User.Identity.Name.ToString();
        string strSqlUploadResume = "UPDATE  gen_profile SET " +
       "resume=@resume where email=@email";
        SqlCommand cmdUploadResume = new SqlCommand(strSqlUploadResume, conUploadResume);

        cmdUploadResume.Parameters.AddWithValue("@email", emailAddLogin);

        HttpPostedFile resume = FileUpload1.PostedFile;
        int resumeSizeInBytes = resume.ContentLength;
        byte[] bufferToReadResume = new byte[resumeSizeInBytes];
        resume.InputStream.Read(bufferToReadResume, 0, resumeSizeInBytes);
        cmdUploadResume.Parameters.AddWithValue("@resume", bufferToReadResume);
        conUploadResume.Open();
        int i=cmdUploadResume.ExecuteNonQuery();
        conUploadResume.Close();
        if (i == 1)
        {
            if (LblErrorupload.Visible == true)
            {
                LblErrorupload.Visible = false;
            }
            LblUploadSuccess.Visible = true;
        }
        else
        {
            LblUploadSuccess.Text = "Resume Upload Attempt was not successful"; 
            LblUploadSuccess.Visible = true; 
        }

   }
  }