asp.net 使用FindControl()查找控件

q0qdq0h2  于 2023-02-06  发布在  .NET
关注(0)|答案(5)|浏览(201)

我试图找到一个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();

没有用我怎么确定位置

mdfafbf1

mdfafbf11#

在主页中:

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

从视图内部:

litNavLinks.Text = sb.ToString();
sr4lhrrt

sr4lhrrt2#

我会尝试不同的方法。
如何使用用户控件并公开相关属性来获取或设置文本值。
该属性将访问文本控件。然而,调用该属性的页面不会更明智。
记住我们生活在一个面向对象的世界。

yrdbyhpb

yrdbyhpb3#

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

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

nbysray54#

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

kzipqqlq5#

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

相关问题