WebControl 和 CompositeControl 之间的区别?

发布于 2024-08-08 04:44:22 字数 1436 浏览 6 评论 0原文

我一直在网上浏览并找到了一些有关该主题的文章,但我仍然无法弄清楚它们之间的区别。我的代码如下所示,如果我从 CompositeControl 继承,它可以完美运行,但如果我从 WebControl 继承,则不能。 (它们都呈现代码,但只有 CompositeControl 处理事件)

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestLibrary
{
    public class TemplateControl : CompositeControl
    {
        TextBox txtName = new TextBox();
        TextBox txtEmail = new TextBox();
        Button btnSend = new Button();

        private void SetValues()
        {
            btnSend.Text = "Skicka";
        }

        protected override void CreateChildControls()
        {
            SetValues();

            this.Controls.Add(new LiteralControl("Namn: "));
            this.Controls.Add(txtName);
            this.Controls.Add(new LiteralControl("<br />"));
            this.Controls.Add(new LiteralControl("Email: "));
            this.Controls.Add(txtEmail);
            this.Controls.Add(new LiteralControl("<br />"));
            btnSend.Command += new CommandEventHandler(btnSend_Command);
            this.Controls.Add(btnSend);
        }

        void btnSend_Command(object sender, CommandEventArgs e)
        {
            this.Page.Response.Write("Du har nu klickat på skicka-knappen! <br /><br />");                
        }
    }
}

因此,当我单击按钮并且控件呈现为 WebControl 时,没有任何反应。但如果我将 WebControl 更改为 CompositeControl,则会打印出文本。为什么? WebControl 和 CompositeControl 有什么区别?

I have been looking around on the web and found some articles about the topic, but i still can't figure out the difference between them. I have the code show below, if i inherit from a CompositeControl it works perfectly but not if i inherit from a WebControl. (They both render the code, but only the CompositeControl handles the event)

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestLibrary
{
    public class TemplateControl : CompositeControl
    {
        TextBox txtName = new TextBox();
        TextBox txtEmail = new TextBox();
        Button btnSend = new Button();

        private void SetValues()
        {
            btnSend.Text = "Skicka";
        }

        protected override void CreateChildControls()
        {
            SetValues();

            this.Controls.Add(new LiteralControl("Namn: "));
            this.Controls.Add(txtName);
            this.Controls.Add(new LiteralControl("<br />"));
            this.Controls.Add(new LiteralControl("Email: "));
            this.Controls.Add(txtEmail);
            this.Controls.Add(new LiteralControl("<br />"));
            btnSend.Command += new CommandEventHandler(btnSend_Command);
            this.Controls.Add(btnSend);
        }

        void btnSend_Command(object sender, CommandEventArgs e)
        {
            this.Page.Response.Write("Du har nu klickat på skicka-knappen! <br /><br />");                
        }
    }
}

So when i click the button and the control is rendered as a WebControl, nothing happens. But if i change the WebControl to a CompositeControl, the text is printed out. Why?
Whats the difference between the WebControl and the CompositeControl?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从此见与不见 2024-08-15 04:44:22

CompositeControls 实现 INamingContainer,而 WebControls 则不实现。

两者之间存在更多差异,但这就是复合控件可以将事件路由到其子控件而 Web 控件不能的原因。您可以通过将类声明更改为以下内容来看到这一点:

public class TemplateControl : WebControl, INamingContainer

瞧,您的按钮事件现在将被处理!

INAmingContainer 是只是一个标记接口,告诉 ASP.NET 某个控件包含可能需要独立于其父控件进行访问的子控件,因此子控件会获得我们 ASP.NET 开发人员所了解和喜爱的额外漂亮 ID(例如,ctl00$ctl00)。

如果 WebControl 未实现 INamingContainer,则无法保证子级的 id 是唯一的,因此父级无法可靠地识别它,也无法转发它的事件。

CompositeControls implement INamingContainer while WebControls don't.

There are more differences between the two, but that's why the composite control can route the event to its child but the web control can't. You can see this by changing your class declaration to this:

public class TemplateControl : WebControl, INamingContainer

Voila, your button event will now be handled!

INamingContainer is just a marker interface that tells ASP.NET a control contains children that may need to be accessed independently of their parent control, so child controls get those extra pretty ids we ASP.NET developers have come to know and love (e.g., ctl00$ctl00).

If the WebControl doesn't implement INamingContainer, the child's id isn't guaranteed to be unique, so the parent can't reliably identify it and can't forward it events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文