Click here to Skip to main content
15,305,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have been looking for a method in ASP.NET to be able to reuse mark-up from within a page file in the same page file.

Most of the time we have mark up that is used over and over within a page, but it is not re-useable in any other page.

For example the following asp markup is created 5 time in an apsx page.
<asp:gridview id="_view5" runat="server" autogeneratecolumns="false" showheader="false" width="100%" xmlns:asp="#unknown">
 GridLines="None" onrowdatabound="RowDataBound"
 DataSource='<%# ProjectValues( (EntityBase)Eval("Project"), "Health, P16") %>'>
		<asp:boundfield runat="server" datafield="Caption" itemstyle-cssclass="label" itemstyle-width="50%" />
		<asp:boundfield runat="server" datafield="DisplayValue" itemstyle-cssclass="controlcell" />

I am looking for something simular to XSL templates where you only place the markup language in a single place and can be called more than once by only providing the name of the template and a number of parameters.
Almost like doing a function call.

For example in XSL you write a template once:
<xsl:template name="table">
    <xsl:param name="value" />

            <td><xsl:value-of select="$value"/></td>
                <xsl:value-of select="$value"/>

and then the template can be called multiple times, like in the following example:
<xsl:call-template name="table">
    <xsl:with-param name="value" select="'test1'" />
<xsl:call-template name="table">
    <xsl:with-param name="value" select="'test2'" />

Any help would be appreciated.
Updated 25-Jul-10 22:52pm

1 solution

Use your toolbox!

If it's the same block of code (or at least very similar) you can select the text and drag it to your toolbox as a quick shortcut.

For the amount of code there, I don't know that creating a control is worth the effort. This is likely debatable, but with the wiring you'd need to expose properties to rig up a GridView, a custom control's likely not worth it's weight in belly dancers.

If you don't want to do the copy/paste thing, aren't open to the toolbox and don't want components that are reusable on other pages, you could always go the placeholder route.

Put the five (or however many you have) placeholders on the page, and replace them via code when the page loads (or load them with JavaScript).

Daniel Joubert 23-Jul-10 10:08am
I do not want to copy and paste anything. Want to be able to write a piece of markup in my aspx page and be able to reuse it within the aspx page.
TheyCallMeMrJames 23-Jul-10 10:37am
Revised a bit per your expanded details. Let me know if I can help further. Cheers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900