• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 812
  • Last Modified:

Creating Dynamic HTML File - using windows C# application.

Hi,

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.
0
jksugu
Asked:
jksugu
  • 3
  • 3
  • 2
  • +1
3 Solutions
 
purpleblobCommented:
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 ?
0
 
TransBindCommented:
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
                  strmEditWrite.Write(TextBox1.Text);
                  strmEditWrite.Close();
            }
0
 
TransBindCommented:
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>";
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
TransBindCommented:
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!!');";
}
0
 
_TAD_Commented:


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);
0
 
seeflatCommented:
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.
0
 
_TAD_Commented:


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.  
0
 
seeflatCommented:
_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,
seeflat
0
 
_TAD_Commented:


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.




jksugu>

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.

cheers!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now