for 循环内的 UserControl 中的 FindControl()

发布于 2024-09-02 01:17:10 字数 633 浏览 4 评论 0原文

我正在创建一个具有一系列 LinkBut​​ton 的用户控件。

我已经在类的顶部声明了所有链接按钮,

LinkButton LB1 = new LinkButton();
LinkButton LB2 = new LinkButton();
//...
LinkButton LB9 = new LinkButton();

现在我希望能够创建一个循环来访问所有这些链接按钮,这样我就不必每次都将它们全部写出来。

我在重写的 CreateChildControls() 方法中尝试了类似的方法:

for (int i = 1; i < 10; i++)
        {
            LinkButton lb = (LinkButton)FindControl("LB" + i.ToString());
            lb.Text = i.ToString() + "-Button";
        }

我不断收到异常,说 lb.Text... 未设置为对象的实例。

我还尝试提供所有 LB1、LB2 等有效 ID。

即:LB1.ID =“LB1”;

仍然没有骰子。

我该怎么做?

I'm creating a usercontrol that will have a series of LinkButtons.

I've declared all of my link buttons at the top of my class

LinkButton LB1 = new LinkButton();
LinkButton LB2 = new LinkButton();
//...
LinkButton LB9 = new LinkButton();

now I'd like to be able to create a loop to access all of those link buttons so I don't have to write them all out every time.

I tried something like this within my overridden CreateChildControls() method:

for (int i = 1; i < 10; i++)
        {
            LinkButton lb = (LinkButton)FindControl("LB" + i.ToString());
            lb.Text = i.ToString() + "-Button";
        }

I keep getting an exception saying that lb.Text... is not set to an instance of an object.

I also tried giving all of my LB1, LB2 etc valid Ids.

ie: LB1.ID = "LB1";

still not dice.

how can I do this?

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

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

发布评论

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

评论(5

z祗昰~ 2024-09-09 01:17:10

FindControl 仅在将这些控件添加到 Controls 集合中后才起作用,并且仅发生在 OnInit 方法内。因此,您会收到异常,因为 LB1、LB2 等控件尚未添加到 Controls 集合中,并且 FindControl 返回 null

一种方法是使用 List,然后在 Init 事件处理程序中将控件添加到列表中。

另一种方法是,您可以使用 LINQ 循环访问您的子控件:

var ctrls = Controls.OfType<LinkButton>();

此版本将返回所有 LinkBut​​ton 控件,因此我不确定这是否正是您想要的。同样,这仅在 Init 事件或页面周期的稍后阶段起作用。

此外

根据页面的结构,您可能最好使用中继器 控件。在您的 .aspx/ascx 文件中,类似这样的内容:

<asp:Repeater ID="repeater" runat="server">
    <ItemTemplate>
        <asp:LinkButton ID="btn" runat="server" />
    </ItemTemplate>
</asp:Repeater>

然后在后面的代码中,您将使用数据绑定来设置数组等。

FindControl only works once those controls have been added to Controls collection, and that only happens inside the OnInit method. So you're getting an exceptions because the LB1, LB2, etc controls haven't been added to the Controls collection and FindControl is returning null.

One way you could do it is have a List<LinkButton>, then in your Init event handler, add the controls to the list.

Another way, you could use LINQ to loop through your child controls:

var ctrls = Controls.OfType<LinkButton>();

This version would return all LinkButton controls, so I'm not sure if that's exactly what you want. Again, this would only work in the Init event or later in the page cycle.

Additionally

Depending on how your page is structure, you might be better off using a Repeater control. Something like this on your .aspx/ascx file:

<asp:Repeater ID="repeater" runat="server">
    <ItemTemplate>
        <asp:LinkButton ID="btn" runat="server" />
    </ItemTemplate>
</asp:Repeater>

Then in your code behind, you'd use data-binding to set up the array and so on.

原谅过去的我 2024-09-09 01:17:10

它肯定找到了 LinkBut​​ton 吗?在我看来,FindControl 实际上并没有找到 LinkBut​​ton。

连接调试器并检查 lb 实际上不为空。

is it definitely finding the LinkButton? It looks to me like FindControl isn't actually finding the LinkButton.

Hook up the debugger and check that lb isn't actually null.

很糊涂小朋友 2024-09-09 01:17:10

我认为 FindControl 仅返回直接容器内的控件,因此如果您将它们放在 DIV 元素或表中,那么它将找不到它们。您可以使用类似于以下内容的辅助函数:

public static Control FindControl(Control start, string id)
{
  Control foundControl;
  if (start != null) 
  {
    foundControl = start.FindControl(id);
    if (foundControl != null) 
      return foundControl;

    foreach (Control c in start.Controls) 
    {
      foundControl = FindControl(c, id);
      if (foundControl != null) 
        return foundControl;
    }
  }
  return null;
}

然后您可以将此容器或特定容器作为启动参数传递。

I think FindControl only returns controls within an immediate container, so if you have them inside of say a DIV element or a table, then it will not find them. You can use a helper function similar to the following:

public static Control FindControl(Control start, string id)
{
  Control foundControl;
  if (start != null) 
  {
    foundControl = start.FindControl(id);
    if (foundControl != null) 
      return foundControl;

    foreach (Control c in start.Controls) 
    {
      foundControl = FindControl(c, id);
      if (foundControl != null) 
        return foundControl;
    }
  }
  return null;
}

You can then pass this or a specific container as a start parameter.

开始看清了 2024-09-09 01:17:10

像这样的事情怎么样:

    int btnCount = 10;
    LinkButton[] btns = new LinkButton[btnCount];
    for (int i = 1; i <= btnCount; i++)
    {
        btns[i - 1] = new LinkButton(){
            Text = string.Format("{0} - Button", i) 
        };
    }

What about something like this:

    int btnCount = 10;
    LinkButton[] btns = new LinkButton[btnCount];
    for (int i = 1; i <= btnCount; i++)
    {
        btns[i - 1] = new LinkButton(){
            Text = string.Format("{0} - Button", i) 
        };
    }
噩梦成真你也成魔 2024-09-09 01:17:10

正如 @Alek 和 @codeka 提到的,FindControl 仅在添加到容器(例如 Page 对象)后才起作用。

您可以做的不是将它们分别声明为变量,而是将它们添加到列表中,如下所示:

var linkButtonList = new List<LinkButton>();
linkButtonList.Add(new LinkButton());
linkButtonList.Add(new LinkButton());
etc..

然后,每当需要访问所有 LinkBut​​ton 控件时,您都可以轻松地循环遍历列表:

foreach (var item in linkButtonList)
{
    // do something with the LinkButton here...
}

As @Alek and @codeka mentioned, FindControl will only work once they have been added to a container, such as a Page object.

What you can do is instead of declaring them each as variables, add them to a List instead, like so:

var linkButtonList = new List<LinkButton>();
linkButtonList.Add(new LinkButton());
linkButtonList.Add(new LinkButton());
etc..

then you can loop through the List easily whenever you need to access all of the LinkButton controls:

foreach (var item in linkButtonList)
{
    // do something with the LinkButton here...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文