日期:2014-05-19  浏览次数:20929 次

一个疑难问题,至今找不出原因在哪里,希望大家帮忙!
我用的是.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;这句提示未将对象引用到实例。我试着排出有可能引发问题各种原因都失败了,请大家帮我找找是哪里的问题,谢谢!

------解决方案--------------------
请检查cp是不是被你Dispose了??

一般运行后被释放的对象会造成只能运行一次。

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

==================================================================
博客空间:http://blog.csdn.net/lovingkiss
资源下载:http://download.csdn.net/user/lovingkiss
Email:loving-kiss@163.com
优惠接单开发,收费带初学者,组件控件定制开发,成品源代码批发
联系方式:Q64180940(请清楚注明业务还是技术咨询) 全天在线
==================================================================
------解决方案--------------------
cp = (CommentPart)this.FindControl( "CommentPart1 ");
这句执行后看看cp是否为null
另外就是this.NewsID是什么
------解决方案--------------------
对了,用户控件是不能用 new 来动态加载滴,



CommentPart cp = Page.LoadControl( "CommentPart.ascx所在虚拟路径 ") as CommentPart;

------解决方案--------------------
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 ");
------解决方案--------------------
代码贴全点,不要忽略重点,一堆无用信息
------解决方案--------------------
把NewsDetail里面的有的控件遍厲出來,看看CommentPart1對像是否還存在。
------解决方案--------------------
if(!IsPostBack)
{
this.ShowNews ();
CommentPart cp = new CommentPart();
cp = (CommentPart)this.FindControl( "CommentPart1 ");
object o1 = cp;
cp.NewsID = this.NewsID;
}
当用户发表评论后,此段代码不会执行的哦,cp当然不会被加载的.
你这样动态的加载.cp的状态是不会被保存的哦.
所以这段代码应该放在!page.ispostback 的外面吧.
------解决方案--------------------
if(!IsPostBack)
{
this.ShowNews ();
CommentPart cp = new CommentPart();
cp = (CommentPart)this.FindControl( "CommentPart1 ");
object o1 = cp;
cp.NewsID = this.NewsID;
}

这里只在页面第一次加栽时执行
------解决方案--------------------
可能你需要用一下ViewState来保存一下你的值