开发者

How to create USER CONTROL like panel

I want create a user control (Div) like panel to be used in all my web pages. For e开发者_StackOverflow社区xample, The UserControl can have two DIVs or TRs with a background design. If it's added to a Page, the UserControl has to allow controls to be added into it, i.e. Divs, Labels, and TextBoxes. The height of the div should be AUTO. Please help me.


Add the following property to your UserControl:

[PersistenceMode(PersistenceMode.InnerProperty)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[TemplateInstance(TemplateInstance.Single)]
public ITemplate Content
{
    get
    {
        return _content;
    }
    set
    {
        _content = value;
    }
}
private ITemplate _content;

Then handle the the Content in the CreateChildControls method:

protected override void CreateChildControls()
{
    if (this.Content != null)
    {
        this.Content.InstantiateIn(this);
    }
    base.CreateChildControls();
}

In markup you can now add anything you want into the Content tags of the Control:

<ctl:YourControl runat="server" ID="Foo" style="background-color:#666">
   <Content>
      <asp:Label ... />
      <asp:TextBox... />
   </Content>
</ct1:YourControl>


write the control panel code on a different page.

on the pages check if its needed and include the control panel. if there are other elements on the page which will need special controls.

you can do a if() and add the controls there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