我试图找到一个Literal
控件,以便在其中插入文本。我有一个母版页,其中包含多个内容占位符。
<asp:Content ID="Content7" ContentPlaceHolderID="MainLinks" runat="server">
<h3>Project Navigation</h3>
<ul class="rightColBoxNav">
<asp:Literal ID="litNavLinks" runat="server" />
</ul>
</asp:Content>
我一直收到"对象引用未设置为对象的示例"。如何定位此对象以便查找和更新它?
我试过:
((Literal)Page.FindControl("litNavLinks")).Text = sb.ToString();
((Literal)Page.Page.FindControl("litNavLinks")).Text = sb.ToString();
((Literal)Page.FindControl("Content7").FindControl("litNavLinks")).Text = sb.ToString();
没有用我怎么确定位置
5条答案
按热度按时间mdfafbf11#
在主页中:
从视图内部:
sr4lhrrt2#
我会尝试不同的方法。
如何使用用户控件并公开相关属性来获取或设置文本值。
该属性将访问文本控件。然而,调用该属性的页面不会更明智。
记住我们生活在一个面向对象的世界。
yrdbyhpb3#
我认为你必须这样做,但我没有我的代码复查现在:
nbysray54#
ASP ContentPlaceHolder control是一个“命名容器”(它实现INamingContainer接口)。Control.FindControls method只在当前命名容器中搜索具有指定ID的控件。
我偶尔会包含一个实用函数,它接受以“/”分隔的字符串,以便在页面上的命名容器中任意导航。我没有尝试编译或测试此代码)
因此,在您的情况下,您应该能够做到以下几点:
或
kzipqqlq5#