asp.net控件 [英] asp.net controls

查看:66
本文介绍了asp.net控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,该网页首先加载母版页,然后加载当前页.我想获取页面上所有可用的控件名称.
当我使用this.controls或this.form.controls我从母版页只获得控件.当前页面控件不可用..但是当我逐行运行时..它显示"this"对象中的所有控件..如何从"this"对象中获取所有名称

thak u ...........

i have a web page which first loads master page and then the current page. I want to get all the names of the controls availble on the page.
when i used this.controls or this.form.controls Im getting only the controls from Master page. The current page controls is not vailable.. but when i run line by line..it shows all the controls in the "this" object.. how to get all the names from the "this" object

thak u...........

推荐答案

您之前曾问过这个问题,并且给了它...

You asked this before, and this was given...

foreach (Control ctrl in this.Controls)
       {
       }



*感谢Sunasara Imdadhusen


我建议您在第三次询问之前先尝试一下.



*With thanks to Sunasara Imdadhusen


I suggest you try it before asking for a third time.


我对上一个问题发布了相同的答案.如果您要在已经问到的问题中添加任何内容,请进行修改.在重新发布之前.

页面上的基本控件(使用MasterPages)是MasterPage然后是HtmlForm然后是ContentPlaceHolder,然后最终将出现所有标签,文本框以及您要查找的其他任何控件(请注意,如果这些控件包含自己的子控件,例如GridView,您将需要遍历该控件以获取其中包含的所有控件).

I posted same answer to your previous question.I would suggest,if you want to add anything to already asked question,please modify it before reposting it.

The basicic heirachy of controls on a page (which uses MasterPages) is MasterPage then HtmlForm then ContentPlaceHolder and then finally will come all the labels, textboxes, and whatever other controls you seek (note that if these controls contain children of their own, like a GridView, you will need to loop through that control to get all the included controls in it).

foreach (Control masterControl in Page.Controls)
{
    if (masterControl is MasterPage)
    {
        foreach (Control formControl in masterControl.Controls)
        {
            if (formControl is System.Web.UI.HtmlControls.HtmlForm)
            {
                foreach (Control contentControl in formControl.Controls)
                {
                    if (contentControl is ContentPlaceHolder)
                    {
                        foreach (Control childControl in contentControl.Controls)
                        {

                        }
                    }
                }
            }
        }
    }
}


For a specific control use this

if (childControl is Label)
{
    Label newLabel = (Label)childControl;
    newLabel.Text = "You found me!"
}



资料来源:

http://www.krissteele.net/blogdetails.aspx?id=104 [ ^ ]



Source :

http://www.krissteele.net/blogdetails.aspx?id=104[^]


尝试一下...

Try this...

private void LoopControls(System.Web.UI.Control ctlParent)
{
    foreach (Control ctrl in ctlParent.Controls)
    {
        if (ctrl.HasControls())
        {
            ///Do Something
            LoopControls(ctrl);
        }
        else
        {
            ///Do Something
        }
    }
}


这篇关于asp.net控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