user1256752
user1256752

Reputation: 3

Dynamically outputting html code/snippets inside ASP.NET webform

I am looking for a way, if possible, to loop through a sub folder populated with multiple HTML files (snippets), then outputting the contents of these files within the one ASP.NET application page.

So if a new snippet was added to the sub folder, the application would automatically update, outputting its contents.

Upvotes: 0

Views: 573

Answers (1)

Irfan TahirKheli
Irfan TahirKheli

Reputation: 3662

I think you need this>

ASPX:

<asp:Literal runat="server" ID="lthtml"></asp:Literal>

CS:

 protected void Page_Load(object sender, EventArgs e)
    {
        string[] files = Directory.GetFiles("directory", "*.html");
        StringBuilder htmlContent = new StringBuilder();
        foreach (string f in files)
        {

            htmlContent.Append(ReadHtmlFile(f));
        }

        lthtml.Text = htmlContent.ToString();
    }


   public static StringBuilder ReadHtmlFile(string htmlFileNameWithPath)
   {
            System.Text.StringBuilder htmlContent = new    System.Text.StringBuilder();
            string line;
            try
            {
                using (System.IO.StreamReader htmlReader = new System.IO.StreamReader(htmlFileNameWithPath))
                {

                    while ((line = htmlReader.ReadLine()) != null)
                    {
                        htmlContent.Append(line);
                    }
                }
            }
            catch (Exception objError)
            {
                throw objError;
            }

            return htmlContent;
        }

Upvotes: 1

Related Questions