开发者

Can I Re-Use Common Html in an ASP.NET Child Control?

开发者 https://www.devze.com 2023-02-08 09:45 出处:网络
I have 5 or so different pieces of HTML in my page that contain the same scaffolding HTML surrounding it, something like this:

I have 5 or so different pieces of HTML in my page that contain the same scaffolding HTML surrounding it, something like this:

//PanelBase.ascx

<div class="panel" id="[PANEL-SPECIFIC-ID]">
    <h3>[开发者_高级运维PANEL-SPECIFIC-HEADER]</h3>
        ...
        [PANEL-SPECIFIC-HTML]
        ...
    </h3>
</div>

Where all the PANEL-SPECIFIC things are different for each panel type. Is there a way I can create a common base control to handle this scaffolding and inherit from it to supply the PANEL-SPECIFIC-HTML? The PANEL-SPECIFIC-ID and PANEL-SPECIFIC-HEADER I can just pass to the panel directly, but since the panel specific HTML is so large I don't want to pass it directly as a string.

Or is there some way to do it like this in each child control's ascx file:

<my:PanelBase PanelId="myChildPanel" Header="My Child's Header">
    // HTML for my child panel.
</my:PanelBase>

Basically, I'm looking for some way to reuse the common portions of my control so I don't have to duplicate it for each child.


I guess the "most proper" way of doing this would be to have the main content of your container as a template, however that requires you to type <ContentTemplate></ContentTemplate> inside all your panels, which is less than ideal.

If I had to do this, I'd probably override AddParsedSubObject, collect any child controls into a collection and add them to a PlaceHolder or something similar in CreateChildControls. This can be done either with a custom control or a user control (.ascx).

For the headings and whatnot, just use Literals and create properties that wrap the Literal.Text properties.


Yes, and its generally good practice to do so. What would be ideal would be to put labels or literals in the places where you have content that would be modifiable. Then in the code behind you would put properties relating to each of them:

'These attributes allow you to specify individual properties about your control
'particularly if you want to be able to bind data to it, list it in the properties
'in your IDE, etc.
<BrowsableAttribute(True), Bindable(False), Category("Misc"), DefaultValue("true"), _
        Description("Gets or sets the content title.")> _
Public Property DisplayContentTitle() As String
    Get
        Return _displayContentTitle
    End Get
    Set(ByVal value As Boolean)
        _displayContentTitle = value
        Me.litContentTitle.Text = value  ' Optional
    End Set
End Property

Then when you include the control in your page you would configure it in the following manner:

<asp:MyHtmlControl ID="blah" runat="server" DisplayContentTitle="Some text" />

This gives you the ability to modify, validate, manipulate or whatever in the code behind of the user control. It also allows the control itself to modified during runtime. You can also override loading and rendering events of your common control to perform specific actions based on any of these settings you create.


You might want to look into Nested Master Pages.

0

精彩评论

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