views:

31

answers:

1

I am creating a custom include method (load) to check if a file exists. The load function includes the file if it exists or sends an email to notify me of a broken link if it doesn't. The problem I am having is if the include file contains server controls, it just displays them as plain text. This is a problem if I were to try to add an include file to an include file.

In default.aspx I have:

<% ResourceMngr.load("~/Includes/menu.inc"); %>

In ResourceMngr:

public static void load(String url) {

    string file = HttpContext.Current.Server.MapPath(url);
    if (System.IO.File.Exists(file))
    {
        HttpContext.Current.Response.Write(System.IO.File.ReadAllText(file));
    }
    else
    {
        String body = "This message was sent to inform you that the following includes file is missing: \r\n";
        body += "Referrer URL: " + HttpContext.Current.Request.Url.AbsoluteUri + "\r\n";
        body += "File Path: " + file;
        MailUtil.SendMail("[email protected]", "Missing Includes File", body);
    }
}

So, if "menu.inc" also includes a <% ResourceMngr.load("~/Includes/test.inc"); %> tag, it just prints it out in plain text on the page instead of trying to include test.inc while all the other html on the page shows up exactly as expected. How would I allow my include file to have server controls?

+2  A: 

I assume you come from a classic asp or php background. asp.net doesn't work in this way with includes. You probably want to look up some basic webforms or mvc tutorials because you want to work with the framework, and not against it :-)

in particular, look up how to use UserControls (ie. the ones with a .ascx extension)

Joel Martinez
When I try to use User Controls, I get the same issue that I was having before. For example <%@ Control Language="C#" AutoEventWireup="true" CodeFile="menu.ascx.cs" Inherits="Includes_menu" %> shows up at the top of my page in plaintext. I also tried having web controls inside <script runat="server"> but still had no luck.
Aaron
@Aaron, I can't see your code so I'm assuming here. But it feels like you are still missing something fundamental about the way asp.net (and webforms specifically) work. Take a look at some material on the web (like this one: http://www.asp101.com/lessons/usercontrols.asp), and then once you've gone through that and if you are still having issues, come back with the results :-) good luck!
Joel Martinez