P
pcloches
I'm attempting to create a control at runtime and render the contents
of it into a string. However when I do this, the RenderControl() ends
up not writing anything. The page_load of the control IS being
executed.
Default.aspx.cs:
public string render_test() {
ControlCollection c = new ControlCollection(this);
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
test t = new test();
c.Add(t);
t.info_str = "asdf";
t.RenderControl(hw);
return sb.ToString();
}
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="~/test.ascx" TagName="test" TagPrefix="test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
a<%=render_test() %>b
</div>
</form>
</body>
</html>
Test.ascx.cs:
public partial class test : System.Web.UI.UserControl
{
public string info_str = "asdf";
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<li>page_load in control</li>");
}
protected void exec() {
Response.Write("<li>exec</li>");
}
}
Test.ascx:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="test.ascx.cs" Inherits="test" %>
aaa
<%=info_str %>
<% exec(); %>
bbb
of it into a string. However when I do this, the RenderControl() ends
up not writing anything. The page_load of the control IS being
executed.
Default.aspx.cs:
public string render_test() {
ControlCollection c = new ControlCollection(this);
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
test t = new test();
c.Add(t);
t.info_str = "asdf";
t.RenderControl(hw);
return sb.ToString();
}
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="~/test.ascx" TagName="test" TagPrefix="test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
a<%=render_test() %>b
</div>
</form>
</body>
</html>
Test.ascx.cs:
public partial class test : System.Web.UI.UserControl
{
public string info_str = "asdf";
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("<li>page_load in control</li>");
}
protected void exec() {
Response.Write("<li>exec</li>");
}
}
Test.ascx:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="test.ascx.cs" Inherits="test" %>
aaa
<%=info_str %>
<% exec(); %>
bbb