Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Extracting innerHTML from a div elements that contains dynamic controls

Extracting innerHTML from a div elements that contains dynamic controls

Scheduled Pinned Locked Moved ASP.NET
htmlsysadminhelpquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    compninja25
    wrote on last edited by
    #1

    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.

    N 1 Reply Last reply
    0
    • C compninja25

      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.

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Every server control comes with a RenderControl() method which render the control and it's child controls to the specified TextWriter. Trick here is to make your div run at server. Add a runat=server attribute to the Confirmation_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

      C M 2 Replies Last reply
      0
      • N N a v a n e e t h

        Every server control comes with a RenderControl() method which render the control and it's child controls to the specified TextWriter. Trick here is to make your div run at server. Add a runat=server attribute to the Confirmation_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

        C Offline
        C Offline
        compninja25
        wrote on last edited by
        #3

        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

        A N 2 Replies Last reply
        0
        • C compninja25

          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

          A Offline
          A Offline
          Abhishek Sur
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • C compninja25

            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

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            T is a generic type and where T : Control is a constraint. It means T can be of any type that derives from Control. When you write GetRenderedOutput(yourDiv);, compiler will infer the type from the type of yourDiv. Writing GetRenderedOutput<HtmlGenericControl>(yourDiv); is more explicit and it will only take a type of HtmlGenericControl. :)

            Navaneeth How to use google | Ask smart questions

            C 1 Reply Last reply
            0
            • N N a v a n e e t h

              T is a generic type and where T : Control is a constraint. It means T can be of any type that derives from Control. When you write GetRenderedOutput(yourDiv);, compiler will infer the type from the type of yourDiv. Writing GetRenderedOutput<HtmlGenericControl>(yourDiv); is more explicit and it will only take a type of HtmlGenericControl. :)

              Navaneeth How to use google | Ask smart questions

              C Offline
              C Offline
              compninja25
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • N N a v a n e e t h

                Every server control comes with a RenderControl() method which render the control and it's child controls to the specified TextWriter. Trick here is to make your div run at server. Add a runat=server attribute to the Confirmation_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

                M Offline
                M Offline
                Member 4579832
                wrote on last edited by
                #7

                can any one have similar method written in asp.net please reply Thanking you

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups