for 循环内的 UserControl 中的 FindControl()
我正在创建一个具有一系列 LinkButton 的用户控件。
我已经在类的顶部声明了所有链接按钮,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
FindControl
仅在将这些控件添加到Controls
集合中后才起作用,并且仅发生在OnInit
方法内。因此,您会收到异常,因为 LB1、LB2 等控件尚未添加到 Controls 集合中,并且 FindControl 返回null
。一种方法是使用
List
,然后在Init
事件处理程序中将控件添加到列表中。另一种方法是,您可以使用 LINQ 循环访问您的子控件:
此版本将返回所有
LinkButton
控件,因此我不确定这是否正是您想要的。同样,这仅在Init
事件或页面周期的稍后阶段起作用。此外
根据页面的结构,您可能最好使用中继器 控件。在您的 .aspx/ascx 文件中,类似这样的内容:
然后在后面的代码中,您将使用数据绑定来设置数组等。
FindControl
only works once those controls have been added toControls
collection, and that only happens inside theOnInit
method. So you're getting an exceptions because the LB1, LB2, etc controls haven't been added to the Controls collection and FindControl is returningnull
.One way you could do it is have a
List<LinkButton>
, then in yourInit
event handler, add the controls to the list.Another way, you could use LINQ to loop through your child controls:
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 theInit
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:
Then in your code behind, you'd use data-binding to set up the array and so on.
它肯定找到了 LinkButton 吗?在我看来,FindControl 实际上并没有找到 LinkButton。
连接调试器并检查 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.
我认为 FindControl 仅返回直接容器内的控件,因此如果您将它们放在 DIV 元素或表中,那么它将找不到它们。您可以使用类似于以下内容的辅助函数:
然后您可以将此容器或特定容器作为启动参数传递。
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:
You can then pass this or a specific container as a start parameter.
像这样的事情怎么样:
What about something like this:
正如 @Alek 和 @codeka 提到的,FindControl 仅在添加到容器(例如 Page 对象)后才起作用。
您可以做的不是将它们分别声明为变量,而是将它们添加到列表中,如下所示:
然后,每当需要访问所有 LinkButton 控件时,您都可以轻松地循环遍历列表:
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:
then you can loop through the List easily whenever you need to access all of the LinkButton controls: