English 中文(简体)
ASP.NET 查阅<<>>>>
原标题:ASP.NET How to access a deeply nested user control on the parent page

I have a login control and at is nested 2 deep in a header control i.e Page --> Header Control --> Login Control. I cannot get a reference to the control on the page using FindControl. I want to be able to set the visible property of the control like

  if (_loginControl != null)
            _loginControl.Visible = false;

最后,我使用了一种宽松的“FindControl”方法,以找到这种控制。

    public static Control FindControlRecursive(Control root, string id)
    {
        if (root.ID == id)
        {
            return root;
        }

        foreach (Control c in root.Controls)
        {
            Control t = FindControlRecursive(c, id);
            if (t != null)
            {
                return t;
            }
        }

        return null;
    }
最佳回答

是否需要使用户控制从伙伴关系中分离出来。 用户控制在主页上,还是存在。 如果它在同一页上,那么就在您的协会中。 NET page scode-behind You d do:

MyUserControlsID.Visible = false

在MyUserControl是用户控制网。 • 确定用户控制网的标识,看您的网页的标识,你将看到这样的内容:

<uc1:UserControlName ID="MyUserControlsID" runat="server" ... />

幸福方案

问题回答

一种很好的办法是:

Page.FindControl() 

如果这一结果无效,控制就不存在。

要求这样做。 FindControl ("_loginControl) or this. 页: 1

See MSDN for method details: http://msdn.microsoft.com/en-us/library/system.web.ui.control.findcontrol.aspx

标识控制如果在标识上登记,也将是贵代码背后页的典型成员;你可以将其从编码背后类别中称为正常成员,使用与身份识别相同的名称(我确实建议使用密码背后,而不是在标识中排入代码,BTW)。

您也可以使用你的网页“FindControl()”方法,该方法将搜索其控制子树,以便与某个特定国际发展法进行控制。 这需要更长的时间,因此,我建议采取第一个选择,除非有活力地增加逻辑控制,而且你不总是知道它在那里。

private List<Control> GetAllNestedUserControl(Control ph)
    {
        List<Control> Get = new List<Control>();
        foreach (var control in ph.Controls)
        {
            if (control is UserControl)
            {
                UserControl uc = control as UserControl;
                if (uc.HasControls())
                {
                   Get =  GetAllNestedUserControl(uc);

                }
            }
            else
            {
                Control c = (Control)control;
                if (!(control is LiteralControl))
                {
                     Get.Add(c);
                }
            }
        }
        return Get;
    }

这部法律只是从你传来的,然后由以下法典加以控制。

        List<Control> Get = GetAllNestedUserControl(ph);
        Label l = (Label)Get.Find(o => o.ID == "lblusername");
        l.Text = "changed from master";




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签