2009-11-09 7 views
0

중첩 된 서버 컨트롤을 중첩 된 HTML을 서버 측에서 "삽입"하지 않고 받아들이는 방법을 아는 사람이 있습니까?중첩 된 서버 컨트롤로 중첩 된 HTML 허용

<uc1:CustomServerControl runat="server"> 
    <NestedControl></NestedControl> 
    <NestedControl2></NestedControl2> 
</uc1:CustomServerControl> 

해야하지만이 :

<uc1:CustomServerControl runat="server"> 
    <div> 
     <NestedControl> 
      <a href="#"></a> 
     </NestedControl> 
     <NestedControl2></NestedControl2> 
    </div> 
</uc1:CustomServerControl> 

답변

1

이 시도 :

Section.cs을 :

[ToolboxData("<{0}:Section runat=\"server\" />")] 
public class Section : WebControl, INamingContainer 
{ 
    private SectionPartCollection _parts; 

    [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty)] 
    public SectionPartCollection Parts 
    { 
     get 
     { 
      if (this._parts == null) 
      { 
       this._parts = new SectionPartCollection(); 
       if (this.IsTrackingViewState) 
        ((IStateManager)this._parts).TrackViewState(); 
      } 
      return this._parts; 
     } 
    } 

    [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty)] 
    public ITemplate LayoutTemplate { get; set; } 

    protected override void CreateChildControls() 
    { 
     base.CreateChildControls(); 

     if (this.LayoutTemplate != null) 
     { 
      this.LayoutTemplate.InstantiateIn(this); 

      foreach (SectionPart part in this.Parts) 
      { 
       Control placeHolder = this.FindControl(part.PlaceHolderID); 
       if (placeHolder != null) 
        if (part.ContentTemplate != null) 
         part.ContentTemplate.InstantiateIn(placeHolder); 
      } 
     } 
    } 

    protected override void LoadViewState(object savedState) 
    { 
     object[] states = (object[])savedState; 

     base.LoadViewState(states[0]); 

     if (states[1] != null) 
      ((IStateManager)this.Parts).LoadViewState(states[1]); 
    } 

    protected override object SaveViewState() 
    { 
     object[] states = new object[2]; 

     states[0] = base.SaveViewState(); 

     if (this._parts != null) 
      states[1] = ((IStateManager)this.Parts).SaveViewState(); 

     return states; 
    } 

    protected override void TrackViewState() 
    { 
     base.TrackViewState(); 

     if (this._parts != null) 
      ((IStateManager)this._parts).TrackViewState(); 
    } 
} 

SectionPart.cs :

[DefaultProperty("PartName")] 
public class SectionPart : IStateManager 
{ 
    private StateBag _viewState; 
    private bool _isTrackingViewState; 

    [DefaultValue("")] 
    public string PlaceHolderID 
    { 
     get { return (string)this.ViewState["PlaceHolderID"] ?? string.Empty; } 
     set { this.ViewState["PlaceHolderID"] = value; } 
    } 

    [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty)] 
    public ITemplate ContentTemplate { get; set; } 

    public void SetDirty() 
    { 
     if (this._viewState != null) 
      this.ViewState.SetDirty(true); 
    } 

    [Browsable(false)] 
    protected StateBag ViewState 
    { 
     get 
     { 
      if (this._viewState == null) 
      { 
       this._viewState = new StateBag(false); 
       if (this._isTrackingViewState) 
        ((IStateManager)this._viewState).TrackViewState(); 
      } 
      return this._viewState; 
     } 
    } 

    protected virtual bool IsTrackingViewState 
    { 
     get { return this._isTrackingViewState; } 
    } 

    protected virtual void LoadViewState(object state) 
    { 
     if (state != null) 
      ((IStateManager)this.ViewState).LoadViewState(state); 
    } 

    protected virtual object SaveViewState() 
    { 
     if (this._viewState != null) 
      return ((IStateManager)this._viewState).SaveViewState(); 
     return null; 
    } 

    protected virtual void TrackViewState() 
    { 
     this._isTrackingViewState = true; 
     if (this._viewState != null) 
      ((IStateManager)this._viewState).TrackViewState(); 
    } 

    bool IStateManager.IsTrackingViewState 
    { 
     get { return this.IsTrackingViewState; } 
    } 

    void IStateManager.LoadViewState(object state) 
    { 
     this.LoadViewState(state); 
    } 

    object IStateManager.SaveViewState() 
    { 
     return this.SaveViewState(); 
    } 

    void IStateManager.TrackViewState() 
    { 
     this.TrackViewState(); 
    } 

} 

