locked
Writing the windows forms data to an HTML file RRS feed

  • Question

  • I have a windows forms application ready now i need to transfer data from the windows forms to the web and to do this i need to write an HTML file that contains the form data, i mean the data contained in the form should be written to an HTML file to be submitted and this HTML file should be generated on the local PC. I have the code for the HTML file but how can i write this HTML file with the data values contained in my windows forms.

    Please give your suggestions.

    Best Regards,

    Syed

    Thursday, August 26, 2010 7:14 PM

Answers

  • The base HTML

    <html>
    <head>
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <input type="hidden" id="hid1" name="val1" value="text box value"/>
    
    </body>
    </html>
    
    

    Save this as a.html

    Then Write the following code; I write this in a button click event

    private void button1_Click(object sender, EventArgs e)
        {
          StreamReader sr = new StreamReader(@"C:\Users\Thurupathan\Desktop\a.html");
          
    
          StringBuilder sb = new StringBuilder(sr.ReadToEnd());
    
          sr.Close();
          sb.Replace("text box value", textBox2.Text);
    
    
          StreamWriter sw = new StreamWriter(@"C:\Users\Thurupathan\Desktop\b.html");
          
          sw.Write(sb.ToString());
          sw.Flush();
          sw.Close();
    
        }
    

    For the value 82 typed in the textbox2, it will generate the html named b.html and the source a.html remains same.

    b.html

    <html >
    <head>
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <input type="hidden" id="hid1" name="val1" value="82"/>
    
    </body>
    </html>
    
    


    Regards Thurupathan My blog : http://thurupathan.spaces.live.com
    • Proposed as answer by Thuru VMVP Thursday, August 26, 2010 9:00 PM
    • Marked as answer by Helen Zhou Friday, September 3, 2010 2:33 AM
    Thursday, August 26, 2010 8:59 PM
  • You need to build up your HTML file from scratch - I have done a similar program:

    so you need to instantiate a StringBuilder() and start filling it with the needed HTML - you need to use absolute positioning

     

    StringBuilder html = new StringBuilder();

    foreach (Control ctrl in this.Controls)

    {

    if (ctrl is Label)

    {

    html.Append("<div style='position: absolute; left:'" + ctrl.Left + "; top: " + ctrl.Top + ";'> + ctrl.Text + "</div>");

    }

    else if (ctrl is TextBox)

    {

    html.Append("<div style='position: absolute; left:'" + ctrl.Left + "; top: " + ctrl.Top + "; border: solid black 1px;'> + ctrl.Text + "</div>");

    }

    }

    And you continue like this for all types of controls on your form - you can include include in the styles the format of your control like font bold, fore color and background color and others - I did a sample project and was able to transform complex windows forms to HTML.

    Then you can save the StringBuilder output to an html file using a StreamWriter.

     


    Ali Hamdar (alihamdar.com)
    • Marked as answer by Helen Zhou Friday, September 3, 2010 2:33 AM
    Thursday, August 26, 2010 7:32 PM
  • Hello,

    To make the data values to be hidden, you can use the hidden type textboxes in the HTML.

    If have same set of data values to be passed to the HTML you can permanently have a HTML document saved in your local drive. When ever you want to pass the value you can simply open the file and replace the values using Replace() method.

    Then save it as a seperate file..

    Just an idea to avoid hard coding the HTML format.


    Regards Thurupathan My blog : http://thurupathan.spaces.live.com
    • Marked as answer by Helen Zhou Friday, September 3, 2010 2:33 AM
    Thursday, August 26, 2010 8:24 PM
  • Dude,

    me too an undergraduate.. :DD


    Regards Thurupathan My blog : http://thurupathan.spaces.live.com
    • Marked as answer by SyedSafi Tuesday, September 7, 2010 4:32 PM
    Thursday, August 26, 2010 9:00 PM

All replies

  • You need to build up your HTML file from scratch - I have done a similar program:

    so you need to instantiate a StringBuilder() and start filling it with the needed HTML - you need to use absolute positioning

     

    StringBuilder html = new StringBuilder();

    foreach (Control ctrl in this.Controls)

    {

    if (ctrl is Label)

    {

    html.Append("<div style='position: absolute; left:'" + ctrl.Left + "; top: " + ctrl.Top + ";'> + ctrl.Text + "</div>");

    }

    else if (ctrl is TextBox)

    {

    html.Append("<div style='position: absolute; left:'" + ctrl.Left + "; top: " + ctrl.Top + "; border: solid black 1px;'> + ctrl.Text + "</div>");

    }

    }

    And you continue like this for all types of controls on your form - you can include include in the styles the format of your control like font bold, fore color and background color and others - I did a sample project and was able to transform complex windows forms to HTML.

    Then you can save the StringBuilder output to an html file using a StreamWriter.

     


    Ali Hamdar (alihamdar.com)
    • Marked as answer by Helen Zhou Friday, September 3, 2010 2:33 AM
    Thursday, August 26, 2010 7:32 PM
  • Thank You Brother Ali for the reply.

     

    I have some text boxes and some of the data is in the data grid view. The data grid view only contains the primary key data and from these primary keys we need to get the data values and write it to an HTML file. I think for the text boxes its just direct as shown above. 

    But do we need to get the values from the database for this.

    Your help is appreciated.

    Regards

    Syed

    Thursday, August 26, 2010 7:43 PM
  • Regarding the grid you need to create the needed HTML like this

    ....

     

    elseif (ctrl is DataGridView)
    {
     		html.Append("<table tyle='position: absolute; right: " + ctrl.Left
                                            + "; top: " + ctrl.Top + ";'>");
    
                        html.Append("<tr>");
                        foreach (DataGridViewColumn col in grid.Columns)
                        {
                            html.Append("<td>" + col.HeaderText + "</td>");
                        }
                        html.Append("</tr>");
    
                        foreach (DataGridViewRow row in grid.Rows)
                        {
                            html.Append("<tr>");
    
                            foreach (DataGridViewCell cell in row.Cells)
                            {
                                html.Append("<td>" + cell.Value.ToString() + "</td>");
                            }
    
                            html.Append("</tr>");
                        }
                        html.Append("</table>");
    }

     


    Ali Hamdar (alihamdar.com)
    Thursday, August 26, 2010 7:51 PM
  • Ok, i got some idea. Also i want the datavalues to be hidden when ever i open an HTML file in the browser and when i press the button those values should be transferred to the website.

    The first step is to write an html file.

    Thanks and waiting for your comment

    Syed

    Thursday, August 26, 2010 8:13 PM
  • Also i want to know how can i open this html file in a new browser window by click of a button in the windows forms.
    Thursday, August 26, 2010 8:19 PM
  • Hello,

    To make the data values to be hidden, you can use the hidden type textboxes in the HTML.

    If have same set of data values to be passed to the HTML you can permanently have a HTML document saved in your local drive. When ever you want to pass the value you can simply open the file and replace the values using Replace() method.

    Then save it as a seperate file..

    Just an idea to avoid hard coding the HTML format.


    Regards Thurupathan My blog : http://thurupathan.spaces.live.com
    • Marked as answer by Helen Zhou Friday, September 3, 2010 2:33 AM
    Thursday, August 26, 2010 8:24 PM
  • Puboo, thanks for your comment. Can you please show me an example as i am a newbie in C# and windows development who is just doing an undergraduate degree.
    Thursday, August 26, 2010 8:31 PM
  • The base HTML

    <html>
    <head>
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <input type="hidden" id="hid1" name="val1" value="text box value"/>
    
    </body>
    </html>
    
    

    Save this as a.html

    Then Write the following code; I write this in a button click event

    private void button1_Click(object sender, EventArgs e)
        {
          StreamReader sr = new StreamReader(@"C:\Users\Thurupathan\Desktop\a.html");
          
    
          StringBuilder sb = new StringBuilder(sr.ReadToEnd());
    
          sr.Close();
          sb.Replace("text box value", textBox2.Text);
    
    
          StreamWriter sw = new StreamWriter(@"C:\Users\Thurupathan\Desktop\b.html");
          
          sw.Write(sb.ToString());
          sw.Flush();
          sw.Close();
    
        }
    

    For the value 82 typed in the textbox2, it will generate the html named b.html and the source a.html remains same.

    b.html

    <html >
    <head>
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <input type="hidden" id="hid1" name="val1" value="82"/>
    
    </body>
    </html>
    
    


    Regards Thurupathan My blog : http://thurupathan.spaces.live.com
    • Proposed as answer by Thuru VMVP Thursday, August 26, 2010 9:00 PM
    • Marked as answer by Helen Zhou Friday, September 3, 2010 2:33 AM
    Thursday, August 26, 2010 8:59 PM
  • Dude,

    me too an undergraduate.. :DD


    Regards Thurupathan My blog : http://thurupathan.spaces.live.com
    • Marked as answer by SyedSafi Tuesday, September 7, 2010 4:32 PM
    Thursday, August 26, 2010 9:00 PM
  • Ok, thanks Puboo and Ali....i will try this out and let you know as early as possible.
    Thursday, August 26, 2010 9:06 PM
  • I just wanted to say THANK YOU for this great example! I have a need to do the same exact thing - write to an HTML file from a Windows Form app, and was having a hard time finding a good example until I came upon this one. I had an idea of what I needed to do, but you summed it up perfectly, and in a simple, straightforward manner. Thanks for sharing your insights....

    q-kev

    Wednesday, November 17, 2010 2:34 PM