How to access page controls from user control?

后端 未结 4 1986
长情又很酷
长情又很酷 2020-12-18 10:08

Is there a way to access page controls from user control . I have some controls in my page and i want to access these controls from the user control .

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-18 10:34

    YourControlType ltMetaTags = null;
    Control ctl = this.Parent;
    while (true)
    {
        ltMetaTags = (ControlType)ctl.FindControl("ControlName");
        if (ltMetaTags == null)
        {
            ctl = ctl.Parent;
            if(ctl.Parent == null)
            {
                return;
            }
            continue;
        }
        break;
    }
    

    Example

    System.Web.UI.WebControls.Literal ltMetaTags = null;
    Control ctl = this.Parent;
    while (true)
    {
        ltMetaTags = (System.Web.UI.WebControls.Literal)ctl.FindControl("ltMetaTags");
        if (ltMetaTags == null)
        {
            if(ctl.Parent == null)
            {
                return;
            }
            ctl = ctl.Parent;
            continue;
        }
        break;
    }
    

提交回复
热议问题