Extracting innerHTML from a div elements that contains dynamic controls
-
Hi Everyone, I was curious if it's possible to grab the HTML that a client would receive after the server processes a bit of code. I'm building a web app that will need to send an e-mail notification to the end user. I've already created the dynamic code and display the confirmation on the screen, but what I would like to do is something like this:
MailMessage.Body = Confirmation_div.InnerHtml.ToString();
This way, I can simply email all of the items without having to rebuild the message body html. This bit of code gives me an error indicating that the server can't grab the innerHTML because the div element isn't using any literal controls. Is there a way to convert all of the dynamic controls (after the values have been designated) to literal controls, or somehow grabbing the HTML equilavent that will be sent to the client during the PAGE_PreRender or something? Thanks!
Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.
-
Hi Everyone, I was curious if it's possible to grab the HTML that a client would receive after the server processes a bit of code. I'm building a web app that will need to send an e-mail notification to the end user. I've already created the dynamic code and display the confirmation on the screen, but what I would like to do is something like this:
MailMessage.Body = Confirmation_div.InnerHtml.ToString();
This way, I can simply email all of the items without having to rebuild the message body html. This bit of code gives me an error indicating that the server can't grab the innerHTML because the div element isn't using any literal controls. Is there a way to convert all of the dynamic controls (after the values have been designated) to literal controls, or somehow grabbing the HTML equilavent that will be sent to the client during the PAGE_PreRender or something? Thanks!
Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.
Every server control comes with a
RenderControl()
method which render the control and it's child controls to the specifiedTextWriter
. Trick here is to make your div run at server. Add arunat=server
attribute to theConfirmation_div
. Here is a generic method to get the rendered output of any control.string GetRenderedOutput<T>(T control) where T : Control
{
StringBuilder builder = new StringBuilder();
using (StringWriter sWriter = new StringWriter(builder))
using (Html32TextWriter writer = new Html32TextWriter(sWriter))
{
control.RenderControl(writer);
}
return builder.ToString();
}Use it like
MailMessage.Body = GetRenderedOutput(Confirmation_div);
OR
MailMessage.Body = GetRenderedOutput<HtmlGenericControl>(Confirmation_div);
:)
Navaneeth How to use google | Ask smart questions
-
Every server control comes with a
RenderControl()
method which render the control and it's child controls to the specifiedTextWriter
. Trick here is to make your div run at server. Add arunat=server
attribute to theConfirmation_div
. Here is a generic method to get the rendered output of any control.string GetRenderedOutput<T>(T control) where T : Control
{
StringBuilder builder = new StringBuilder();
using (StringWriter sWriter = new StringWriter(builder))
using (Html32TextWriter writer = new Html32TextWriter(sWriter))
{
control.RenderControl(writer);
}
return builder.ToString();
}Use it like
MailMessage.Body = GetRenderedOutput(Confirmation_div);
OR
MailMessage.Body = GetRenderedOutput<HtmlGenericControl>(Confirmation_div);
:)
Navaneeth How to use google | Ask smart questions
Hi Navaneeth, Thanks! I do remember seeing the RenderControl in the intellisense, but it never dawned on me to look into that method! I thought it would be "ClientOutput" or something like that. I did have a quick question, though. Can you tell me what this line is saying?
string GetRenderedOutput<T>(T control) where T : Control
I understand
Where T is inherenting from the Control class
, but I've never seen<t>(T Control)
before. Thanks Again!Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.
modified on Monday, September 28, 2009 12:22 PM
-
Hi Navaneeth, Thanks! I do remember seeing the RenderControl in the intellisense, but it never dawned on me to look into that method! I thought it would be "ClientOutput" or something like that. I did have a quick question, though. Can you tell me what this line is saying?
string GetRenderedOutput<T>(T control) where T : Control
I understand
Where T is inherenting from the Control class
, but I've never seen<t>(T Control)
before. Thanks Again!Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.
modified on Monday, September 28, 2009 12:22 PM
T represents a Generic Type here. Means you can assign any value to T type that inherits Control. If you write where T : class It would take any classes. I suggest to read the basic books on Generics to clear it more. :)
Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
-
Hi Navaneeth, Thanks! I do remember seeing the RenderControl in the intellisense, but it never dawned on me to look into that method! I thought it would be "ClientOutput" or something like that. I did have a quick question, though. Can you tell me what this line is saying?
string GetRenderedOutput<T>(T control) where T : Control
I understand
Where T is inherenting from the Control class
, but I've never seen<t>(T Control)
before. Thanks Again!Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.
modified on Monday, September 28, 2009 12:22 PM
T
is a generic type andwhere T : Control
is a constraint. It meansT
can be of any type that derives fromControl
. When you writeGetRenderedOutput(yourDiv);
, compiler will infer the type from the type ofyourDiv
. WritingGetRenderedOutput<HtmlGenericControl>(yourDiv);
is more explicit and it will only take a type ofHtmlGenericControl
. :)Navaneeth How to use google | Ask smart questions
-
T
is a generic type andwhere T : Control
is a constraint. It meansT
can be of any type that derives fromControl
. When you writeGetRenderedOutput(yourDiv);
, compiler will infer the type from the type ofyourDiv
. WritingGetRenderedOutput<HtmlGenericControl>(yourDiv);
is more explicit and it will only take a type ofHtmlGenericControl
. :)Navaneeth How to use google | Ask smart questions
Oh, so then you could write a method to say, format or parse strings, Dictonaries<>, Lists, ect and using a generic, pass the method anything and let it figure out what it is?
bool myMethod(T Object) Where T : Object {}
I'm a self-taught programmer and I've read the whole way through Head First C# (which I really enjoyed and learned a lot), but I don't recall seeing anything about generics....Or if I did, obviously it didn't sink in. I'll have to go back and review that tonight. Thank's again for all your help!
Knowledge is not power, however, the acquisition and appropriate application of knowledge can make you a very powerful individual.
-
Every server control comes with a
RenderControl()
method which render the control and it's child controls to the specifiedTextWriter
. Trick here is to make your div run at server. Add arunat=server
attribute to theConfirmation_div
. Here is a generic method to get the rendered output of any control.string GetRenderedOutput<T>(T control) where T : Control
{
StringBuilder builder = new StringBuilder();
using (StringWriter sWriter = new StringWriter(builder))
using (Html32TextWriter writer = new Html32TextWriter(sWriter))
{
control.RenderControl(writer);
}
return builder.ToString();
}Use it like
MailMessage.Body = GetRenderedOutput(Confirmation_div);
OR
MailMessage.Body = GetRenderedOutput<HtmlGenericControl>(Confirmation_div);
:)
Navaneeth How to use google | Ask smart questions
can any one have similar method written in asp.net please reply Thanking you