我用的是.net2003;
我用个Repeater列出新闻的标题的列表
<ItemTemplate>
<asp:HyperLink CssClass="webnews" Target="_blank" ID="HNews" Runat="server"></asp:HyperLink>
</ItemTemplate>
然后在后台HNews.NavigateUrl = String.Format("NewsDetail.aspx?NewsID={0}", ID);
在NewsDetail.aspx里我放了个名为CommentPart的用户控件用来发表评论,然后给它赋参数:
CommentPart cp = new CommentPart();
cp = (CommentPart)this.FindControl("CommentPart1");
cp.NewsID = this.NewsID;
问题来了,第一次打开任何一个新闻详细界面的时候,都能正常显示,但是第二次打开新闻详细界面的时候会在cp.NewsID = this.NewsID;这句提示未将对象引用到实例。我试着排出有可能引发问题各种原因都失败了,请大家帮我找找是哪里的问题,谢谢!

解决方案 »

  1.   

    请检查cp是不是被你Dispose了??一般运行后被释放的对象会造成只能运行一次。==================================================================
    博客空间:http://blog.csdn.net/lovingkiss
    资源下载:http://download.csdn.net/user/lovingkiss
    Email:loving-kiss@163.com
    优惠接单开发,收费带初学者,组件控件定制开发,成品源代码批发
    联系方式:Q64180940(请清楚注明业务还是技术咨询)  全天在线
    ==================================================================
      

  2.   

    编程需要学会中断,看看是那一个变量引发的故障;==================================================================
    博客空间:http://blog.csdn.net/lovingkiss
    资源下载:http://download.csdn.net/user/lovingkiss
    Email:loving-kiss@163.com
    优惠接单开发,收费带初学者,组件控件定制开发,成品源代码批发
    联系方式:Q64180940(请清楚注明业务还是技术咨询)  全天在线
    ==================================================================
      

  3.   

    cp = (CommentPart)this.FindControl("CommentPart1");
    这句执行后看看cp是否为null
    另外就是this.NewsID是什么
      

  4.   

    CommentPart cp = new CommentPart();
    cp = (CommentPart)this.FindControl("CommentPart1");
    cp.NewsID = this.NewsID;==========LZ 在哪里将 cp 加入到页面的控件集合中?
      

  5.   

    对了,用户控件是不能用 new 来动态加载滴,得CommentPart cp = Page.LoadControl("CommentPart.ascx所在虚拟路径") as CommentPart;
      

  6.   

    看见的帮忙http://community.csdn.net/Expert/topic/5677/5677248.xml?temp=.4019739谢谢
      

  7.   

    不好意思,我忘了说明,我检测了this.NewsID是有值得,就是cp.NewsID这里有问题,我刚才检测了,在第二次打开新闻界面的时候CP的确是空的,刚才lovingkiss说的是不是被我Dispose了,我没找到我在哪里Dispose了,能不能说的再说的详细些?谢谢
      

  8.   

    不用过5分钟,用_blank或者_self连续的打开新闻连接都会出现问题,我是这样加载控件的:
    private void Page_Load(object sender, System.EventArgs e)
    {
    this.NewsID=Tool.Cint (this.GetQueryString ("NewsID"));
    if(!IsPostBack)
    {
    this.ShowNews ();
    CommentPart cp = new CommentPart();
    cp = (CommentPart)this.FindControl("CommentPart1");
    object o1 = cp;
    cp.NewsID = this.NewsID;
    }
    }
      

  9.   

    1。
    UserControl 就没有实现 IDisposable 接口,不会有 Dispose 方法2。
    我是这样加载控件的:CommentPart cp = new CommentPart();
    cp = (CommentPart)this.FindControl("CommentPart1");
    =======哪里加载的?CommentPart1 拽到 .aspx 的?另外,这里 new 一个,纯属浪费 CommentPart cp = null;
    cp = (CommentPart)this.FindControl("CommentPart1");
      

  10.   

    不好意思我还是不大会贴代码,CommentPart是拖拽到NewsDetail.aspx里的,所以我刚才贴的代码是在NewsDetail.aspx里加载的,我刚才用了CommentPart cp = null;
    cp = (CommentPart)this.FindControl("CommentPart1");,第二次连接到NewsDetail.aspx的时候cp还是未定义的值
      

  11.   

    把NewsDetail里面的有的控件遍厲出來,看看CommentPart1對像是否還存在。
      

  12.   

    回楼上的,先问一句,如果不存在呢?我确实已经将空间拖到NewsDetail里了,怎么可能会不存在呢?
      

  13.   

    if(!IsPostBack)  
    {
    this.ShowNews ();
    CommentPart cp = new CommentPart();
    cp = (CommentPart)this.FindControl("CommentPart1");
    object o1 = cp;
    cp.NewsID = this.NewsID;
    }
    当用户发表评论后,此段代码不会执行的哦,cp当然不会被加载的.
    你这样动态的加载.cp的状态是不会被保存的哦.
    所以这段代码应该放在!page.ispostback 的外面吧.
      

  14.   

    if(!IsPostBack)  
    {
    this.ShowNews ();
    CommentPart cp = new CommentPart();
    cp = (CommentPart)this.FindControl("CommentPart1");
    object o1 = cp;
    cp.NewsID = this.NewsID;
    }这里只在页面第一次加栽时执行
      

  15.   

    CommentPart cp = new CommentPart();
    cp = (CommentPart)this.FindControl("CommentPart1");
    cp.NewsID = this.NewsID;
    ---------------------------------
    bs 的特点:无状态
    --------------------
    你的创建控件代码放在 load事件,加入判断IsPostPage?
      

  16.   

    可能你需要用一下ViewState来保存一下你的值
      

  17.   

    回楼上的,用ViewState保存自定义的变量类型需要实现ISerialization接口,使变量可被序列化 ,比较麻烦,我用session保存似乎可以,我再多调试下!