1

How can I insert code into a page in ASP.Net from a seperate source file?

Say I have:

   <% 
         Response.Write("hello world");
   %>

How can I make it something like:

   <% include(helloworld.cs) %>

I know how it work sin the header with the <%@ and CodeFile= but I can't make it work for different spots of code. Is there a way ASP.Net handles this? I've tried googling but not sure what to search for.

4 Answers 4

1

Another option not mentioned yet is to use ASP.NET Master Pages. This is useful to have a consistent look and feel (and code behind) in the master page which extends to the child pages.

Sign up to request clarification or add additional context in comments.

Comments

1

you could create a class library and add a reference to the library.

link that may help:

http://support.microsoft.com/kb/306575

2 Comments

I too think a Object Oriented approach would be appropriate :)
Creating a class library just to include some boilerplate UI markup seems like overkill.
1

You can use old-style Server-Side include tags like so:

<!-- #include virtual="/inc.inc" -->

But I do not recommend it. You should use User Controls instead; they give you more capability, and do not potentially expose server-side code if someone should happen to try to request them directly.

If you nevertheless decide to use includes like that, note that the content of the file is included in the ASPX/ASCX source code just as if you had typed it right in the main source file itself.

Comments

1

You can't shouldn't.

Instead, you should create a User Control.

5 Comments

This is not correct. You can use old-style server-side includes. I agree that you shouldn't do it, and should use User Controls instead, though.
Absolutely yes, SLaks; with Server-side code in them. The content of the included file runs exactly as if you had typed it in the source file that references it.
@Andrew: What are you talking about? Can you provide a link?
I've just done it myself with a file named "inc.inc" and included it in an ASP.NET 4.0 page. the inc.inc file used a server-side tag like so: <%= DateTime.Now%> and it worked perfectly.
@Andrew: I take that back. I never knew that; thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.