How do I use ParseControl?
-
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.
-
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.
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 -
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, 2002Thanks 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.
-
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.
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 -
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, 2002I'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.
-
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.
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
-
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