Creating Dynamic HTML File - using windows C# application.

Posted on 2003-12-10
Last Modified: 2013-11-19

I need to create and save set of dynamic html page using windows c# application.
eg: If user clicks on preview   button i hv to create a html file (dynamically based on the data avilable on DB) and save it in a temp location and that should be open it automatically(it should be done automatically without knowing the use).

Can anyone help me out in this regard.
Question by:jksugu
  • 3
  • 3
  • 2
  • +1

Expert Comment

ID: 9911403
Can you supply some further information - is this button in a WebForm (i.e. HTML or ASP.NET or similar) or is this a Windows form ?

Creating a file from C# is pretty simple and outputting HTML is pretty simple also so can you ellaborate on precisely what problems/issues you have with coding this ?

Accepted Solution

TransBind earned 168 total points
ID: 9911950
I wrote a small sample for u, this is for a web application

            String strPath="c:";
            String strFile="Temp.html";

            private void SaveHTML_Click(object sender, System.EventArgs e)
                  StreamWriter strmEditWrite;

                  //delete the file if exists
                  File.Delete(strPath + strFile);

                  //create the file with new text
                  strmEditWrite = File.CreateText(strPath + "\\" + strFile);
                  //notice what i am passing to strmEditWrite.Write() it could be a string variable instead of TextField

Expert Comment

ID: 9911972
strmEditWrite.Write()  accepts a context for your html you could define a (String htmlVar) and pass to it

String htmlVar = "<html><head></head><body><b>this is bold</b></body></html>";

Expert Comment

ID: 9912277
In case you are wondering how to open a new windown when a user clicks on a button which is a server control button you can do it in two ways in the click event of the Button Webcontrol register a script code block or add an attribute to your button webcontrol.

Example for you, this shows how you can add client side javascript to your server control button using first method:

private void Page_Load(object sender, System.EventArgs e)
     //Button1 is a button id name
      Button1.Attributes["onclick"]="javascript:alert('Hello! Focus lost from text box!!');";
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

LVL 22

Expert Comment

ID: 9913469

From a windows platform (not asp)

using system.Diagnostics

                  ProcessStartInfo qOptions = new ProcessStartInfo(@"C:\Program Files\Internet Explorer\iexplore", @"C:\temp\test.html");

                  qOptions.UseShellExecute = false;  // set to true to make silent/background
                  Process myCmd = Process.Start(qOptions);

Expert Comment

ID: 9913989
You said "windows c# application", so I'll assume you don't want an ASP.NET solution.
_TAD_'s solution is a good one, however if you don't want to launch an IE instance in a seperate process, you can embed a MSHTML object inside the application as a control for previewing your generated page.

If this sounds like what you want, I'll provide more information for you.
LVL 22

Expert Comment

ID: 9914097

There are ways to open default browser instead of forcing Internet Explorer, however I find seeFlat's proposal very interesting, and from a programming architech point of view using seeFlat's proposal would be much more sound (provided that there isn't a ton of overhead).

seeFlat>  If you have some code or web pages handy I'd like to take a peek at them.  Currently a lot of things I do tend to use ProcessStartInfo and Process Classes.  But if I can reign in some of those 3rd party tasks and pull them into the umbrella of .Net I think I'd be better off.  

Assisted Solution

seeflat earned 166 total points
ID: 9914475
_TAD_> Quick and dirty example (hope it's not too confusing):

1. Create a new Windows application in Visual C# .NET.
2. Click View, and then click Toolbox.
3. Right-click the Toolbox to bring up the Customize Toolbox dialog box. On the COM components tab, click to select the Microsoft Web Browser check box, and then click OK.
4. Add the WebBrowser control to the form.
5. In Solution Explorer, right-click the References folder and select Add Reference.
The Add Reference dialog box opens.

6. Click on the .NET tab and double-click the component named Microsoft.mshtml.
7. Click OK.
8. Add the following to the "using declarations": using mshtml;
9. Add the following event handler declaration to InitializeComponent:
this.axWebBrowser1.DocumentComplete += new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.axWebBrowser1_DocumentComplete);
10. Add the following to the Form1_Load function:
object oEmpty = "";
axWebBrowser1.Navigate("about:blank", ref oEmpty, ref oEmpty, ref oEmpty, ref oEmpty);
11. Create the following function for the event handler declared in step 9:
private void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
      IHTMLDocument2 iDoc = (IHTMLDocument2)axWebBrowser1.Document;
      iDoc.write("<script language=\"javascript\">function test() { alert('Hello.'); }</script>");
      iDoc.write("<a href=\"#\" onclick=\"test();\">Hello</a>");

The footprint for this is no worse then IE itself, so as for overhead, "six in one hand, half dozen in the other". You end up with an IE browser that you can access with code directly. I've done DHTML, CSS, and javascript with this.

For purposes of this thread (jksugu original question), I usually create functions to return a string of HTML and simply pass the HTML to the control. Then when the user is satisfied, open a Stream and call the same HTML generating function to save the HTML to a file.

The actual API is huge, so there are LOTS of possibilities here; you're only limited by what knowledge of COM you have.

Good luck,
LVL 22

Assisted Solution

_TAD_ earned 166 total points
ID: 9914584

SeeFlat>  The process is pretty straight forward.

Add a COM component and create a reference.  What interested me the most was creating a delegate event handler and then these three lines of code:

IHTMLDocument2 iDoc = (IHTMLDocument2)axWebBrowser1.Document;
iDoc.write("<script language=\"javascript\">function test() { alert('Hello.'); }</script>");
iDoc.write("<a href=\"#\" onclick=\"test();\">Hello</a>");

Very slick.


While I believe my solution may be easier to understand "right out of the box" as it were.  Looking at SeeFlat's example, I would be inclined to say that his comment provides a much better solution to your question.  

Assuming that SeeFlat's code actually works (I haven't played with it yet), I would strongly urge you to go that route with your programming.  I may not be as savy a programmer as some, but I like to think I am a pretty good software architech (that's what I do for a living, coding is merely a hobby) and I know a good/better concept when I see it.


Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now