我有这个代码
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>");
在自定义网络控件中。我在Anchor.rendercontrol中得到了无效的例外,为什么?我调试了上述,作家也不是无效的,也不是锚。那里发生了什么?谢谢!
编辑:我正在添加用于调试目的的堆栈跟踪
[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
查看getPostbackoptions方法的代码,您要么需要将页面属性设置为当前页面,要么将casusesvalidation属性设置为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;
}
添加 anchor.page = this.page 或 anchor.causesvalidation = false 在rendercontrol调用之前。