By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

How to access the html content generated by a UserControl?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.