13

我有一个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();

无济于事。如何确定位置?

4

5 回答 5

12

从母版页中:

var literal = (Literal)FindControl("MainLinks").FindControl("litNavLinks");
literal.Text = sb.ToString();

从视图内部:

litNavLinks.Text = sb.ToString();
于 2010-09-16T21:40:38.680 回答
1

我会尝试不同的方法。

如何使用用户控件并公开相关属性来获取或设置文本值。

该属性将访问文字控件。但是,调用该属性的页面不会更明智。

请记住,我们生活在一个面向对象的世界中。

于 2010-09-16T21:48:09.280 回答
1

我认为您必须这样做,但我现在没有要仔细检查的代码:

Page.Master.FindControl("MainLinks").FindControl("litNavLinks");
于 2010-09-16T21:50:58.337 回答
1

ASP ContentPlaceHolder 控件是一个“命名容器”(它实现了INamingContainer 接口)。Control.FindControls 方法仅在当前命名容器中搜索具有您指定的 ID 的控件。

我偶尔会包含一个实用程序函数,它接受“/”分隔的字符串来任意导航页面上的命名容器。类似于以下实现。(注意:我没有尝试编译或测试此代码)

    public static Control FindControlByPath(this Control start, string controlPath)
    {
        if(controlPath == null)
            throw new ArgumentNullException("controlPath");

        string[] controlIds = controlPath.split('/');

        Control current = start;
        if(controlIds[0] == "") // in case the control path starts with "/"
            current = start.Page; // in that case, start at the top

        for(int i=0; i<controlIds.Length; i++)
        {
            switch(controlIds[i])
            {
                case "":
                    // TODO: handle syntax such as "<controlId>//<controlId>", if desired
                    break;

                case ".":
                    // do nothing, stay on the current control
                    break;

                case "..":
                    // navigate up to the next naming container
                    current = current.Parent;
                    if(current == null)
                        throw new ArgumentOutOfRangeException("No parent naming container exists.", "controlPath");

                    while(!(current is INamingContainer))
                    {
                        current = current.Parent;
                        if(current == null)
                            throw new ArgumentOutOfRangeException("No parent naming container exists.", "controlPath");
                    }                       
                    break;

                default:
                    current = current.FindControl(controlIds[i]);
                    break;
            }
        }

        return current;
    }

因此,在您的情况下,您应该能够执行以下操作:

<some control>.FindControlByPath("/MainLinks/litNavLinks").Text = sb.ToString();

或者

Page.FindControlByPath("MainLinks/litNavLinks").Text = sb.ToString();
于 2010-09-16T21:57:02.073 回答
-1
Literal tbx = this.Controls.Find("Literal1", true).FirstOrDefault() as Literal;
于 2016-01-19T11:04:00.267 回答