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. Get Controls in htmlstring

Get Controls in htmlstring

Scheduled Pinned Locked Moved ASP.NET
htmltutorial
9 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.
  • I Offline
    I Offline
    Imran Khan Pathan
    wrote on last edited by
    #1

    Hey. I am working on application in which I want to create dynamic controls and retrives them in html string. for example. public string CreateControl() { HtmlTable hTable = new HtmlTable(); hTable.CellPadding = 2; hTable.CellSpacing = 2; hTable.Width = "100%"; HtmlTableRow hRow = new HtmlTableRow(); HtmlTableCell hCell = new HtmlTableCell(); hCell.InnerText = "Username"; hRow.Cells.Add(hCell); hTable.Rows.Add(hRow); // Now here I want to return created controls in html string. } It should be like this <table CellPadding=2 CellSpacing = 2 Width=100><tr><td>Username</td></tr></table> Thanks Imrankhan

    please don't forget to vote on the post that helped you.

    C A 2 Replies Last reply
    0
    • I Imran Khan Pathan

      Hey. I am working on application in which I want to create dynamic controls and retrives them in html string. for example. public string CreateControl() { HtmlTable hTable = new HtmlTable(); hTable.CellPadding = 2; hTable.CellSpacing = 2; hTable.Width = "100%"; HtmlTableRow hRow = new HtmlTableRow(); HtmlTableCell hCell = new HtmlTableCell(); hCell.InnerText = "Username"; hRow.Cells.Add(hCell); hTable.Rows.Add(hRow); // Now here I want to return created controls in html string. } It should be like this <table CellPadding=2 CellSpacing = 2 Width=100><tr><td>Username</td></tr></table> Thanks Imrankhan

      please don't forget to vote on the post that helped you.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Your best bet to do that, is to use a string builder to generate the HTML yourself.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      I 1 Reply Last reply
      0
      • C Christian Graus

        Your best bet to do that, is to use a string builder to generate the HTML yourself.

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        I Offline
        I Offline
        Imran Khan Pathan
        wrote on last edited by
        #3

        Christian Graus wrote:

        Your best bet to do that, is to use a string builder to generate the HTML yourself.

        I have to write all controls in HTML and I dont want to write inlinne code at server side. I know It is possible because I had done it before one year ago when I worked on custom control but now I forget it. :doh: regard Imrankhan

        please don't forget to vote on the post that helped you.

        C 1 Reply Last reply
        0
        • I Imran Khan Pathan

          Christian Graus wrote:

          Your best bet to do that, is to use a string builder to generate the HTML yourself.

          I have to write all controls in HTML and I dont want to write inlinne code at server side. I know It is possible because I had done it before one year ago when I worked on custom control but now I forget it. :doh: regard Imrankhan

          please don't forget to vote on the post that helped you.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          You're right, 5 seconds with google got me this:

          public string RenderControl(Control ctrl)
          {
          StringBuilder sb = new StringBuilder();
          StringWriter tw = new StringWriter(sb);
          HtmlTextWriter hw = new HtmlTextWriter(tw);

          ctrl.RenderControl(hw);
          return sb.ToString();
          

          }

          Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

          I 1 Reply Last reply
          0
          • C Christian Graus

            You're right, 5 seconds with google got me this:

            public string RenderControl(Control ctrl)
            {
            StringBuilder sb = new StringBuilder();
            StringWriter tw = new StringWriter(sb);
            HtmlTextWriter hw = new HtmlTextWriter(tw);

            ctrl.RenderControl(hw);
            return sb.ToString();
            

            }

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

            I Offline
            I Offline
            Imran Khan Pathan
            wrote on last edited by
            #5

            Thanks Christian.

            please don't forget to vote on the post that helped you.

            S 1 Reply Last reply
            0
            • I Imran Khan Pathan

              Hey. I am working on application in which I want to create dynamic controls and retrives them in html string. for example. public string CreateControl() { HtmlTable hTable = new HtmlTable(); hTable.CellPadding = 2; hTable.CellSpacing = 2; hTable.Width = "100%"; HtmlTableRow hRow = new HtmlTableRow(); HtmlTableCell hCell = new HtmlTableCell(); hCell.InnerText = "Username"; hRow.Cells.Add(hCell); hTable.Rows.Add(hRow); // Now here I want to return created controls in html string. } It should be like this <table CellPadding=2 CellSpacing = 2 Width=100><tr><td>Username</td></tr></table> Thanks Imrankhan

              please don't forget to vote on the post that helped you.

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

              Imran Khan Pathan wrote:

              hCell.InnerText = "Username"; hRow.Cells.Add(hCell); hTable.Rows.Add(hRow); // Now here I want to return created controls in html string.

              Now use the following to get the string :

              StringBuilder sb = new StringBuilder();
              StringWriter tw = new StringWriter(sb);
              HtmlTextWriter hw = new HtmlTextWriter(tw);
              hTable.RenderControl(tw);
              string renderedoutput = sb.ToString();
              

              Now find the control string from the object, inspect renderedoutput. :rose:

              Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


              My Latest Articles-->** Microsoft Bing MAP using Javascript
              CLR objects in SQL Server 2005
              Uncommon C# Keywords
              /xml>

              1 Reply Last reply
              0
              • I Imran Khan Pathan

                Thanks Christian.

                please don't forget to vote on the post that helped you.

                S Offline
                S Offline
                sashidhar
                wrote on last edited by
                #7

                Imran Khan Pathan wrote:

                please don't forget to vote on the post that helped you.

                LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

                C I 2 Replies Last reply
                0
                • S sashidhar

                  Imran Khan Pathan wrote:

                  please don't forget to vote on the post that helped you.

                  LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #8

                  ROTFL - yes, that is quite ironic.

                  Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                  1 Reply Last reply
                  0
                  • S sashidhar

                    Imran Khan Pathan wrote:

                    please don't forget to vote on the post that helped you.

                    LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

                    I Offline
                    I Offline
                    Imran Khan Pathan
                    wrote on last edited by
                    #9

                    sashidhar wrote:

                    LatestArticle :Log4Net Why Do Some People Forget To Mark as Answer .If It Helps.

                    I tried to give him 5 vote but it did't work. when I click on button to vote, it goes to continuous process then I have to refresh page again. I am not able to vote any post here.

                    please don't forget to vote on the post that helped 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