Solved

Creating Dynamic HTML File - using windows C# application.

Posted on 2003-12-10
11
792 Views
Last Modified: 2013-11-19
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
Comment
Question by:jksugu
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 6

Expert Comment

by:purpleblob
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 ?
0
 
LVL 7

Accepted Solution

by:
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
                  strmEditWrite.Write(TextBox1.Text);
                  strmEditWrite.Close();
            }
0
 
LVL 7

Expert Comment

by:TransBind
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>";
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 7

Expert Comment

by:TransBind
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!!');";
}
0
 
LVL 22

Expert Comment

by:_TAD_
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);
0
 
LVL 5

Expert Comment

by:seeflat
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.
0
 
LVL 22

Expert Comment

by:_TAD_
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.  
0
 
LVL 5

Assisted Solution

by:seeflat
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,
seeflat
0
 
LVL 22

Assisted Solution

by:_TAD_
_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.




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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

809 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