[index.htm]
...
<form id="form1"  method="post" action="prep.aspx" >
内有
<input type="text" name="TextBox1" size="20" id="TextBox1" />
...
[prep.aspx]
内有
...
name = Request["TextBox1"].ToString().Trim(); 
...第一次执行常有“未将对象引用设置到对象的实例”,以后再重复执行就没有这个错误,真奇怪?
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 38:         if (!IsPostBack)
行 39:         {
行 40:             name = Request["TextBox1"].ToString().Trim();  源文件: g:\asp.net\prep.aspx.cs    行: 40 堆栈跟踪: 
[NullReferenceException: 未将对象引用设置到对象的实例。]
   prep.Page_Load(Object sender, EventArgs e) in g:\asp.net\prep.aspx.cs:40
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +47
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061 
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.42; ASP.NET 版本:2.0.50727.42

解决方案 »

  1.   

    Request.Form["TextBox1"].ToString().Trim();
      

  2.   

    if(Request["TextBox1"] != null)
    {
     name = Request["TextBox1"].ToString().Trim(); }
      

  3.   

    可是明明TextBox1里输入了值,怎么会是null呢?
      

  4.   

    我用VS2005调试时没有问题,只是用IE访问时才会出错。
      

  5.   

    内有
    <input type="text" name="TextBox1" size="20" id="TextBox1" value=""/>
    应该先赋初值
      

  6.   

    你是用POST提交的,只有在你触发类似Button按钮的时候 Request["TextBox1"]才存在这个对象。第一次Request["TextBox1"]是null.
    if(Request["TextBox1"] != null)
    {
     name = Request["TextBox1"].ToString().Trim(); }
      

  7.   

    如果是第一次访问此页面,在地址后面加上“?TextBox1=”
      

  8.   

    是不是因为HTML页不能用POST传值,把文件名改成aspx就好用了。
      

  9.   

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
    <form runat="server">
    <input type="text" name="TextBox1" size="20" id="TextBox1" />
    <asp:Button ID="Button1" runat="server" Text="Button" />
        </form>
    </body>
    </html>public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    string name = Request.Params["TextBox1"];
    if(name != null)
    {
    name.Trim();
    Response.Write(name);
    }
        }
    }
      

  10.   

    Xml_explorer(姿势改变命运) ( ) 信誉:100    Blog  2006-09-30 21:16:00  得分: 0  
     
     
       你是用POST提交的,只有在你触发类似Button按钮的时候 Request["TextBox1"]才存在这个对象。第一次Request["TextBox1"]是null.
    if(Request["TextBox1"] != null)
    {
     name = Request["TextBox1"].ToString().Trim(); }有道理
      

  11.   

    你把
    name = Request["TextBox1"].ToString().Trim();
    放在button_click中不就行了
      

  12.   

    第一次执行时不时PostBack,Request["TextBox1"]根本就是null,当你要取它的值进行ToString()就肯定会错误。另外不建议用Request这样来取值,你要在服务器端交互的都用控件,不要直接用input。