Creating Dynamic HTML File - using windows C# application.

Posted on 2003-12-10
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 672 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>";
Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands


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!!');";
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 664 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 664 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

762 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