我这里有一段代码
var anchor = new HtmlAnchor {HRef = temp, InnerText = this.LinkDescription};
anchor.Attributes.Add("class", "navActive back");
anchor.ServerClick += new EventHandler(AnchorServerClick);
writer.Write("<div id=\"leftnav\"><ul><li>");
anchor.RenderControl(writer);
writer.Write("</li></ul></div>");在自定义web控件内。我在anchor.RenderControl得到了一个nullReference异常,为什么?我调试了上面的代码,编写器也不是空的,锚也是。那是怎么回事?谢谢!
编辑:我添加堆栈跟踪是为了调试
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.HtmlControls.HtmlAnchor.GetPostBackOptions() +107
System.Web.UI.HtmlControls.HtmlAnchor.RenderAttributes(HtmlTextWriter writer) +10975634
System.Web.UI.HtmlControls.HtmlControl.RenderBeginTag(HtmlTextWriter writer) +56
System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +26
CER.Portal.Dashboard.Controls.BackLink.Render(HtmlTextWriter writer) +1151发布于 2012-10-23 01:05:08
查看GetPostBackOptions方法的代码,您需要将page属性设置为当前页面,或者将CausesValidation属性设置为false:
private PostBackOptions GetPostBackOptions()
{
PostBackOptions options = new PostBackOptions(this, string.Empty)
{
RequiresJavaScriptProtocol = true
};
if (this.CausesValidation && (this.Page.GetValidators(this.ValidationGroup).Count > 0))
{
options.PerformValidation = true;
options.ValidationGroup = this.ValidationGroup;
}
return options;
}在RenderControl调用前添加anchor.Page = this.Page或anchor.CausesValidation = false。
https://stackoverflow.com/questions/13015212
复制相似问题