gpt4 book ai didi

asp.net - 如何计算我在页面中拥有的用户控件的数量?

转载 作者:行者123 更新时间:2023-12-02 02:06:44 27 4
gpt4 key购买 nike

我有一个包含 5 个用户控件的页面。我想用它们做更多的事情,但现在,我只想使用以下方法对它们进行计数。

void btnFormSave_Click(object sender, EventArgs e)
{
int i = 0;
ControlCollection collection = Page.Controls;
foreach (Control ctr in collection)
{
if (ctr is UserControl)
{
i++;
}
}
}

当我调试这段代码时,i = 1,控件是母版页(我不知道母版页是用户控件)。

我如何计算我页面上的用户控件?

编辑

这是我的内容占位符标记。

<asp:Content ID="cntMain" runat="Server" ContentPlaceHolderID="ContentMain">

最佳答案

你只需要改变你寻找的地方:

void btnFormSave_Click(object sender, EventArgs e)
{
int i = 0;
// here, look inside the form, and not on page.
ControlCollection collection = Page.Form.Controls;
foreach (Control ctr in collection)
{
if (ctr is UserControl)
{
i++;
}
}
}

我已经测试并为我工作。

关于asp.net - 如何计算我在页面中拥有的用户控件的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14634359/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com