Asp.Net 2.0的TreeView执行RenderControl方法时会出错?
请问有没有人会解决这个问题?我的源代码如下:
System.Web.UI.WebControls.TreeView tree = new TreeView();
this.InitializePostNodes(tree.Nodes, table, (int)topic["tid"], topPostID);
System.IO.StringWriter writer = new System.IO.StringWriter();
tree.SkipLinkText = "";
tree.RenderControl(new HtmlTextWriter(writer));
错误信息如下:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 49: System.IO.StringWriter writer = new System.IO.StringWriter();
Line 50: tree.SkipLinkText = "";
Line 51: tree.RenderControl(new HtmlTextWriter(writer));
Line 52: this.Response.Write(writer);
Line 53:
Source File: h:\Websites\show.aspx Line: 51
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.WebControls.TreeView.GetImageUrl(Int32 index) +5656
System.Web.UI.WebControls.TreeNode.Render(HtmlTextWriter writer, Int32 position, Boolean[] isLast, Boolean enabled) +2472
System.Web.UI.WebControls.TreeView.RenderContents(HtmlTextWriter writer) +487
System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +29
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22
ASP.aspx_1_showforum_aspx.WriteTreeRow(DataRow topic) in h:\Websites\show.aspx:51
ASP.aspx_1_showforum_aspx.OnInit(EventArgs e) in h:\Websites\show.aspx:1043
System.Web.UI.Control.InitRecursive(Control namingContainer) +321
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +692
--------------------编程问答-------------------- 确实有这个问题,应该是一个BUG.
我以前也遇到过,google了一下国外论坛,这个问题也解决不了 --------------------编程问答-------------------- Object reference not set to an instance of an object
感觉什么引用不是实体 哈 --------------------编程问答-------------------- Object reference not set to an instance of an object
======================================
未引用对象到实例 --------------------编程问答-------------------- (int)topic["tid"]这个为null值错误的可能性大些 --------------------编程问答-------------------- 不是的,你可以试试执行这几行
System.Web.UI.WebControls.TreeView tree = new TreeView();
System.IO.StringWriter writer = new System.IO.StringWriter();
tree.RenderControl(new HtmlTextWriter(writer)); --------------------编程问答-------------------- tree.RenderControl(new HtmlTextWriter(writer));
这一行有错?
你单步 调试跟踪进去 看看 是那里出的错 --------------------编程问答-------------------- 这一行完全是调用System.Web.dll的 --------------------编程问答-------------------- 遇到同样问题了。 --------------------编程问答-------------------- Object reference not set to an instance of an object
System.Web.UI.WebControls.TreeView tree = new TreeView();
this.InitializePostNodes(tree.Nodes, table, (int)topic[ "tid "], topPostID);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter writer= new System.IO.StringWriter(sb);
tree.SkipLinkText = " ";
tree.RenderControl(new HtmlTextWriter(writer)); --------------------编程问答-------------------- 是,遇到同样问题了。很奇怪。把treeview换成 button就没事
补充:.NET技术 , ASP.NET