使用FindControl()来查找控件

时间:2010-09-16 21:34:20

标签: c# asp.net findcontrol

我正在尝试找到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 个答案:

答案 0 :(得分:12)

在主页内:

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

从视图中:

litNavLinks.Text = sb.ToString();

答案 1 :(得分:1)

我会尝试不同的方法。

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

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

请记住,我们生活在OO世界。

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

ASP ContentPlaceHolder control是一个“命名容器”(它实现了INamingContainer接口)。 Control.FindControls method仅在当前命名容器中搜索具有您指定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();

答案 4 :(得分:-1)

Literal tbx = this.Controls.Find("Literal1", true).FirstOrDefault() as Literal;