SectionPartCollection.cs :

public class SectionPartCollection : StateManagedCollection 
{ 

    public SectionPart this[int index] 
    { 
     get { return (SectionPart)((IList)this)[index]; } 
    } 

    public void Add(SectionPart part) 
    { 
     if (part == null) 
      throw new ArgumentNullException("part"); 

     ((IList)this).Add(part); 
    } 

    public void Insert(int index, SectionPart part) 
    { 
     if (part == null) 
      throw new ArgumentNullException("part"); 

     ((IList)this).Insert(index, part); 
    } 

    public void Remove(SectionPart part) 
    { 
     if (part == null) 
      throw new ArgumentNullException("part"); 

     ((IList)this).Remove(part); 
    } 

    public void RemoveAt(int index) 
    { 
     ((IList)this).RemoveAt(index); 
    } 

    protected override void SetDirtyObject(object o) 
    { 
     ((SectionPart)o).SetDirty(); 
    } 

} 

예 :

<uc:Section ID="Section1" runat="server"> 
    <LayoutTemplate> 
     <table> 
      <tr> 
       <td id="TitlePlaceHolder" runat="server"> 
       </td> 
      </tr> 
      <tr> 
       <td id="BodyPlaceHolder" runat="server"> 
       </td> 
      </tr> 
     </table> 
    </LayoutTemplate> 
    <Parts> 
     <uc:SectionPart PlaceHolderID="TitlePlaceHolder"> 
      <ContentTemplate> 
       <span>Title</span> 
      </ContentTemplate> 
     </uc:SectionPart> 
     <uc:SectionPart PlaceHolderID="BodyPlaceHolder"> 
      <ContentTemplate> 
       <p> 
        Some content...</p> 
      </ContentTemplate> 
     </uc:SectionPart> 
    </Parts> 
</uc:Section> 
+0

나에게 아이디어를 주었다. 일단 구현되면 다시 올게. –

0

이 시도 :

[ToolboxData("..."), ParseChildren(false), PersistChildren(true)] 
public class CustomServerControl : WebControl, INamingContainer 
{ 
} 
+0

주 서버 컨트롤에 렌더링해야하는 중첩 컨트롤이 포함되어 있으므로 컨트롤 자식을 속성으로 처리하고 html로 렌더링하는 기능을 결합해야하므로 내 문제는 ParseChildren (true)을 설정해야한다는 것입니다. –

+0

제 생각에, 정확하게는 불가능합니다. 두 번째 코드 스 니펫에서 설명한 것처럼 서버 컨트롤 이 파싱 할 수없는 div의 내부 HTML에 CustomServerControl 속성을 추가합니다. 네가하고 싶은 것과 그 이유를 모르겠다. ServerControl 작업에 대해 설명하면 아마도 도움이 될 것입니다. –

0
이 basicly의 목적은

이 경우에 말하는 것 할말을 테잎에 사용자 정의 일반 태그 라이브러리를 가지고 기본 객체 모델. objectmodel은 기사를 제목, blurb, Images, Comments 등으로 나눠서 처리합니다.이 경우 나는 그것들의 모음을 가지고 있습니다. 내부 태그를 지정하면 기본적으로 필터가 적용됩니다. 섹션에서 기사의 어떤 부분을보고 싶습니까? 사용자는 그가 필요로 어떤 태그에 의해 지정 내용에 따라

<uc1:Section runat="server">  
    <HeadLine></HeadLine>  
    <Blurb></Blurb>  
    <Body></Body> 
    </uc1:Section> 

, 각각의 내용은 다음 프론트 엔드에 기록됩니다.

이제 기사의 개별 부분에 대해 일종의 내부 구조가 실제로 필요한 한 두 가지 경우를 제외하고는이 방법이 훌륭하게 잘 수행 된 다음 <table> 헤드 라인은 <td>에 들어가야하고 나머지는 다른 것으로 들어가야합니다. <td>

희망적입니다.