You will also need to get round the limitation of the fact that the <a> tag is not allowed around a table row, so here are the two javascript functions that perform the magic:
function highlight(tableRow, active) { if (active) { tableRow.style.backgroundColor = '#cfc'; } else { tableRow.style.backgroundColor = '#fff'; } } function link(Url) { document.location.href = Url; }
And here's the method that adds the javascript to each row of the table as it is rendered:
string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="; connstring += Server.MapPath("App_Data/NWind.mdb"); string query = "SELECT [CustomerID], [CompanyName], [ContactName], [Address], [City]"; query += " FROM [Customers]"; OleDbConnection conn = new OleDbConnection(connstring); OleDbDataAdapter da = new OleDbDataAdapter(query, conn); DataSet ds = new DataSet(); da.Fill(ds, "Employees"); foreach (DataRow dr in ds.Tables[0].Rows) { TableRow trow = new TableRow(); foreach (DataColumn dc in ds.Tables[0].Columns) { TableCell tcell = new TableCell(); tcell.Controls.Add(new LiteralControl(dr[dc.ColumnName].ToString())); trow.Cells.Add(tcell); trow.Attributes["onmouseover"] = "highlight(this, true);"; trow.Attributes["onmouseout"] = "highlight(this, false);"; trow.Attributes["onclick"] = "link('edit.aspx?id=" + dr[0].ToString() + "');"; HttpResponse myHttpResponse = Response; HtmlTextWriter myHtmlTextWriter = new HtmlTextWriter(myHttpResponse.Output); trow.Attributes.AddAttributes(myHtmlTextWriter); Table1.Rows.Add(trow); } } conn.Close();