在 Web 表单页面中列出所有更新面板 ClientID

发布于 2024-09-11 20:08:57 字数 677 浏览 3 评论 0原文

有人知道如何在列表中读出我在页面上拥有的所有 UpdatePanel 客户端 ID 吗?基本上我需要使用 UpdatePanel 类型循环遍历页面中的所有控件,并显示每个控件的 ClientID。

我在此页面上有四个更新面板,我正在使用它

        private string LoopUpdatePanel(ControlCollection controlCollection)
    {
        StringBuilder sb = new StringBuilder();
        foreach (Control control in controlCollection)
        {
            if (control is UpdatePanel)
            {
                sb.Append(((UpdatePanel)control).ClientID + ", ");
            }

            if (control.Controls != null)
            {
                LoopUpdatePanel(control.Controls);
            }
        }
        return sb.ToString();
    }

它返回一个空字符串?

Anyone know of a way to read out in a list all of the UpdatePanel Client ID's I have on a page? Basically I need to loop through all controls in the page with a type of UpdatePanel, and display the ClientID for each..

I have four update panels on this page and I am using this

        private string LoopUpdatePanel(ControlCollection controlCollection)
    {
        StringBuilder sb = new StringBuilder();
        foreach (Control control in controlCollection)
        {
            if (control is UpdatePanel)
            {
                sb.Append(((UpdatePanel)control).ClientID + ", ");
            }

            if (control.Controls != null)
            {
                LoopUpdatePanel(control.Controls);
            }
        }
        return sb.ToString();
    }

It returns an empty string??

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

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

发布评论

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

评论(4

久光 2024-09-18 20:08:57

不确定您使用的是什么语言或您尝试过什么...请给我们一些代码,以便我们知道您尝试过什么,也许我们会发现错误。

但作为示例,这里介绍了如何循环控制。只需使用您想要的属性而不是文本(在您的情况下 ID 值和更新面板)。我相信获取控件的重要部分是遍历整个控件层次结构,这样您就不会错过任何子级、孙级等。

    private void Page_Load(object sender, System.EventArgs e)
{
    LoopTextboxes(Page.Controls);
}

private void LoopTextboxes(ControlCollection controlCollection)
{
    foreach(Control control in controlCollection)
    {
        if(control is TextBox)
        {
            ((TextBox)control).Text = "I am a textbox";
        }

        if(control.Controls != null)
        {
            LoopTextboxes(control.Controls);
        }
    }
}

Not sure what language you're using or what you've tried...Please give us some code so we know what you have tried maybe we'll catch the error.

but as an example here's how you'd loop through controls. Just use the properties you want instead of text (in your case Id value and update panels). I believe the important part of acquiring controls is to go through the entire control hierarchy so you don't miss any children, grand children etc.

    private void Page_Load(object sender, System.EventArgs e)
{
    LoopTextboxes(Page.Controls);
}

private void LoopTextboxes(ControlCollection controlCollection)
{
    foreach(Control control in controlCollection)
    {
        if(control is TextBox)
        {
            ((TextBox)control).Text = "I am a textbox";
        }

        if(control.Controls != null)
        {
            LoopTextboxes(control.Controls);
        }
    }
}
爱的故事 2024-09-18 20:08:57

听起来像是一个正确的逻辑,也许您可​​能正在搜索一组不同的控件?

您是否尝试过使用调试单步执行并检查 ID?


 protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string result = "result = ";

            foreach (Control tmpControl in Page.Controls)
            {
                Type tmpType = tmpControl.GetType();

                if (tmpControl is SiteMaster)
                {
                    foreach (Control SiteMasterControlItem in tmpControl.Controls)
                    {
                        if (SiteMasterControlItem is System.Web.UI.HtmlControls.HtmlForm)
                        {
                            int i = 0;

                            for( i =0;i < SiteMasterControlItem.Controls.Count; i++)
                            {
                                Type tmpType2 = SiteMasterControlItem.Controls[i].GetType();
                            }
                        }


                    }
                }
            }

            Response.Write(result);
        }

        catch(Exception ex)
        {
            Response.Write("error = " + ex.StackTrace);
        }
    }

最后一个循环包含 contentplace 持有者。你可能需要更深入。
这是在 VS2010 上用 c#4.0

HTH尝试过的

Sounds like a proper logic, maybe you might be searching through a different set of controls?

Have you tried to step through using debug and check the IDs?


 protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string result = "result = ";

            foreach (Control tmpControl in Page.Controls)
            {
                Type tmpType = tmpControl.GetType();

                if (tmpControl is SiteMaster)
                {
                    foreach (Control SiteMasterControlItem in tmpControl.Controls)
                    {
                        if (SiteMasterControlItem is System.Web.UI.HtmlControls.HtmlForm)
                        {
                            int i = 0;

                            for( i =0;i < SiteMasterControlItem.Controls.Count; i++)
                            {
                                Type tmpType2 = SiteMasterControlItem.Controls[i].GetType();
                            }
                        }


                    }
                }
            }

            Response.Write(result);
        }

        catch(Exception ex)
        {
            Response.Write("error = " + ex.StackTrace);
        }
    }

The last loop contains the contentplace holder. You might have to go deeper.
This was tried on VS2010 with c#4.0

HTH

抹茶夏天i‖ 2024-09-18 20:08:57

我认为它是一个包含母版页内容的控件
可能是 c# 4.0 的新手
只需忽略它并使用内循环即可。
只需浏览一下您目前拥有的所有控件并将其全部打印出来即可。

此外,您在那里得到的方法没有正确返回值,您被称为没有返回值的方法,而不是签名,但是当您调用像 LoopUpdatePanel(control.Controls); 这样的方法时

It is a control that contains master page contents I think
Could be new to c# 4.0
Just Ignore it and use inner loop.
Just go through all the controls you have at the moment and print it all out.

Also the method you got up there is not returning values correctly, you are called the method without return values, not the signature but when you call the method like LoopUpdatePanel(control.Controls);

海螺姑娘 2024-09-18 20:08:57
if (_contrl is UpdatePanel)
{

UpdatePanel _cntrl = (UpdatePanel)_contrl;

foreach (Control ctr in _cntrl.ContentTemplateContainer.Controls)
{

if (ctr is TextBox)
{

 // Do Work Here 

}
}

}
if (_contrl is UpdatePanel)
{

UpdatePanel _cntrl = (UpdatePanel)_contrl;

foreach (Control ctr in _cntrl.ContentTemplateContainer.Controls)
{

if (ctr is TextBox)
{

 // Do Work Here 

}
}

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