473,408 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Creating a control at runtime and rendering it to a string

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

Apr 3 '07 #1
3 3514
your control uses Response.Write, not the HtmlWriter to render itself.
You will need to supply your own Response, that uses your html writer.

-- bruce (sqlwork.com)

pc******@gmail.com wrote:
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
Apr 3 '07 #2
I am not expert on control development but the following works:

System.Web.UI.WebControls.TextBox textBox = new
System.Web.UI.WebControls.TextBox();
System.Text.StringBuilder stringBuilder = new
System.Text.StringBuilder();
System.IO.StringWriter stringWriter = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter htmlWriter = new
System.Web.UI.HtmlTextWriter(stringWriter);

theTextBox.RenderControl(htmlWriter);
Response.Write(sb.ToString());

On Apr 3, 1:11 pm, pcloc...@gmail.com wrote:
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

Apr 3 '07 #3
I thought by passing my own HtmlWriter to RenderControl it used that
when it wrote?

carion's response apparently works for a TextBox, and its the same way
I did it... so why doesn't it work for my control?

Thanks everyone,
-Patrick

Apr 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
0
by: Scott Yenor | last post by:
Hello, I am writing a control library that has a control that inherits from System.Web.UI.Control. The control works fine and I am able to loop through the child controls and display everything...
1
by: Alan Mendelevich | last post by:
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...
3
by: Sky Sigal | last post by:
I coming unglued... really need some help. 3 days chasing my tail all over MSDN's documentation ...and I'm getting nowhere. I have a problem with TypeConverters and storage of expandableobjects...
7
by: Chuck Hartman | last post by:
I have a Windows service that requests web pages from a site using an HttpWebRequest object. When I try to request a page from an ASP.NET 2 site, I get a WebException with message "The remote...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
2
by: Mike | last post by:
Hi, I am strugling with a simple problem which I can't seem to resolve. I have an asp.net page which contains a server-control (flytreeview, which is a kind of a tree to be exact). The tree is...
4
by: Charles Zhang | last post by:
I created a custom server control whose rendering logic relies on session variables and cookies. It works just fine at run time. The problem is at the design time, because session variables and...
0
by: hardieca | last post by:
Hi, I would like to create an adapter for the image web control to cure a pet peeve of mine. Currently, if I write out: <asp:Image ImageUrl="myPic.jpg" AlternateText="Some text"...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.