System.NullReferenceException: Object reference not set to an instance of an object. 出错代码:string FilePath = PhotoUpload.PostedFile.FileName;为何!???????????

解决方案 »

  1.   

    PhotoUpload.PostedFile为空
    就是没有选择文件
      

  2.   

    已经选了,不过我的这些控件是放在UpdatePanel中才出现这情况,要怎么搞才不会出错呢
      

  3.   

    FileUpLoad和UpdatePanel冲突,你在点击的上传按钮的时候,在client端加上$clearHandler(当前所在表单);就可以了
      

  4.   


    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SimpleFileUpLoad.aspx.cs"
        Inherits="WebApplication1.SimpleFileUpLoad" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>无标题页</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>    <script language="javascript" type="text/javascript">   
            function Clear(formId)   
            {   
                $clearHandlers($get(formId));
            }     
        </script>    <div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <table style="width: 343px">
                        <tr>
                            <td style="width: 100px">
                                单文件上传
                            </td>
                            <td style="width: 100px">
                            </td>
                        </tr>
                        <tr>
                            <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
                                <ContentTemplate>
                                    <td style="width: 100px">
                                        <asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
                                            <ContentTemplate>
                                                <asp:FileUpload ID="FileUpload1" runat="server" Width="478px" />
                                            </ContentTemplate>
                                        </asp:UpdatePanel>
                                    </td>
                                    <td style="width: 100px">
                                        <asp:Button ID="bt_upload" runat="server" OnClick="bt_upload_Click" Text="上传" />
                                    </td>
                                </ContentTemplate>
                            </asp:UpdatePanel>
                        </tr>
                        <tr>
                            <td style="width: 100px; height: 21px;">
                                <asp:Label ID="lb_info" runat="server" ForeColor="Red" Width="183px"></asp:Label>
                            </td>
                            <td style="width: 100px; height: 21px">
                            </td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        </form>
    </body>
    </html>
      

  5.   


    using System;
    using System.Web.UI;namespace WebApplication1
    {
        public partial class SimpleFileUpLoad : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    this.bt_upload.Attributes.Add("onclick", "Clear('" + this.form1.ClientID + "');");
                }
            }        protected void bt_upload_Click(object sender, EventArgs e)
            {
                try
                {
                    if (FileUpload1.PostedFile.FileName == "")
                    {
                        this.lb_info.Text = "请选择文件!";
                    }
                    else
                    {
                        string filepath = FileUpload1.PostedFile.FileName;
                        string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
                        string serverpath = Server.MapPath("images/") + filename;
                        FileUpload1.PostedFile.SaveAs(serverpath);
                        this.lb_info.Text = "上传成功!";
                    }
                }
                catch (Exception ex)
                {
                    this.lb_info.Text = "上传发生错误!原因是:" + ex.ToString();
                }
            }
        }
    }
      

  6.   

    这样可以是可以了,但是整个面会刷新的,那我还不如不用UpdatePanel了,还有什么方法吗
      

  7.   

    文件上传,不能使用UpdatePanel。否则是会错误的。你可以用Iframeset替换上传部分。