locked
Declaring Control/WebControl in a page RRS feed

  • Question

  • User-1982247078 posted

    Hello,

    the question is quite simple, I want to use the System.Web.UI.Control or WebControl in the ASPX page, like

    <% @ Register TagPrefix="ui" Namespace="System.Web.UI" Assembly="System.Web" %>

    <ui:Control></ui:Control>

    However, it keeps throwing me that it is not a known element.

    If this can be done, how? If not, why?

     

    Thanks!

    Jan

    [EDIT: WebControl does not have a public parameterless constructor. As for Control, it is only designer error and can be ignored. Suggested reason for the error is that the designer explicitly forbids usings these controls.]

    Monday, October 17, 2011 2:04 AM

Answers

  • User3866881 posted

    That's not the reason why they cannot be used...

    If I make class TestControl : Control { } it shows up in the toolbox and can be used without problems. I cannot see how this is a better class than the Control itself (as it is not abstract). So are the controls you mentioned explictly forbidden in the ASP.NET compiler?

    Of course from your single comparation, you cannot see the difference bewteen a common Control. I mean that a Control has many virtual methods, these methods can be override with your own specific logic codes to achieve what you really want. Simply write a class inheriting from Control does nothing, that's why you cannot see this....

    In fact, we don't usually use Control, but many series of interfaces like IPostBackEventHandler or IPostBackData....

    Here's the full sample of a button:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;

    namespace SimpleCustomizedControl
    {
        public class MyButton : System.Web.UI.Control, IPostBackEventHandler
        {
            public event EventHandler Click;

            public void RaisePostBackEvent(string eventArgument)
            {
                Click(this, new EventArgs());
            }

            protected override void Render(HtmlTextWriter output)
            {
                output.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
                output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
                output.AddStyleAttribute(HtmlTextWriterStyle.Color, "yellow");
                output.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "green");
                output.AddAttribute(HtmlTextWriterAttribute.Value, "MyButton Click");
                output.RenderBeginTag(HtmlTextWriterTag.Input);
                output.RenderEndTag();
                base.Render(output);
            }
        }

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 19, 2011 4:28 AM
  • User-1002157272 posted

    When I was saying I cannot see how this is a better class than the Control itself, I meant it from the compiler point of view. I cannot see the difference and yet using the TestControl compiles while using the Control does not compile. This is what confuses me and what I am looking an answer to. What does the TestControl has that the Control does not that makes it to compile?

    When you are using your "TestControl", it works because your test control is inheriting from Control.   the   : Control      part of the declaration assigns Control as it's base class.

    You can't actually instantiate a Control object as a useable functioning control. When you see code that creates a Control object, it is not being used to perform server control type functions. It is used for things like cheking if an object is a control (  example.   if (textbox1 is Control) ... etc etc.

    It's like when you learn fundamental class design. The "Is-A" and Has-A" relationships.  A TextBox "is-a" Control, and as such, a TextBox recieves base functionality of a Control.   

    Basically, the Control class is only meant as a "framework" for other control types and some various forms of validation, etc.

    Do you understand what we mean now?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 11, 2011 6:21 AM
  • User-1002157272 posted

    yess, you can instantiate it, but you cannot use it itsself as a full-fledged control type is what im saying.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 11, 2011 7:00 PM

