none
Ajax 程序总是报错,奇怪(用的是vs2005+ asp.net ajax 1.0 RRS feed

  • 问题

  • 未将对象引用设置到对象的实例。


    Timer.aspx文件内容
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Timer.aspx.cs" Inherits="Timer" %>

    <!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">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
       
        </div>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:Label ID="Label1" runat="server" Text="txtTime"></asp:Label>
                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
                </Triggers>
            </asp:UpdatePanel>
            <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
            </asp:Timer>
        </form>
    </body>
    </html>

    Timer.aspx.cs内容:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Drawing;

    public partial class Timer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            Label txtTime = (Label)UpdatePanel1.FindControl("txtTime");
            txtTime.Text = "服务器现在时间是:" + DateTime.Now.ToLongTimeString();  //这一句报错,未将对象引用设置到对象的实例。咋办呢?谢谢。
            int red = new Random().Next(255);
            System.Threading.Thread.Sleep(100);  //暂停100毫秒
            int green = new Random().Next(255);
            System.Threading.Thread.Sleep(100);
            int blue = new Random().Next(255);
            txtTime.BackColor = Color.FromArgb(red, green, blue);
            txtTime.ForeColor = Color.FromArgb(green, blue, red);
        }
    }

    上面就停在所示的这一句了,咋办呢?


    maiyude
    2009年8月22日 9:03

答案

  • txtTime 为 null, FindControl 应填 Label1 应该是笔误吧
    Label txtTime = (Label)UpdatePanel1.FindControl("Label1");

    知识改变命运,奋斗成就人生!
    2009年8月22日 9:10
    版主

全部回复