HtmlTableCell cell = new HtmlTableCell(); cell.Attributes.Add("onmouseover", "alert('afdasf')");
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <table id="tb1"> <tr><td>aaaa</td></tr> <tr><td>bbbb</td></tr> </table> </div> </form> </body> </html> <script> obj = document.getElementById('tb1'); for(var i=0;i<obj.rows.length;i++) { for(var j=0;j<obj.rows[i].cells.length;j++) { obj.rows[i].cells[j].attachEvent('onmouseover',rcolor); obj.rows[i].cells[j].attachEvent('onmouseout',ccolor); } } function rcolor() { var t=event.srcElement.parentElement; t.style.backgroundColor="blue"; } function ccolor() { var t=event.srcElement.parentElement; t.style.backgroundColor="white"; } </script>
protected override void Render(HtmlTextWriter writer) { System.IO.StringWriter stringWriter = new System.IO.StringWriter(); HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter); base.Render(htmlWriter); string html = stringWriter.ToString(); html = html.Replace("<td", "<td onmouseover=\"javascript:alert('aa')\""); writer.Write(html); }