A
Alan Mendelevich
Hi,
I'm trying to render control stored in ascx file to a string. I succeed with
the main part but events (at least OnLoad) doesn't fire (or aren't
automatically wired to Page_Load() method in the control). Here's what I do
(simplified):
in a script that renders to a string:
-------
Control ctrl = LoadControl("test_control.ascx");
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
ctrl.RenderControl(htw);
string body = sb.ToString();
-------------
test_control.ascx:
--------------
<%@ Control language="C#" %>
<script language="C#" runat="server">
void Page_Load()
{
Label1.Text = "world!";
}
</script>
Hello <asp:Label id="Label1" runat="server"/>
--------------
If instead of rendering to a string I add "ctrl" to Page.Control then it
renders "Hello world!" to the page, but "body" string contains only "Hello
". So I assume that OnLoad event doesn't fire (or doesn't autowire to
Page_Load method) when I call RenderControl (which makes some sense).
So my problem is how to make some method in control execute when it's
rendered to a string. Any ideas?
Thanks in advance,
Alan.
I'm trying to render control stored in ascx file to a string. I succeed with
the main part but events (at least OnLoad) doesn't fire (or aren't
automatically wired to Page_Load() method in the control). Here's what I do
(simplified):
in a script that renders to a string:
-------
Control ctrl = LoadControl("test_control.ascx");
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
ctrl.RenderControl(htw);
string body = sb.ToString();
-------------
test_control.ascx:
--------------
<%@ Control language="C#" %>
<script language="C#" runat="server">
void Page_Load()
{
Label1.Text = "world!";
}
</script>
Hello <asp:Label id="Label1" runat="server"/>
--------------
If instead of rendering to a string I add "ctrl" to Page.Control then it
renders "Hello world!" to the page, but "body" string contains only "Hello
". So I assume that OnLoad event doesn't fire (or doesn't autowire to
Page_Load method) when I call RenderControl (which makes some sense).
So my problem is how to make some method in control execute when it's
rendered to a string. Any ideas?
Thanks in advance,
Alan.