2009-07-21 62 views
1

我有一個服務器控件,繼承自PlaceHolder。實現IPostbackHandler .net

基本上,它是一個佔位符,頂部有一個"<div class etc...",底部關閉。

因此,典型的用法是

<control:control runat="server" id="phControl"> 
    <asp:TextBox runat="server" id="txtControl"> 
    <asp:DropDownList runat="server"id="ddlControl"> 
</control:control> 

或類似的東西。

它讓我很震驚,如果我回發到控件,它會丟失ddlControl中的所有項目(或其他),並且執行IPostBackHandler顯然會解決我所有的問題。

我不得不通過文檔快速瀏覽,但我還是沒有我實現真正知道什麼(很明顯,我有方法的名字,但我真的不明白是什麼在這裏預期)

任何指針在正確的方向將不勝感激。

感謝,

回答

1

它看起來像你只是想可以包含其他控件或「模板」的服務器控制,我剛纔已經做到了這一點用在例如:http://msdn.microsoft.com/en-us/library/ms178657.aspx

這應該處理所有在回發上完成的工作。

一個基本的例子改編自上面的鏈接:

using System; using System.ComponentModel; using System.Security.Permissions; using System.Web; using System.Web.UI; using System.Web.UI.Design; using System.Web.UI.WebControls;

命名空間Made4Print.Web.UI { [性AspNetHostingPermission(SecurityAction.InheritanceDemand,級別= AspNetHostingPermissionLevel.Minimal),性AspNetHostingPermission(SecurityAction.Demand,級別= AspNetHostingPermissionLevel.Minimal),設計師(typeof運算(VacationHomeDesigner)),DefaultProperty( 「標題」),ToolboxData(「< {0}:TemplateContainer runat = \」server \「>」),] public class TemplateContainer:CompositeControl { private ITemplate templateValue; 私人TemplateOwner ownerValue;

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 
    public TemplateOwner Owner 
    { 
     get 
     { 
      return ownerValue; 
     } 
    } 

    [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), DefaultValue(typeof(ITemplate), ""), Description("Control template"), TemplateContainer(typeof(TemplateContainer))] 
    public virtual ITemplate Template 
    { 
     get 
     { 
      return templateValue; 
     } 
     set 
     { 
      templateValue = value; 
     } 
    } 

    protected override void CreateChildControls() 
    { 
     Controls.Clear(); 
     ownerValue = new TemplateOwner(); 

     ITemplate temp = templateValue; 
     if (temp == null) 
     { 
      temp = new DefaultTemplate(); 
     } 

     temp.InstantiateIn(ownerValue); 
     this.Controls.Add(ownerValue); 
    } 

    public override void DataBind() 
    { 
     CreateChildControls(); 
     ChildControlsCreated = true; 
     base.DataBind(); 
    } 

} 

[ToolboxItem(false)] 
public class TemplateOwner : WebControl 
{ 
} 

#region DefaultTemplate 
sealed class DefaultTemplate : ITemplate 
{ 
    void ITemplate.InstantiateIn(Control owner) 
    { 
     // Create Controls Here 
     //Label title = new Label(); 
     //title.DataBinding += new EventHandler(title_DataBinding); 
     //owner.Controls.Add(title); 
    } 

    //void title_DataBinding(object sender, EventArgs e) 
    //{ 
    // Label source = (Label)sender; 
    // TemplateContainer container = (TemplateContainer)(source.NamingContainer); 
    // source.Text = container.Title; 
    //} 
} 
#endregion 


public class VacationHomeDesigner : ControlDesigner 
{ 

    public override void Initialize(IComponent Component) 
    { 
     base.Initialize(Component); 
     SetViewFlags(ViewFlags.TemplateEditing, true); 
    } 

    public override string GetDesignTimeHtml() 
    { 
     return "<span>[Template Container Control]</span>"; 
    } 

    public override TemplateGroupCollection TemplateGroups 
    { 
     get 
     { 
      TemplateGroupCollection collection = new TemplateGroupCollection(); 
      TemplateGroup group; 
      TemplateDefinition template; 
      TemplateContainer control; 

      control = (TemplateContainer)Component; 
      group = new TemplateGroup("Item"); 
      template = new TemplateDefinition(this, "Template", control, "Template", true); 
      group.AddTemplateDefinition(template); 
      collection.Add(group); 
      return collection; 
     } 
    } 


} 

}