0

Is there a way to reuse a piece of ASP.NET code (that tag-based code, not the code-behind) in many different pages? For example I have a <div> with some contents in it that appears in 5 different pages of my site. I'm looking for a technique through which I could save this <div> in a separate web-content file so to speak, with maybe a different extension like MyDiv.ASPC and could then do a server-side tag anywhere in a webpage like:

<asp:Import href="~/MyDiv.aspc" />

and ASP.NET would inject the entire content of this file at that point where this tag appears.

I thought of creating a user control for this, but I'm not sure if a user control always injects precisely what is written in its body, or could there sometimes be unwanted tags generated by user control itself.

Or are there existing better ways of doing this?

Edit

About Master Pages, they are far away from what I'm looking for. They are actually good for a common basic layout of your website. My idea is quite opposite of that. My pages do not have a common layout; it is just that they have one common <div>. It is more closely fulfilled by a UserControl.

For UCs, my fear is that they generate more than what is written in their body, whereas what I'm after is a simple text injection. To put it technically, what I'm looking for is basically a preprocessor step (kind of #include thing in C++) rather than a compiler step, if you see what I mean.

dotNET
  • 33,414
  • 24
  • 162
  • 251
  • 3
    Yes, they're called [master pages](http://msdn.microsoft.com/en-us/library/wtxbf3hh.ASPX), user controls are for reusable...controls placed in multiple pages (their HTML content doesn't change in each page). – Adriano Repetti Dec 20 '13 at 13:42
  • 1
    Master pages, yeah, and user controls work just fine too (they don't change the tags, just the names and ids). – Luaan Dec 20 '13 at 13:43
  • Ah no. Master pages have several problems. In fact they are quite opposite of what I'm trying to achieve. – dotNET Dec 20 '13 at 13:43
  • Let me edit my question to shed some light on Master Pages. – dotNET Dec 20 '13 at 13:44
  • several problems? can you listen them? MP and UC are something really different (as scenario you would use them). MP are good if general page layout is fixed (and it's what they're useful for). UC are better if you want to reuse a (small) portion of HTML in different pages (with its own logic). Well...pretty raw... – Adriano Repetti Dec 20 '13 at 13:44

3 Answers3

1

You need to use ASP.NET User Controls, as these are specifically created to be the solution to the problem you are describing. For more information, see MS Documentation.

From their documentation...

In addition to using Web server controls in your ASP.NET Web pages, you can create your own custom, reusable controls using the same techniques you use for creating ASP.NET Web pages. These controls are called user controls.

A user control is a kind of composite control that works much like an ASP.NET Web page—you can add existing Web server controls and markup to a user control, and define properties and methods for the control. You can then embed them in ASP.NET Web pages, where they act as a unit.

mason
  • 31,774
  • 10
  • 77
  • 121
1

An empty userControl would do just that - nothing. A user Control just adds it's contents to the page, or usercontrol hosting it. It adds nothing extra.

UserControls give you a nice easy page fragment type approach to reusing content. They work great within a project & most people use them for just that.

If you wanted to make something more reusable across projects, you could write server control. It's more involved, but much more reusable. Google should be able to find you many tutorials on how to do this.

Simon Halsey
  • 5,459
  • 1
  • 21
  • 32
0

Ran a short test. User Controls do not enter extra tags as long as you don't place any Runat="Server" tags in it, so this would indeed be a solution I guess.

You can also read output from a cache object where you would read your files.

So

<%= Static.ContentXyz %>

would mean:

public static class Static
{
    public static string ContentXyz
    {
        get
        {
            string s;

            if (!this.cacheDictionary.TryGetValue("ContentXyz", out s))
            {
                s = File.ReadAllText(Server.MapPath("ContentXyz.html"));
                this.cacheDictionary("ContentXyz", s);
            }

            return s;
        }
    }
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Thanks. If I have a server-side tag, say `` in my UC, will it still spit it out if I remove `runat`? – dotNET Dec 20 '13 at 13:56
  • Well, it does, but not the way you want to. When you use a textbox without asp.net runat="server" tag you'd better use the html input tag (since without runat="server" you can't access it, so no need for the asp.net control). – Patrick Hofman Dec 20 '13 at 14:00
  • Let me wrap my head around this second approach. So this code will inject the contents of `ContentXyz.html` (which is ASP.NET server-side code itself) at the point where `<%= Static.ContentXyz %>` appears. But this will happen at runtime. Now who will run a second pass on the webpage to generate HTML from these contents? From where I see, this will send the contents of `ContentXyz.html` to user's browser. – dotNET Dec 20 '13 at 14:03
  • No, only if you use static text this is a solution (hence the name Statics). Else you need user controls. – Patrick Hofman Dec 20 '13 at 14:13