我需要一个html控件,该控件必须在被调用时调用一个方法。我使用以下代码创建html控件服务器端:
System.Web.UI.WebControls.Table t = new Table();
TableRow tr = new TableRow();
TableCell tc = new TableCell();
System.Web.UI.WebControls.TextBox tb = new System.Web.UI.WebControls.TextBox();
tb.Attributes.Add("onchange", "alert('foo');");
tc.Controls.Add(tb);
tr.Cells.Add(tc);
t.Rows.Add(tr);
string contents = null;
using (System.IO.StringWriter swriter = new System.IO.StringWriter())
{
System.Web.UI.HtmlTextWriter writer = new System.Web.UI.HtmlTextWriter(swriter);
t.RenderControl(writer);
contents = swriter.ToString();
}
不幸的是,有一种html编码
结果:
<table>
<tr>
<td><input type="text" onchange="alert('foo');" /></td>
</tr>
</table>
预期结果:
<table>
<tr>
<td><input type="text" onchange="alert('foo');" /></td>
</tr>
</table>