All replies

  • User3866881 posted

    Hello:)

    I didn't see the detail steps of your developments of the certain control, but you can do as the follow steps:

    1) Create a Web project and right click the proj and choose "ascx User Control".

    2) And then drag and drop several things onto the panel.

    3) Then drag and drop the customized control onto the normal aspx page to see what's happening.

    For more about Developments of Customized Controls, plz have a look at:

    http://msdn.microsoft.com/en-us/library/26db8ysc(VS.71).aspx

    Tuesday, October 18, 2011 9:55 PM
  • User-1982247078 posted

    Hi!

    Thanks for your reply. I have not posted any steps simply because I do not develop any control. I just want to have a System.Web.UI.Control or System.Web.UI.WebControl on the page, using markup, that's it. Since neither of them is marked abstract, I don't see why it shouldn't be possible...

    Jan

    Wednesday, October 19, 2011 3:08 AM
  • User3866881 posted

    Do you mean that this error even happens when you drag and drop the controls produced by Microsoft on the ToolBox?

    Wednesday, October 19, 2011 3:18 AM
  • User-1982247078 posted

    The thing is that neither Control nor WebControl are on the ToolBox...

    Wednesday, October 19, 2011 3:20 AM
  • User3866881 posted

    The thing is that neither Control nor WebControl are on the ToolBox...

    What? You mean when you install VS, you CANNOT see anything on the ToolBox?

    Have you switch to the design mode?

    Or your screenshot, plz.

    Thx again

    Wednesday, October 19, 2011 3:25 AM
  • User-1982247078 posted

    I have all controls on the ToolBox you would expect (like 3 pages of screen) but not the System.Web.UI.(Web)Control. You have it there?

    Wednesday, October 19, 2011 3:29 AM
  • User3866881 posted

    but not the System.Web.UI.(Web)Control.

    So you don't have controls like TextBox, CheckBox?

    They are System.Web.UI.WebControls.

    Sorry if I misunderstand you:)

    Wednesday, October 19, 2011 3:38 AM
  • User-1982247078 posted

    Yes, I have TextBox and these, and I know they derive from the WebControls. But I would like to add directly the WebControl to the page... not any of its derived classes.... is that more clear? :)

    Wednesday, October 19, 2011 3:40 AM
  • User3866881 posted

    No you cannot do that.

    WebControl is a basic control class that is used for creating web controls like TextBox or CheckBox with Width, Color,……, some HTML properties.

    But you can inherit it to override some methods to write your own controls and use your own ones.

    Wednesday, October 19, 2011 3:43 AM
  • User-1982247078 posted

    Okay, why I cannot do that? What are the formal requirements on the class so that it could be used in the page markup? (and obviously (Web)Control does not meet them)

    Wednesday, October 19, 2011 3:45 AM
  • User3866881 posted

    Okay, why I cannot do that?

    Because the "WebControl" is the class for creating other web controls, you can reguard it as a basic class. Just like an abstract class cannot be "new".

    What are the formal requirements on the class so that it could be used in the page markup?

    Just write a class that inherit the "WebControl", and then you can compile your whole proj and see the newly-added control on the ToolBox.

    e.g: Create a C# file and say——

    public class MyControl:WebControl
        {
            protected override void Render(HtmlTextWriter writer)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Type"button");
                writer.AddAttribute(HtmlTextWriterAttribute.Value"MyButton");
                writer.RenderBeginTag(HtmlTextWriterTag.Input);
            }
        }
    Recompile your whole app 
    and you can see the newly-added Control called "MyControl", 
    drag and drop onto your web aspx page and you can see there's button called "MyButton" there...

     

    Wednesday, October 19, 2011 3:57 AM
  • User-1982247078 posted

    Great, so the first important thing is that the WebControl does not have a public parameterless constructor, very good point.

    Now, System.Web.UI.Control does have it. Why this one cannot be used?

    Wednesday, October 19, 2011 4:06 AM
  • User3866881 posted

    Now, System.Web.UI.Control does have it. Why this one cannot be used?

    System.Web.UI.Controls,

    System.Web.UI.WebControl,

    System.Web.UI.CompositeControl……

    All of the three controls (not only them), cannot be used directly, because they are used to be inherited to create other controls with.

    Wednesday, October 19, 2011 4:14 AM
  • User-1982247078 posted

    That's not the reason why they cannot be used...

    If I make class TestControl : Control { } it shows up in the toolbox and can be used without problems. I cannot see how this is a better class than the Control itself (as it is not abstract). So are the controls you mentioned explictly forbidden in the ASP.NET compiler?

     

    Wednesday, October 19, 2011 4:19 AM
  • User3866881 posted

    That's not the reason why they cannot be used...

    If I make class TestControl : Control { } it shows up in the toolbox and can be used without problems. I cannot see how this is a better class than the Control itself (as it is not abstract). So are the controls you mentioned explictly forbidden in the ASP.NET compiler?

    Of course from your single comparation, you cannot see the difference bewteen a common Control. I mean that a Control has many virtual methods, these methods can be override with your own specific logic codes to achieve what you really want. Simply write a class inheriting from Control does nothing, that's why you cannot see this....

    In fact, we don't usually use Control, but many series of interfaces like IPostBackEventHandler or IPostBackData....

    Here's the full sample of a button:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;

    namespace SimpleCustomizedControl
    {
        public class MyButton : System.Web.UI.Control, IPostBackEventHandler
        {
            public event EventHandler Click;

            public void RaisePostBackEvent(string eventArgument)
            {
                Click(this, new EventArgs());
            }

            protected override void Render(HtmlTextWriter output)
            {
                output.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
                output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
                output.AddStyleAttribute(HtmlTextWriterStyle.Color, "yellow");
                output.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "green");
                output.AddAttribute(HtmlTextWriterAttribute.Value, "MyButton Click");
                output.RenderBeginTag(HtmlTextWriterTag.Input);
                output.RenderEndTag();
                base.Render(output);
            }
        }

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 19, 2011 4:28 AM
  • User-1982247078 posted

    Simply write a class inheriting from Control does nothing, that's why you cannot see this....

    No, I think you are still trying to see it more complex than my question is. :) I am not hiding any other scenario or code, using the Control class alone is everything what I need. The functionality it provides itself is enough and my TestControl : Control { } is a working workaround I am likely going to use.

    When I was saying I cannot see how this is a better class than the Control itself, I meant it from the compiler point of view. I cannot see the difference and yet using the TestControl compiles while using the Control does not compile. This is what confuses me and what I am looking an answer to. What does the TestControl has that the Control does not that makes it to compile?

    Wednesday, October 19, 2011 5:48 AM
  • User3866881 posted

    cannot see how this is a better class than the Control itself

    Yes, this is really almost no better than Control itself. Because I've told you that Control class cannot be shown on the ToolBar because VS has forbidden it from showing. So you cannot use it.

    Wednesday, October 19, 2011 9:00 PM
  • User-1002157272 posted

    lol Are you just messing with us or are you serious hjd.honza?

    Thursday, November 10, 2011 10:44 PM
  • User-1982247078 posted

    What is so funny about it? Do you know the answer?

    Friday, November 11, 2011 1:57 AM
  • User-1002157272 posted

    When I was saying I cannot see how this is a better class than the Control itself, I meant it from the compiler point of view. I cannot see the difference and yet using the TestControl compiles while using the Control does not compile. This is what confuses me and what I am looking an answer to. What does the TestControl has that the Control does not that makes it to compile?

    When you are using your "TestControl", it works because your test control is inheriting from Control.   the   : Control      part of the declaration assigns Control as it's base class.

    You can't actually instantiate a Control object as a useable functioning control. When you see code that creates a Control object, it is not being used to perform server control type functions. It is used for things like cheking if an object is a control (  example.   if (textbox1 is Control) ... etc etc.

    It's like when you learn fundamental class design. The "Is-A" and Has-A" relationships.  A TextBox "is-a" Control, and as such, a TextBox recieves base functionality of a Control.   

    Basically, the Control class is only meant as a "framework" for other control types and some various forms of validation, etc.

    Do you understand what we mean now?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 11, 2011 6:21 AM
  • User-1982247078 posted

    You can't actually instantiate a Control object as a useable functioning control.

    Yes I can, see:

    public partial class WebForm1 : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            Controls.Add(new Control());
        }
    }
     

    And it not only compiles, but works fine. Now I cannot manage to do this in the markup. Can you?

    Friday, November 11, 2011 7:12 AM
  • User-1002157272 posted

    yess, you can instantiate it, but you cannot use it itsself as a full-fledged control type is what im saying.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 11, 2011 7:00 PM
  • User-1982247078 posted

    But thats not what I was asking.

     

    Anyway, seems that it works from markup at runtime too, so it is only a designer error. My mistake posting it here, sorry.
    If anybody knows how to get rid of it, feel free to reply, otherwise I consider this closed.

    Saturday, November 12, 2011 3:36 AM