본문 바로가기

.NET/ASP.NET

중첩 마스터 페이지의 FindControl로 컨트롤 찾기 - FindControlRecursive Method 만들기

반응형
먼저 그림 설명
 

위의 화면은 중첩 마스터 페이지중에 서브 마스터 페이지의 컨트롤 들이다 

 


위 화면은 Default페이지의 구현 화면




전체 소스
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        (FindControlRecursive(this.Master.Master, "Button1") as Button).Text = "great";
    }

    public static Control FindControlRecursive(Control Root, string Id)
    {
        if (Root.ID == Id)
            return Root;

        foreach (Control Ctl in Root.Controls)
        {
            Control FoundCtl = FindControlRecursive(Ctl, Id);
            if (FoundCtl != null)
                return FoundCtl;
        }

        return null;
    }

}



기존에 쓰던 방식~!


아래는 내가 삽질로 알아낸 기존의 방식이였다 ㅎㅎ ㅡ.ㅡ;;


ex) 
 
((Label)Master.Master.FindControl("ContentPlaceHolder1").
FindControl("JoinInfo1").FindControl("Label1")).Font.Bold = true;