2012-02-13 5 views
1

다른 컨트롤의 관계를 알게되면 멋진 것들을 할 사용자 지정 컨트롤이 있습니다. 이 기사를 연결하는 방법은 다음과 같습니다. 더 나은 방법을 알고 있다면 제안에 대해 공개합니다.asp.net에서 내 사용자 정의 WebControl을 찾을 수없는 이유는 무엇입니까?

먼저 인터페이스를 만든 다음 관계를 관리하는 컨트롤을 만들었습니다. 그런 다음

public class CustomDDL : DropDownList, IRegisterSelf 
{ 
    public string ParentId { get; set; } 

    private ICustomControl _customControl; 

    public string CustomControlId 
    { 
     get 
     { 
      return ((Control)_customControl).ID; 
     } 
     set 
     { 
      _customControl = (ICustomControl)this.FindControl(value); 
      RegisterToControl(_customControl); 
     } 
    } 

    public void RegisterToControl(ICustomControl controller) 
    { 
     if (string.IsNullOrEmpty(ParentId)) 
      controller.Register(this, null); 
     else 
      controller.Register(this, (IRegisterSelf)FindControl(ParentId)); 
    } 
} 

모든 관계를 정의 할 수있는 마크 업 :

<c:CustomControl ID="myControl" runat="server" /> 

<c:CustomDDL ID="box1" CustomControlId="myControl" runat="server"> 
    <asp:ListItem Text="_value1" Value="Value 1" /> 
    <asp:ListItem Text="_value2" Value="Value 2" /> 
    <asp:ListItem Text="_value3" Value="Value 3" /> 
</c:CustomDDL> 

<c:CustomDDL ID="box2" ParentId="box1" CustomControlId="myControl" runat="server"> 
    <asp:ListItem Text="_value1" Value="Value 1" /> 
    <asp:ListItem Text="_value2" Value="Value 2" /> 
    <asp:ListItem Text="_value3" Value="Value 3" /> 
</c:CustomDDL> 

문제는이다,에

public interface IRegisterSelf 
{ 
    string ParentId { get; set; } 
    string CustomControlId { get; set; } 
    void RegisterToControl(ICustomControl controller); 
} 

public interface ICustomControl 
{ 
    void Register(IRegisterSelf child, IRegisterSelf parent); 
} 

public class CustomControl : WebControl, ICustomControl 
{ 
    public List<KeyValuePair<IRegisterSelf, IRegisterSelf>> _relationShips 
     = new List<KeyValuePair<IRegisterSelf, IRegisterSelf>>(); 

    public void Register(IRegisterSelf child, IRegisterSelf parent) 
    { 
     _relationShips.Add(new KeyValuePair<IRegisterSelf, IRegisterSelf>(parent, child)); 
    } 
} 

그 후, 나는 IRegisterSelf 인터페이스에의 부착 다른 사용자 지정 컨트롤을 생성 CustomDDL의 CustomControlId 속성은 asp.net이 찾지 못하기 때문에 컨트롤러를 등록 할 수 없습니다. FindControl은 항상 null을 반환합니다. 왜? 나는 ID를 설정하고 runat 속성을 server로 설정했습니다. 나는 생성 된 HTML에서도 그것을 볼 수있다. 어떤 도움을 주시면 감사하겠습니다.

+0

FindControl을 사용하는 장소와 제어 할 장소를 표시하십시오. –

답변

1

FindControl은 재귀 적으로 페이지의 컨트롤을 찾지 않습니다. 수정 방법은 here을 참조하십시오.

관련 문제