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. General Programming
  3. C#
  4. How do I use ParseControl?

How do I use ParseControl?

Scheduled Pinned Locked Moved C#
helpquestiondesign
7 Posts 2 Posters 1 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hello. I am building a custom web control, and I need to render child controls which are passed as literal strings. I am using ParseControl for this. However, it doesn't seem to be working. When I compile my custom control I get "CS0118 System.Web.UI.TemplateControl.ParseControl denotes a method where a class was expected". My code looks like: this.Controls.Add(new LiteralControl(new System.Web.UI.TemplateControl.ParseControl(""))); I tried just ParseControl as apposed to System.Web.UI.TemplateControl.ParseControl but when I used ParseControl I got the error "No class or namespace named ParseControl in MyControl, (are you missing an assembly reference?)" But I have System.Web.UI imported into my class. I don't know what to do. Any help is very greatly appreciated.

    J 1 Reply Last reply
    0
    • L Lost User

      Hello. I am building a custom web control, and I need to render child controls which are passed as literal strings. I am using ParseControl for this. However, it doesn't seem to be working. When I compile my custom control I get "CS0118 System.Web.UI.TemplateControl.ParseControl denotes a method where a class was expected". My code looks like: this.Controls.Add(new LiteralControl(new System.Web.UI.TemplateControl.ParseControl(""))); I tried just ParseControl as apposed to System.Web.UI.TemplateControl.ParseControl but when I used ParseControl I got the error "No class or namespace named ParseControl in MyControl, (are you missing an assembly reference?)" But I have System.Web.UI imported into my class. I don't know what to do. Any help is very greatly appreciated.

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      ParseControl is a method of TemplateControl which is an abstract class that Page inherits from. You use ParseControl when you want to add a new ASP.NET server control to the page.

      void Page_Load(object sender, System.EventArgs e)
      {
      Button myButton = (Button)ParseControl("");
      myButton.Text = "Hello World!";
      Controls.Add(myButton);
      }

      A literal control is used when you wish to insert HTML into your page, not ASP.NET server controls. If you want to put HTML in your website, use Controls.Add(new LiteralControl(myHTML)); If you want to add a new server control (all server controls have runat="server" in the HTML for them) use the first code example. HTH, James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002

      L 1 Reply Last reply
      0
      • J James T Johnson

        ParseControl is a method of TemplateControl which is an abstract class that Page inherits from. You use ParseControl when you want to add a new ASP.NET server control to the page.

        void Page_Load(object sender, System.EventArgs e)
        {
        Button myButton = (Button)ParseControl("");
        myButton.Text = "Hello World!";
        Controls.Add(myButton);
        }

        A literal control is used when you wish to insert HTML into your page, not ASP.NET server controls. If you want to put HTML in your website, use Controls.Add(new LiteralControl(myHTML)); If you want to add a new server control (all server controls have runat="server" in the HTML for them) use the first code example. HTH, James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Thanks alot, but that's not really what I was trying to do. You see, I'm getting this string for the ASP.NET web controls from a DB. They are dynamic. I don't know wether they will be buttons, labels, dropdownlists, or what. I am trying to use ParseControl on that literal string. The literal string is the actual code of the ASP.NET web controls, not references to instantiate them or set their properties. Does that make sense now? I have to find out how to use ParseControl in that sort of instance. Thanks alot.

        J 1 Reply Last reply
        0
        • L Lost User

          Thanks alot, but that's not really what I was trying to do. You see, I'm getting this string for the ASP.NET web controls from a DB. They are dynamic. I don't know wether they will be buttons, labels, dropdownlists, or what. I am trying to use ParseControl on that literal string. The literal string is the actual code of the ASP.NET web controls, not references to instantiate them or set their properties. Does that make sense now? I have to find out how to use ParseControl in that sort of instance. Thanks alot.

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          ok, i think i see what you're doing. In the Page_Load event Controls.Add(ParseControl(stringFromDB)); You'll have to set all your properties for the control in the string coming from the DB ie <asp:button id="button1" text="push me"> In general you use LiteralControl for static HTML, use ParseControl when you are creating a server control. HTH, James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002

          L 1 Reply Last reply
          0
          • J James T Johnson

            ok, i think i see what you're doing. In the Page_Load event Controls.Add(ParseControl(stringFromDB)); You'll have to set all your properties for the control in the string coming from the DB ie <asp:button id="button1" text="push me"> In general you use LiteralControl for static HTML, use ParseControl when you are creating a server control. HTH, James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I'm using the code you provided above, but it still does not work. I'm not doing this in an ASPX page. I am calling ParseControl in a custom web control source (.CS file), that's being compiled into an assembly. And when I compile the assembly, I get the error: "ParseControl does not exist in the class MyControl" so I reference it explicitly: Controls.Add(System.Web.UI.TemplateControl.ParseControl(stringFromDB); When i compile the assembly I get: "CS0120: An object reference is required for the nonstatic field, method, or property 'System.Web.UI.TemplateControl.ParseControl(string)'" I've got to find out how to solve that. Thank you for all your help.

            J 1 Reply Last reply
            0
            • L Lost User

              I'm using the code you provided above, but it still does not work. I'm not doing this in an ASPX page. I am calling ParseControl in a custom web control source (.CS file), that's being compiled into an assembly. And when I compile the assembly, I get the error: "ParseControl does not exist in the class MyControl" so I reference it explicitly: Controls.Add(System.Web.UI.TemplateControl.ParseControl(stringFromDB); When i compile the assembly I get: "CS0120: An object reference is required for the nonstatic field, method, or property 'System.Web.UI.TemplateControl.ParseControl(string)'" I've got to find out how to solve that. Thank you for all your help.

              J Offline
              J Offline
              James T Johnson
              wrote on last edited by
              #6

              ok, now we're getting somewhere :) There are only two classes that inherit from TemplateControl, Page and UserControl. So your control is going to have to inherit from one of these (most likely UserControl) to make use of the ParseControl method. Because ParseControl is not a static method you need an instance of TemplateControl (or a derivative of it) to call it. But TemplateControl is an abstract class so you cannot create an instance of it, leaving you with having to have an instance of one of TemplateControl's derivatives. Out of the box your choices are Page and UserControl. Is it possible to change your control so it inherits from UserControl instead of Control? I'll admit I'm no where near up-to-date on ASP.NET as I am C# and the rest of the framework, so I'm just throwing out ideas :-O James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002

              L 1 Reply Last reply
              0
              • J James T Johnson

                ok, now we're getting somewhere :) There are only two classes that inherit from TemplateControl, Page and UserControl. So your control is going to have to inherit from one of these (most likely UserControl) to make use of the ParseControl method. Because ParseControl is not a static method you need an instance of TemplateControl (or a derivative of it) to call it. But TemplateControl is an abstract class so you cannot create an instance of it, leaving you with having to have an instance of one of TemplateControl's derivatives. Out of the box your choices are Page and UserControl. Is it possible to change your control so it inherits from UserControl instead of Control? I'll admit I'm no where near up-to-date on ASP.NET as I am C# and the rest of the framework, so I'm just throwing out ideas :-O James Sonork ID: 100.11138 - Hasaki "Not be to confused with 'The VD Project'. Which would be a very bad pr0n flick. :-D" - Michael P Butler Jan. 18, 2002

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Thanks for all the help!! ParseControl now works perfectly :-)

                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