Convert XmlText to HtmlText

I have been using Xml streams for quite some time. When I use them in an ASP.Net page, using SOAP or Ajax calls, I sometimes want to see what I send and what I recieve from the server. Showing them on the page was always a bit of tweak. This ample code demonstrates a very simple tweek. It provides a label that can show the xml Text (as its content or Text property) on the page, by rendering the simpel characters.
When the control is compiled I can use it as one of the two ways:

<panahyAjax:XmlLabel runat=”server” ID=”xmlLabel”>
<this>
 
<is just=”one”>sample</is>
</this>
</panahyAjax:XmlLabel>

Or

XmlLabel label = new XmlLabel();
var file = File.OpenText(“Sample.xml”);
label.Text = file.ReadToEnd();
And the code is as folllows:

public class XmlLabel : Label
{
 public static string ConvertXmlTextToHtmlText(string inputText)
 {
  // Replace all start and end tags.
  string startPattern = @”<([^>]+)>”;
  var regEx = new Regex(startPattern);
  string outputText = regEx.Replace(inputText, “&lt;<b>$1&gt;</b>”);
  outputText = outputText.Replace(
” “, “&nbsp;”);
  outputText = outputText.Replace(
“rn”, “<br />”);
  return outputText;
 }

 protected override void RenderContents(HtmlTextWriter output)
 {
  string xmlText = XmlLabel.ConvertXmlTextToHtmlText(Text);
  output.Write(xmlText);
 }
}

Using RenderControl method to generate Html


This article demonstrates how to generate perfect HTML using the RenderControl method of server controls. The Html code is generated without makeing any server-side state or view state.
/// <summary>
/// This method demonstrates how to show Html output of a Server Control
/// </summary>
private void ShowHtml()
{
  // Create the in-memory objects that will catch the rendered output.
  StringWriter writer = new StringWriter();
  HtmlTextWriter output = new HtmlTextWriter(writer);

  // Render the control to an in-memory string.
  var control = new HyperLink();
  control.NavigateUrl =
@”http:\asghar.panahy.com”;
  control.Text =
“Asghar Panahy”;
  control.Target =
“_blank”;
  control.ToolTip =
“Demostration”;

  control.RenderControl(output);

  // Display the HTML (and encode it properly so that
  // it appears as text in the browser).
  lblHtml.Text = “The HTML for LinkWebControl1 is<br /><blockquote>”
               + Server.HtmlEncode(writer.ToString()) +
“</blockquote>”;
}

The output of this codewill be reading what you would ecpect in the Html code behind.
Notice that skipping the Server.HtmlEncode call will put the generated html as a link into the page.

How to stick the footer to the buttom of the page

As usual there are more roads leading to Mekka. This is one way of getting the same result without any scripts. It uses a simple cascading stylesheet:


div#footer
{
   clear: both;
   padding: .5em 1em;
   border-top: 0px solid #ccc;
   text-align: center;
   padding: 0 0 0 0;
   margin: -20px 0 0 -100px;
   position: fixed;
   left : 50%;
   top: 100%;
}

Disabling the Browser Context Menu

Date: Monday, 18 Dec 2006 14:14
By Bret Taylor, December 3, 2006

You can disable the browser context menu for an element in your DOM with the following JavaScript recipe:

function disableContextMenu(element)
{
element.oncontextmenu = function()
{ return false; }
}

For example, to disable the context menu over a photo on your page, you could use:

disableContextMenu(document.getElementById("photo"));

Browsers like Firefox do not always allow web pages to disable the context menu (this option is a setting in Firefox), so this recipe will not work 100% of the time in all browsers.

See the example