3

I used to use a tool that you type in an HTML Code lik this one:

        <div id="pnlLoggedIn" style="width:480px;">
            <label for="txtUsername">Username</label>:
            <input name="txtUsername" type="text" id="txtUsername" class="input_small" tabindex="1"> 
            &nbsp;&nbsp;&nbsp;
            <label for="txtPassword">Password</label>:
            <input name="txtPassword" type="password" id="txtPassword" class="input_small" tabindex="2"> 
            &nbsp;&nbsp;
            <input type="submit" name="cmdLogin" value="Login" id="cmdLogin" class="red-button" tabindex="3" runat="server">
        </div>

And it gives you this as output:

StringBuilder sb = new StringBuilder();
sb.AppendLine("            <div id=\"pnlLoggedIn\" style=\"width:480px;\">");
sb.AppendLine("                <label for=\"txtUsername\">Username</label>:");
sb.AppendLine("                <input name=\"txtUsername\" type=\"text\" id=\"txtUsername\" class=\"input_small\" tabindex=\"1\"> ");
sb.AppendLine("                &nbsp;&nbsp;&nbsp;");
sb.AppendLine("                <label for=\"txtPassword\">Password</label>:");
sb.AppendLine("                <input name=\"txtPassword\" type=\"password\" id=\"txtPassword\" class=\"input_small\" tabindex=\"2\"> ");
sb.AppendLine("                &nbsp;&nbsp;");
sb.AppendLine("                <input type=\"submit\" name=\"cmdLogin\" value=\"Login\" id=\"cmdLogin\" class=\"red-button\" tabindex=\"3\" runat=\"server\">");
sb.AppendLine("            </div>");
return sb.ToString();

I cant remember the name of the tool, i remember it was an online tool.

If someone knows a tool that does that can please write it here.


--UPDATE--

Here is the tool i created: C# HTML Builder

Danpe
  • 18,668
  • 21
  • 96
  • 131
  • 2
    If I'm not mistaken...this seems like an extremely trivial tool to write (~5-10 minutes maybe). Why not make your own? – Justin Niessner Apr 21 '11 at 17:41
  • 1
    Having HTML in C# strings is a very bad idea as it very prone to resulting in a maintenance drama. Perhaps try the built in as.net objects to achieve your goal. Remember that you can extend an existing object to add the aspects that you are missing. `public class MyTextBox : TextBox` – Bazzz Apr 21 '11 at 17:43
  • Why not just keep it in the file system and load it in your app? Or use an embedded resource if you can't have anything in the file system? Either way seems far easier than maintaining HTML embedded in code. – Jamie Treworgy Apr 21 '11 at 17:47
  • @Justin I created my own tool you can download it here: [C# HTML Builder](http://www.mediafire.com/?7ew49ti37h3sbyf) – Danpe May 24 '11 at 10:01
  • @Bazzz: +1 for "maintenance drama." I'm stealing that. – Mike Hofer Dec 30 '11 at 17:44
  • @Danpe : +1 for the quick tool you made. helped me with UnitTests. – Rotem Slootzky Aug 25 '15 at 13:33

3 Answers3

4

At the risk of providing an anti-answer, this strikes me as a bad solution. The code sample provided looks like a static resource to me. As such, it likely doesn't belong in the source code. Rather, it belongs somewhere else: either as an embedded resource, or as a page that's loaded on demand.

This sort of code tends to be a nightmare to maintain. Further, depending on how much code of this sort you have in your system, it can become a major performance sink due to all the string allocations and the eventual pressure on the garbage collector. (StringBuilder won't save you from this. Monitor your application with a tool like .NET Performance Monitor and view the data type you allocate most frequently. You might be surprised.)

The point of this response (I won't deign to call it an answer), is this: think outside the box. Your solution may not be the right one. Is there a better way to tackle this problem? If so, consider it.

Mike Hofer
  • 16,477
  • 11
  • 74
  • 110
  • I need to write my HTML data according to the results i get from my SQLReader, what is the best way doing this? Isn't it Response.Write(sb.ToString()); if sb is the StringBuilder of the HTML code that i need? – Danpe Apr 21 '11 at 18:08
  • Even if that's the case, the HTML you're writing is a static, unchanging blob of HTML. You could load it at runtime from a file, and spit that file into the Response. This has the benefit of making it far easier to *modify* the file if the HTML is wrong, and simplifying the code that injects it into your page. And that's just *one* scenario. – Mike Hofer Apr 21 '11 at 18:18
  • How can i load it at runtime from a file and insert some C# vairbles in the middle? – Danpe Apr 21 '11 at 18:23
  • 1
    String.Format is your friend. Insert placeholders into the file where you'll want to insert the c# variables ("{0}" and so on). At runtime, call Response.Write(String.Format(myFileContents, arg1, arg2, arg3...)). – Mike Hofer Apr 21 '11 at 21:16
3

I made the application i needed :)

Hope anyone else can find a use in that:

C# HTML Builder

Danpe
  • 18,668
  • 21
  • 96
  • 131
2

http://www.codeproject.com/KB/vb/File2SB.aspx

Not online, but well. Should do the job ?

user703016
  • 37,307
  • 8
  • 87
  • 112
  • 1
    Thats what i need but to use it i need to copy my HTML code to a Notepad then open the notepad with the program then copy the code from the program and paste it on my code. I'm trying to find something that works very fast and easy :) if i had enough rep i whould have voting up for your answer – Danpe Apr 21 '11 at 17:47