Code Collection: Old code to improve the DataGrid control
Found this code in my old hard disk, I remember I received a few emails about how to improve a control, this could be helpful to somebody:
/// <summary>
/// Summary description for MyDataGrid.
/// </summary>
public class MyDataGrid : System.Web.UI.WebControls.DataGrid
{
[Category("Design"), Description("OverLink Color")]
private string sBackgroundColor = "#dddddd";
public string SpecialLinkColor
{
get{return(sBackgroundColor);}
set{sBackgroundColor=value;}
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
string sDataGridID = this.ID.ToString();
string sJavaScript = "<script language=\"javascript\">\r" +
"function ShowRow1(elm, BgColour, FgColour)\r" +
"{\r" +
"elm.bgColor = BgColour;\r" +
"}\r" +
"function HilightRow(elm, hover, highlight)\r" +
"{\r" +
" var BgColour = (hover)? \"" + SpecialLinkColor + "\" : \"white\";\r" +
" var FgColour = \"black\";\r" +
" var Cursor = (hover)? \"hand\" : \"auto\";\r" +
" ShowRow1(elm, BgColour, FgColour);\r" +
" return false;\r" +
"}\r" +
"function RowOn() { return HilightRow(this, true, false); }\r" +
"function RowOff() { return HilightRow(this, false, false); }\r" +
"function RowClick()\r" +
"{\r" +
" HilightRow(this, false, true);\r" +
" var elm = eval('document.getElementById(\"' + this.name + '_link\")');\r" +
" if (elm) elm.click();\r" +
" return true;\r" +
"}\r" +
"var table = document.getElementById(\"" + sDataGridID + "\");\r" +
"var rows = table.getElementsByTagName(\"tr\");\r" +
"for (var i = 0; i < rows.length; i++) {\r" +
"rows[i].onmouseover = RowOn;\r" +
"rows[i].onmouseout = RowOff;\r" +
//"rows[i].onclick = RowClick;" +
"}\r</script>\r";
base.Render (writer);
base.Page.Response.Write(sJavaScript);
}
}
Cheers
Al