본문 바로가기

SharePoint/SPS2010

Sharepoint 에서 Postback 문제로 Findcontrol 리턴 값이 null 일때

참조 : http://www.west-wind.com/weblog/posts/2006/Apr/09/ASPNET-20-MasterPages-and-FindControl

동적 테이블이나 동적으로 데이타를 읽어 올때 필요한 FindControl 이 Sharepoint에서 안될때가 있습니다.

이럴 때는 아래의 매서드를 이용하여 사용하면 됩니다.

 

/// <summary>

/// Finds a Control recursively. Note finds the first match and exists

/// </summary>

/// <param name="ContainerCtl"></param>

/// <param name="IdToFind"></param>

/// <returns></returns>

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;

}

 

ASP.NET에서 MasterPage 를 사용할때

/// <summary>

/// Assigns controls from the subclassed control to this instance so

/// we always can access the controls in our base class.

/// </summary>

protected void AssignControls()

{

this.lblError = wwWebUtils.FindControlRecursive(this.Master,"lblError") as Label;

this.dgItemList = wwWebUtils.FindControlRecursive(this.Master, "dgItemList") as DataGrid;

}

 

 

Sharepoint 경우는 TextBox txtTest = FindcontrolRecursive(this.Page.Master, "txtTest1") as TextBox