473,382 Members | 1,204 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,382 software developers and data experts.

How to access the html content generated by a UserControl?

I have a UserControl living on MyPage.aspx. How would MyPage.aspx go
about retrieving the HTML content generated by that UserControl as a
string? I've looked into two options, neither of which seems like any
fun.

Option 1 is to use WebRequest to call MyPage.aspx from elsewhere, then
parse out the HTML chunk that I'm looking for by hand. This is pretty
cumbersome, and requires a separate application/webpage to work (i.e.,
using WebRequest in an MyPage.aspx page to load MyPage.aspx would have
disastrous results).

Option 2 is to mess with the the Render method of the UserControl.
This seems like overkill, since I don't need to grap the HTML content
every time that I run this control. Only on MyPage.aspx.

Anyone care to suggest an option 3 that I may have missed? Prehaps a
refinement to one of the above?

Jason

---
Expat Software Consulting Services
http://www.expatsoftware.com/
Nov 18 '05 #1
2 1529
Hello Jason,

How about doing this from a method MyPage.aspx?

StringBuilder builder = new StringBuilder();

using (HtmlTextWriter writer = new HtmlTextWriter(
new StringWriter(builder)))
{
myControl.RenderControl(writer);
}

string s = builder.ToString();

--
Matt Berther
http://www.mattberther.com
I have a UserControl living on MyPage.aspx. How would MyPage.aspx go
about retrieving the HTML content generated by that UserControl as a
string? I've looked into two options, neither of which seems like any
fun.

Option 1 is to use WebRequest to call MyPage.aspx from elsewhere, then
parse out the HTML chunk that I'm looking for by hand. This is pretty
cumbersome, and requires a separate application/webpage to work (i.e.,
using WebRequest in an MyPage.aspx page to load MyPage.aspx would have
disastrous results).

Option 2 is to mess with the the Render method of the UserControl.
This seems like overkill, since I don't need to grap the HTML content
every time that I run this control. Only on MyPage.aspx.

Anyone care to suggest an option 3 that I may have missed? Prehaps a
refinement to one of the above?

Jason

---
Expat Software Consulting Services
http://www.expatsoftware.com/


Nov 18 '05 #2
Nice. Exactly what I was looking for. Thanks!

Jason
Matt Berther <mb******@hotmail.com> wrote in message news:<#y**************@TK2MSFTNGP15.phx.gbl>...
Hello Jason,

How about doing this from a method MyPage.aspx?

StringBuilder builder = new StringBuilder();

using (HtmlTextWriter writer = new HtmlTextWriter(
new StringWriter(builder)))
{
myControl.RenderControl(writer);
}

string s = builder.ToString();

--
Matt Berther
http://www.mattberther.com
I have a UserControl living on MyPage.aspx. How would MyPage.aspx go
about retrieving the HTML content generated by that UserControl as a
string? I've looked into two options, neither of which seems like any
fun.

Option 1 is to use WebRequest to call MyPage.aspx from elsewhere, then
parse out the HTML chunk that I'm looking for by hand. This is pretty
cumbersome, and requires a separate application/webpage to work (i.e.,
using WebRequest in an MyPage.aspx page to load MyPage.aspx would have
disastrous results).

Option 2 is to mess with the the Render method of the UserControl.
This seems like overkill, since I don't need to grap the HTML content
every time that I run this control. Only on MyPage.aspx.

Anyone care to suggest an option 3 that I may have missed? Prehaps a
refinement to one of the above?

Jason

---
Expat Software Consulting Services
http://www.expatsoftware.com/

Nov 18 '05 #3

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

Similar topics

4
by: ATS | last post by:
BUG/PRODUCTION-DOWN: UserControl running from HTML not working. Someone please help, I have a production down situation. For whatever reason, UserControls are not owkring from HTML/IE. When...
2
by: paul meaney | last post by:
All, myself and another developer have been staring blankly at a screen for the past 48 hours and are wondering just what stunningly obvious thing we are missing. We are trying to load up 2...
29
by: Patrick | last post by:
I have the following code, which regardless which works fine and logs to the EventViewer regardless of whether <processModel/> section of machine.config is set to username="SYSTEM" or "machine" ...
1
by: Patrick | last post by:
Problem I have an ASPX file, with a table of 3 columns, 2 rows with a usercontrol in the middle column (in the code snippet stated below, although in reality, the left hand column would also...
4
by: Alexander Widera | last post by:
I have a page with the following code for example: <%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" Debug="true"%> <script runat="server"> void Page_Load(object...
5
by: c676228 | last post by:
Hi, I guess I am confused. In aspx script, I mean (you won't use Codebehind="enrollinfo.aspx.vb", but mix code with html and code together) You can access user control's property directly. Since I...
1
by: Christian Perthen | last post by:
Hi, I have a sub in the codebehind page of a user control file that writes html. How do I get the sub triggered and output the html in the content page? See my approach below. Partial Class...
5
by: Paul | last post by:
I have a MasterPage that has on it several UserControls (*ascx files). I wish to control the visibility of the controls on the content pages according to some parameter (for example on a user...
1
by: =?Utf-8?B?Tmltcm9kIFJvdG5lcg==?= | last post by:
Hi, I built a web control wich has text box and public property that set or get the text value of that text box. when i try to access that property i got error: "Object reference not set to an...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.