小弟最近初学.net。编了一个小程序如下,却老出现提示错误
源程序:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;namespace WebApplication5
{
/// <summary>
/// WebForm4 的摘要说明。
/// </summary>
public class WebForm4 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
File1.MaxLength=30;
File1.Size=20;
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
// Application.Clear();
// Server.HtmlDecode("<script>window.alert("siwjpioj");
} private void Button2_Click(object sender, System.EventArgs e)
{ HttpPostedFile hpf=File1.PostedFile;
           Label1.Text=hpf.FileName.ToString();
Label2.Text=hpf.ContentLength.ToString();
char[] de={'\\'};
string[] Afilename=hpf.FileName.Split(de);
string strFilename=Afilename[Afilename.Length-1];
Label3.Text=strFilename;
hpf.SaveAs(Server.MapPath(".")+"\\"+strFilename);
}
}
}
错误提示如下:(可以选择文件,但是当选择一个文件后,按按钮是就)
“/WebApplication5”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 67: 
行 68:  HttpPostedFile hpf=File1.PostedFile;
行 69:            Label1.Text=hpf.FileName.ToString();
行 70:  Label2.Text=hpf.ContentLength.ToString();
行 71:  char[] de={'\\'};
 源文件: c:\inetpub\wwwroot\webapplication5\webform4.aspx.cs    行: 69 堆栈跟踪: 
[NullReferenceException: 未将对象引用设置到对象的实例。]
   WebApplication5.WebForm4.Button2_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\webapplication5\webform4.aspx.cs:69
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
   System.Web.UI.Page.ProcessRequestMain() +1277 
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573 
不知道是怎么回事,那位大侠可以帮忙解决一下吗?先谢谢啦!

解决方案 »

  1.   

    楼主, 很明显, 出问题的代码是:  Label1.Text=hpf.FileName.ToString();
    --------------------------------------------------------------------------
    你要将 hpf 这个HtmlInputFile 控件声明为服务器控件, 解决的方法是: 在 .aspx 文件中, 声明 hpf 的地方加上: runat="server" , 就可以解决你的问题
      

  2.   

    这段代码我调试通过了,把你的HtmlInputFile上点右键-“做为服务器端控件运行”即可。如果最后一句hpf.SaveAs(Server.MapPath(".")+"\\"+strFilename);提示你对该路径的访问被拒绝,可以通过对给定路径文件夹配置ASP.NET和VSDEVELOPERS可以读写访问的权限来解决。
      

  3.   

    谢谢楼上两位的意见,我已经在HtmlInputFile上点右键-“做为服务器端控件运行”了。但是还是提示那个问题啊!我真的有点晕啦!
      

  4.   

    加发一下前台的代码,期盼大侠门的指点啊!
    <%@ Page language="c#" Codebehind="WebForm4.aspx.cs" AutoEventWireup="false" Inherits="WebApplication5.WebForm4" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm4</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server" enctype="multipart-data">
    <FONT face="宋体">
    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 80px; POSITION: absolute; TOP: 32px" runat="server"
    Width="152px"></asp:DataGrid>
    <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 352px; POSITION: absolute; TOP: 184px" runat="server"
    Text="Button"></asp:Button>
    <INPUT id="File1" style="Z-INDEX: 103; LEFT: 304px; POSITION: absolute; TOP: 24px" type="file"
    name="File1" runat="server"></FONT>
    <asp:Button id="Button2" style="Z-INDEX: 104; LEFT: 376px; POSITION: absolute; TOP: 96px" runat="server"
    Text="Button"></asp:Button>
    <asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 520px; POSITION: absolute; TOP: 80px" runat="server">Label</asp:Label>
    <asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 520px; POSITION: absolute; TOP: 112px" runat="server">Label</asp:Label>
    <asp:Label id="Label3" style="Z-INDEX: 107; LEFT: 520px; POSITION: absolute; TOP: 152px" runat="server">Label</asp:Label>
    </form>
    </body>
    </HTML>
      

  5.   

    经测试可用的代码,后台:
    using System;
    using System.Collections;
    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.IO;namespace WebApplication5
    {
    /// <summary>
    /// WebForm4 的摘要说明。
    /// </summary>
    public class WebForm4 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid DataGrid2;
    protected System.Web.UI.HtmlControls.HtmlInputFile File2;
    protected System.Web.UI.HtmlControls.HtmlInputFile File1;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Button Button2;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.Label Label3;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    File1.MaxLength=30;
    File1.Size=20;
    // 在此处放置用户代码以初始化页面
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.Button2.Click += new System.EventHandler(this.Button2_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {
    // Application.Clear();
    // Server.HtmlDecode("<script>window.alert("siwjpioj");
    } private void Button2_Click(object sender, System.EventArgs e)
    { HttpPostedFile hpf=File1.PostedFile;
    Response.Write(hpf.FileName.ToString());
    Label1.Text=hpf.FileName.ToString();
    Label2.Text=hpf.ContentLength.ToString();
    char[] de={'\\'};
    string[] Afilename=hpf.FileName.Split(de);
    string strFilename=Afilename[Afilename.Length-1];
    Label3.Text=strFilename;
    hpf.SaveAs(Server.MapPath(".")+"\\"+strFilename);
    }
    }
    }
      

  6.   

    html代码:
    <%@ Page language="c#" Codebehind="WebForm4.aspx.cs" AutoEventWireup="false" Inherits="WebApplication5.WebForm4" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm4</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <FONT face="宋体">
    <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 232px; POSITION: absolute; TOP: 104px" runat="server"
    Text="Button"></asp:Button>
    <asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 248px; POSITION: absolute; TOP: 184px" runat="server"
    Text="Button"></asp:Button>
    <asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 416px; POSITION: absolute; TOP: 96px" runat="server">Label</asp:Label>
    <asp:Label id="Label2" style="Z-INDEX: 104; LEFT: 432px; POSITION: absolute; TOP: 152px" runat="server">Label</asp:Label>
    <asp:Label id="Label3" style="Z-INDEX: 105; LEFT: 440px; POSITION: absolute; TOP: 192px" runat="server">Label</asp:Label>
    <asp:DataGrid id="DataGrid2" style="Z-INDEX: 106; LEFT: 304px; POSITION: absolute; TOP: 264px"
    runat="server"></asp:DataGrid><INPUT style="Z-INDEX: 107; LEFT: 376px; POSITION: absolute; TOP: 40px" type="file" name="File2"
    runat="server" id="File1"></FONT>
    </form>
    </body>
    </HTML>
      

  7.   

    HttpPostedFile hpf=new HttpPostedFile();
    试试。
      

  8.   

    已经试过啦,但是
    HttpPostedFile hpf=new HttpPostedFile();
    的括号里边必须是要带参数的