带有子控件的 asp.net Web 服务器控件,事件未触发

发布于 2024-08-12 10:43:53 字数 2354 浏览 4 评论 0原文

我有一个简单的 Web 控件 (TaskList),它可以有从 LinkBut​​ton 继承的子项 (Task),可以以声明方式或以编程方式添加。这工作正常,但我无法在后面的代码中触发任务的 onclick 事件。代码 ..

    [ToolboxData("<{0}:TaskList runat=\"server\"> </{0}:TaskList>")]
[ParseChildren(true)]
[PersistChildren(false)]
public class TaskList : System.Web.UI.Control
{
    //[DefaultProperty("Text")]   
    public TaskList()
    {}

    private List<Task> _taskList = new List<Task>();

    private string _taskHeading = "";

    public string Heading
    {
        get
        {
            return this._taskHeading;
        }
        set
        {
            this._taskHeading = value;
        }
    }

    [NotifyParentProperty(true)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]      
    public List<Task> Tasks
    {
        get
        {
            return this._taskList;
        }

        set
        {
            this._taskList = value;
        }
    }


    protected override void CreateChildControls()
    {
        foreach (Task task in this._taskList)
            this.Controls.Add(task);

        base.CreateChildControls();
    }


    protected override void Render(HtmlTextWriter writer)
    {
        writer.Write("<h2>" + this._taskHeading + "</h2>");
        writer.Write("<div class='tasks_container'>");
        writer.Write("<div class='tasks_list'>");
        writer.Write("<ul>");
        foreach (Task task in this._taskList)
        {
                writer.Write("<li>");
                task.RenderControl(writer);
                writer.Write("</li>");
        }
        writer.Write("</ul>");
        writer.Write("</div>");
        writer.Write("</div>");
    }
}

public class Task : LinkButton
{
    private string _key = "";

    public string Key
    {
        get
        {
            return this._key;
        }
        set
        {
            this._key = value;
        }
    }

}

标记:

 <rf:TaskList runat="server" ID="tskList" Heading="Tasks">
    <Tasks>
        <rf:Task Key="ba" ID="L1" Text="Helllo" OnClick="task1_Click" runat="server" />
    </Tasks>
</rf:TaskList>

单击时,Onclick 事件 task1_Click 永远不会触发(尽管会发生回发)。

I have a simple web control (TaskList) that can have children (Task) which inherit from LinkButton, that can be added declaratively or programatically. This works ok, but I can't get the onclick event of a Task to be fired in my code behind. The code ..

    [ToolboxData("<{0}:TaskList runat=\"server\"> </{0}:TaskList>")]
[ParseChildren(true)]
[PersistChildren(false)]
public class TaskList : System.Web.UI.Control
{
    //[DefaultProperty("Text")]   
    public TaskList()
    {}

    private List<Task> _taskList = new List<Task>();

    private string _taskHeading = "";

    public string Heading
    {
        get
        {
            return this._taskHeading;
        }
        set
        {
            this._taskHeading = value;
        }
    }

    [NotifyParentProperty(true)]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]      
    public List<Task> Tasks
    {
        get
        {
            return this._taskList;
        }

        set
        {
            this._taskList = value;
        }
    }


    protected override void CreateChildControls()
    {
        foreach (Task task in this._taskList)
            this.Controls.Add(task);

        base.CreateChildControls();
    }


    protected override void Render(HtmlTextWriter writer)
    {
        writer.Write("<h2>" + this._taskHeading + "</h2>");
        writer.Write("<div class='tasks_container'>");
        writer.Write("<div class='tasks_list'>");
        writer.Write("<ul>");
        foreach (Task task in this._taskList)
        {
                writer.Write("<li>");
                task.RenderControl(writer);
                writer.Write("</li>");
        }
        writer.Write("</ul>");
        writer.Write("</div>");
        writer.Write("</div>");
    }
}

public class Task : LinkButton
{
    private string _key = "";

    public string Key
    {
        get
        {
            return this._key;
        }
        set
        {
            this._key = value;
        }
    }

}

Markup:

 <rf:TaskList runat="server" ID="tskList" Heading="Tasks">
    <Tasks>
        <rf:Task Key="ba" ID="L1" Text="Helllo" OnClick="task1_Click" runat="server" />
    </Tasks>
</rf:TaskList>

The Onclick event task1_Click never fires when clicked (although a postback occurs).

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

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

发布评论

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

评论(3

素手挽清风 2024-08-19 10:43:53

TaskList 需要实现 INamingContainer 以正确路由每个任务的事件。

TaskList needed to implement INamingContainer to correctly route the events of each Task.

一场信仰旅途 2024-08-19 10:43:53

您还可以从 CompositeControl 继承,它为您实现 INamingContainer,而不是从 Control 继承。

You could also inherit from CompositeControl, which implements INamingContainer for you, instead of inheriting from Control.

请帮我爱他 2024-08-19 10:43:53

您是否考虑过 INamingContainer。

Have you thought about INamingContainer.

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