473,545 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rendering user control to a string and events in it

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("te st_control.ascx ");

StringBuilder sb = new StringBuilder() ;
StringWriter sw = new StringWriter(sb );
HtmlTextWriter htw = new HtmlTextWriter( sw);

ctrl.RenderCont rol(htw);

string body = sb.ToString();

-------------

test_control.as cx:

--------------

<%@ 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.

Nov 18 '05 #1
1 5108
"Alan Mendelevich" <ne**@stop.spam ming.ailon.org> wrote in message
news:ea******** ******@TK2MSFTN GP12.phx.gbl...
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("te st_control.ascx ");

StringBuilder sb = new StringBuilder() ;
StringWriter sw = new StringWriter(sb );
HtmlTextWriter htw = new HtmlTextWriter( sw);

ctrl.RenderCont rol(htw);

string body = sb.ToString();


If you want control events to fire, you need to add the control to your
page's Controls collection. Alternatively, if you have a container control
on the page, you can add the user control to this container.
--
John Saunders
johnwsaundersii i at hotmail
Nov 18 '05 #2

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

Similar topics

2
4716
by: Mike Speak | last post by:
I have a user control that I want to use to render 4 menu items (retrieved from db) on the top of each of my asp.net pages. The user control defines a table, with one TR and one TD. Within the TD I have a placeholder defined: <asp:PlaceHolder ID="categoriesPlaceHolder" Runat="server" /> In the code behind of the user control, on the...
6
12265
by: grist2mill | last post by:
I want to create a standard tool bar that appears on all pages that is a control. The toolbar has a button 'New'. What I wolud like when the user clicks on 'New' depends on the page they are on. I would like to do this by defining a NewFunc() that is different in each (code-behind) page which is called by the standard 'New' button in the...
4
2497
by: louise raisbeck | last post by:
Resending this as own topic as didnt get answer from original. Would be grateful for a response from anyone that knows. Thanks. Hi there, I found your post really helpful..but i wondered if, once I have exposed a public property containing the value of a textbox in a user control..how do I grab this from the calling page? I cant think of the...
8
2253
by: David Lozzi | last post by:
Howdy, I have a user control that is a report to display data. On the page the control is inserted in, I have filter options to filter the report. When I try to do something like this, nothing happens. dim filt as string ... build filter string... UserControl.ReportFilter = filt
5
2617
by: Dave A | last post by:
I am writing an ASP.NET tool that will allow the client to create their own online froms. ie the client can add tect boxes, text, drop downs,etc with absolutely no technical skill what so ever. The form can then be deployed to their intranet. The form designer is somewhat WYSIWYG but it contains a heap of 'edit', 'delete' and reordering...
0
2480
by: jonathan.beckett | last post by:
I have been working on a client project recently that is using winforms ..NET user controls within web pages in Internet Explorer, and need to find out how to make the user control communicate back to the webpage (typically to tell it that it has finished doing something). I started digging around and found out that I needed to implement a COM...
5
2310
by: paul.hester | last post by:
Hi all, I have a custom control with an overridden Render method. Inside this method I'm rendering each control in its collection using their RenderControl method. However, I'm running into a problem in this scenario: <myprefix:mycontrol runat="server"> <%= SomeVariable %> </myprefix:mycontrol>
2
4805
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be required: <%@ Register TagPrefix="UC" TagName="MyUserCtrl" Src="MyUC.ascx" %> Assuming that the ASPX page doesn't use a code-behind, I can access the...
2
4231
by: Benton | last post by:
Hi there, I'm creating a custom server control, inheriting from TextBox. It has this AsDateTime property that returns the textbox contents converted to the nullable DateTime data type, as follows: public class TextBoxWeb : System.Web.UI.WebControls.TextBox {
0
7459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7803
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7411
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7749
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5965
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5322
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3444
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3439
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1871
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.