How to fill in a dynamic html form

Asked

Viewed 86 times

1

Good evening, everyone.

I have a question here, I hardly touch with web, but I needed to assemble an html file that aims to print a boleto. The project is done in C# and the html file is next to the project. I need to somehow play the data that is in the modeling (model) for the created html.

Part of html:

   <p style="margin: 0; "><b>ESTADO</b><br/><!-- Estado Aqui --></p>

I need, the modeling, insert, for example, the "SP" and fill in the place of this field 'Status Here' The project has a modeling with address fields.

Thanks in advance.

  • How is this file served? It’s an Asp.net page or a stream with html?

  • It is a part file for the document printing I made using pure HTML. I did not mount in Asp nor mvc. I did not imagine that I would use html page. Now that I need to, just using Asp.net to manipulate with data in html? Wouldn’t there be another way? If you have a tutorial that would help me on the Net.

  • Does the server read the disk file? If so, there is no way (without doing anything fancy), because the file would be static. You find on the net millions of tutorials from Asp.net and php that is suitable for dynamic pages.

  • 1

    This html file is generated by the program only for printing or is on a server?

  • So, it has no server use, well, I see no utility because it will not have database use, for now.

  • html is already a page, I need to play only the missing data for it. It is not on a server.

Show 1 more comment

1 answer

2

Your template page:

<!-- modelo.html -->
<html>
<head>
  <meta charset="UTF-8">
</head>
... etc ...

<p style="margin: 0; "><b>ESTADO</b><br/>|!@[ESTADO]@!|</p>

... etc ...

</html>

Then in c# you call:

// Coloque em cima: using System.Web; using System.Text; using System.IO;

var html = File.ReadAllText("modelo.html");

html.Replace("|!@[ESTADO]@!|", HttpUtility.HtmlEncode("<O ESTADO A SER INSERIDO>"));

File.WriteAllText("saida.html", html, Encoding.UTF8);

The pattern "|! @[STATUS]@!|" can be anything, as long as it is unique in the page and identical in html and code.

Note the <meta> tag at the beginning indicating the UTF-8 encoding, the same used when saving the output file, it is important to set this tag to prevent those question mark characters from appearing.

Also note the use of HttpUtility.HtmlEncode, to encode text in HTML defaults.

There are several ways to do this but this is a quick and few lines of code.

  • Opa, thanks and buddy. I followed his steps and gave it right. I am grateful even for the help and dedicated time. Many thanks to all ae.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.