2009-12-16 2 views
3

제목 특성을 허용하는 사용자 정의 컨트롤이 있습니다. 또한 해당 사용자 컨트롤 태그 내부에 해당 내부 HTML (ASP 컨트롤도)을 입력하고 싶습니다.ASP.NET 사용자 정의 내부 내용

<uc:customPanel title="My panel"> 
    <h1>Here we can add whatever HTML or ASP controls we would like.</h1> 
    <asp:TextBox></asp:TextBox> 
</uc:customPanel> 

어떻게 구현할 수 있습니까? 제목 속성이 제대로 작동합니다.

감사합니다.

+0

나는이 작업을 수행하지 못할 생각 ...하지만 요는 조금하시기 바랍니다 지정할 수 있습니다 UR 사용자 컨트롤 –

+0

의의 .ascx에서 그것을 할 수 있습니까? 내가 잘 작동하는 내 ascx 파일에 <%=title%>과 <%=content%>을 가지고 있지만 그것은 사용자 정의 HTML을 content 속성에 전달하는 고통입니다. – ademers

+0

System.Web.UI.UserControl에 속성 텍스트 상자가 없습니다. –

답변

7

하는 INamingContainer를 패널을 확장 및 구현하는 클래스 구현 : 방법에 다음

public Container ContainerContent 
{ 
    get 
    { 
     EnsureChildControls(); 
     return content; 
    } 
} 
[TemplateContainer(typeof(Container))] 
[TemplateInstance(TemplateInstance.Single)] 
public virtual ITemplate Content 
{ 
    get { return templateContent; } 
    set { templateContent = value; } 
} 

:

다음
public class Container: Panel, INamingContainer 
{ 
} 

, 당신의 CustomPanel 유형 컨테이너의 속성과 타입 ITemplate의 또 다른 속성을 노출 할 필요를 CreateChildControls()을 다음에 추가하십시오.

if (templateContent != null) 
{ 
    templateContent.InstantiateIn(content); 
} 

<uc:customPanel title="My panel"> 
    <Content>  
     <h1>Here we can add whatever HTML or ASP controls we would like.</h1> 
     <asp:TextBox></asp:TextBox> 
    </Content> 
</uc:customPanel> 
0

EnsureChildControls가 호출되는지 확인해야합니다. CreateChildControls 기본 메서드를 사용하는 등 여러 가지 방법이 있지만 간단하게 렌더링 할 사용자 지정 컨트롤의 내부 내용을 가져올 수 있습니다. 상태를 기억하고 이벤트를 트리거해야 할 때 더 복잡해 지지만 일반적인 HTML의 경우에는 이것이 작동합니다.

protected override void Render(HtmlTextWriter writer) { 
    EnsureChildControls(); 
    base.Render(writer); 
} 
관련 문제