Click here to Skip to main content
15,891,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently updating my company's website. Over the last 8 years or so, different programmers have been inconsistent in how they include common files into pages: some used the old <!-- #include ... directive, others used <% Response.WriteFile(Request.MapPath(..., along with several variations.

Is there any effective difference between these methods?

I would also like to write a toolbox function that would open the file and return the text as a string, so I could do something like <% Toolbox.Include(filename) %>. I like this idea as it gives other programmers a consistent tool. Would this likely decrease performance?
Posted

I think you are thinking more like old style ASP way, rather than the Asp.net way.

Of course, you can develop a tool that accepts a file name (Aspx), executes it and returns the response. The following code depicts the idea:

C#
StringWriter writer = new StringWriter();
Server.Execute("Login.aspx", writer);
Response.Write("<H3>Please Login:</H3><br>"+ writer.ToString());


But, there is more elegant way in Asp.net to reuse the page or modules. There is User Control and Master page concept which you can use to re-use your common page/modules in a more manageable way than the old fashioned Asp #include.

In Asp.net, you can develop some user controls that would contain the re-usable GUI and functionality that can be used across different pages and you just include these user controls in whatever pages you require. See http://msdn.microsoft.com/en-us/library/fb3w5b53%28VS.71%29.aspx[^] to learn how to develop and use user controls.

And, you can also develop Master page(s) and include the common user controls within the Master page(s). Instead of adding the user controls individually at each page, this will allow you to reuse the common GUI and functionality across the pages by just hooking up your pages to the Master page(s). See http://www.asp.net/master-pages/tutorials[^] to learn how to develop and use Master pages.

As you seem more used to traditional Asp based programming model, you might need some time to cope up with the Asp.net's User control and Master page concept and utilize the full power of them. But once you become familiar with these, I bet you will love those.

Asp.net will let you work "smarter", not "harder". You will love it.
 
Share this answer
 
Comments
Gregory Gadow 16-Aug-10 13:02pm    
I don't think you read my question correctly.

I have some text -- say, a list of the rules for creating passwords -- that is used on several different pages. Rather than have this text actually written on different pages, I create a static text file and include it into the page. This way, I have a single file that can be updated and I eliminate the risk that different pages will drift out of synch.

My question is about the best way in ASP.Net to include that static text file in my pages.
Al-Farooque Shubho 16-Aug-10 22:24pm    
I read your question, but, didn't understand correctly, as your requirement was not elaborated enough. Sorry for that.

As "Aspdotnetdev" suggested, there may be better ".net" way of implementing this requirement, you can try to explore those. Thanks.
I figured out the answer I need.

<-- #include file="..." --> is executed before the page cycle begins, while Response.Write is executed during Render. Because some of the include files have their own in-line ASP code, they must be brought in with the #include directive so that the code is there when needed.

Thanks anyway.
 
Share this answer
 
v2
There may be some valid scenarios for when you would want to use the "include file=" way of including text. However, most of the time you'll want to use alternative ways. For example, if you want to include some settings, you would usually store that in the Web.config and load it up from the code behind. For user-based settings, you'd typically store that in a database/session/cookie/query string/form state. And if you want to reuse code, you'd put it in a class and use that from the code behind. And if you want to reuse markup, you'd put that in a user control that, optionally, accepts various parameters to render differently based on where it is used. These are typically the "proper .Net ways" of doing things in ASP.Net.
 
Share this answer
 

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