我在那里有这段代码
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异常,为什么?我调试了上面的内容并且编写器不是null,也是锚点。那里发生了什么?谢谢!
编辑:我正在添加堆栈跟踪以进行调试
[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
答案 0 :(得分:1)
查看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 。