Previously I've posted a few Helper Classes . This post describes my HtmlHelper class:
using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
///<summary>
/// Summary description for HtmlHelper.
///</summary>
public class HtmlHelper
{
// Methods
public HtmlHelper()
{
}
public static void AddBookMark(Control parent, string bookMarkName)
{
LiteralControl control1 = new LiteralControl();
control1.Text = "<a name=\"" + bookMarkName + "\">";
parent.Controls.Add(control1);
}
// 'idea from http://www.dotnetjunkies.com/Article/6614EF8F-2ADB-4AEB-B2B7-D96278B53D74.dcik
// 'MNF seems that I can't use bookmark this way.BookMark appends to url and seems lost viewstate
// 'Didn't find workaround so far -see for research http://www.eggheadcafe.com/articles/20030531.asp
public static void GoToBookMark(Page page, string bookMarkName)
{
StringBuilder sb = new StringBuilder();
sb.Append("<script language=\"JavaScript\">");
sb.Append("location.href=\"#" + bookMarkName + "\";");
sb.Append("</script>");
page.ClientScript.RegisterClientScriptBlock(TypeForClientScript(),"GoToBookMark", sb.ToString());
}
public static string ReplaceNewLine(string sMsg)
{
return sMsg.Replace(Environment.NewLine, NewLine);
}
// 'set text and tooltip
public static void SetClickToBtnText(Button btn, string text)
{
btn.ToolTip = "Click here to " + text;
btn.Text = text;
}
public static string WriteLine(HttpResponse Response, string sMsg)
{
string text1= WriteLine( sMsg);
Response.Write(text1);
return text1;
}
public static string WriteLine( string sMsg)
{
return sMsg + NewLine;
}
///<summary>
/// Function replaces 2 or more spaces to one space.
/// Function was created for use MetaBuilder ComboBox to modify list values.
/// Client DHTML innerText replaces multiple spaces with one,
/// but when return back to server string with single space doesn't match Listbox strings in ListItemCollection.FindByText Method
///</summary>
public static string RemoveNonPreservedSpace(string strText)
{
Regex reNonPreservedText = new Regex(@"\s{2,}", RegexOptions.IgnoreCase);
// Do the replacements
strText = reNonPreservedText.Replace(strText, " ");
return strText;
}
//from ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcon/html/vbwlkWalkthroughCreatingCustomWebControls.htm
public static string WriteHyperLinkHtml(string Url,string Text)
{
StringWriter sw = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(sw);
HyperLink placeholderLink = new HyperLink();
// put control text into the link's Text
placeholderLink.NavigateUrl = Url;
if ((Text==null) || (0==Text.Length)) Text=Url;
placeholderLink.Text = Text;
placeholderLink.RenderControl(tw);
return sw.ToString();
}
private static Type TypeForClientScript()
{
return typeof(HtmlHelper);
}
// Fields
public const string NewLine = "<BR />";
}