使用 FindControl() 查找控件

发布于 2024-09-19 12:35:47 字数 684 浏览 9 评论 0原文

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

没有效果。我如何确定位置?

I have a Literal control that I am trying to locate so I can insert text into it. I have a Master page that contains several content placeholders.

<asp:Content ID="Content7" ContentPlaceHolderID="MainLinks" runat="server">
    <h3>Project Navigation</h3>
<ul class="rightColBoxNav">
<asp:Literal ID="litNavLinks" runat="server" />
</ul>
</asp:Content>

I keep getting "Object reference not set to an instance of an object." How do I locate this object so I can find and update it?

I have tried:

((Literal)Page.FindControl("litNavLinks")).Text = sb.ToString();
((Literal)Page.Page.FindControl("litNavLinks")).Text = sb.ToString();
((Literal)Page.FindControl("Content7").FindControl("litNavLinks")).Text = sb.ToString();

to no avail. How do I determine the location?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拥有 2024-09-26 12:35:47

从母版页内:

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

从视图内:

litNavLinks.Text = sb.ToString();

From within the masterpage:

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

From within the view:

litNavLinks.Text = sb.ToString();
绻影浮沉 2024-09-26 12:35:47

我会尝试不同的方法。

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

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

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

I would try a different approach.

How about using a user control and exposing the relevant properties to get or set the text value.

The property would be accessing the literal control. However, the page calling the property wouldn't be any wiser.

Remember we live in a OO world.

柠檬色的秋千 2024-09-26 12:35:47

我认为你必须这样做,但我现在没有代码可以仔细检查:

Page.Master.FindControl("MainLinks").FindControl("litNavLinks");

I think you have to do this, but I don't have my code to double-check right now:

Page.Master.FindControl("MainLinks").FindControl("litNavLinks");
神魇的王 2024-09-26 12:35:47

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();

The ASP ContentPlaceHolder control is a "naming container" (it implements the INamingContainer interface). The Control.FindControls method only searches within the current naming container for a control with the ID that you specify.

I've occassionally included a utility function that accepts a "/" delimited string to arbitrarily navigate through the naming containers on a page. Something like the following implementation. (Note: I have not tried to compile or test this code)

    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;
    }

So, in your case you should be able to do the following:

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

or

Page.FindControlByPath("MainLinks/litNavLinks").Text = sb.ToString();
乞讨 2024-09-26 12:35:47
Literal tbx = this.Controls.Find("Literal1", true).FirstOrDefault() as Literal;
Literal tbx = this.Controls.Find("Literal1", true).FirstOrDefault() as Literal;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文