嵌套用户控件中的 LinkBut​​ton 未触发 Command 事件

发布于 2024-07-20 04:26:16 字数 1226 浏览 8 评论 0原文

我有一个嵌套的 UserControl (此控件由另一个 UserControl 动态加载,该控件由 MasterPage 内的 aspx 页面动态加载),我想在其中使用 LinkBut​​tonOnCommand 事件。

此按钮必须添加到某个面板,因此我连接到面板的 OnLoad 事件(需要在生命周期中触发事件之前创建它):

protected void PatentAssignee_Load(object sender, EventArgs e) {
    Label label = (Label)sender;

    LinkButton link = new LinkButton();
    link.Text = name;
    link.Command += Link_OnCommand;
    link.CommandArgument = "argument";
    link.ID = "someID";

    label.Controls.Add(link)
}

protected void Link_OnCommand(object sender, CommandEventArgs e) {
    Response.Write(e.CommandArgument);
}

但我无法获取 Link_OnCommand被调用的方法。 与此问题相关的另一件事是:UserControl 位于 UpdatePanel 内。 我还尝试使链接触发完整回发:

ScriptManager s = (ScriptManager)this.Page.Master.FindControl("__scriptManager");
s.RegisterPostBackControl(link);

...但它没有太大变化,页面已完全重新加载,但没有触发任何事件。

编辑:根据评论中的要求,以下是有关嵌套的更多详细信息:

MasterPage
  PlaceHolder
    Page
      UpdatePanel
        UserControl1
          FormView
            PlaceHolder
              UserControl2
                LinkButton

这意味着 UserControl2 是动态加载的。

I have a nested UserControl (this control is dynamicall loaded by another UserControl that is dynamically loaded by an aspx page inside a MasterPage) in which I would like to use a LinkButton and the OnCommand Event.

This button must be added to some panel, so I hooked up to the OnLoad event of the panel (it needs to be created before events are fired in the lifecycle) :

protected void PatentAssignee_Load(object sender, EventArgs e) {
    Label label = (Label)sender;

    LinkButton link = new LinkButton();
    link.Text = name;
    link.Command += Link_OnCommand;
    link.CommandArgument = "argument";
    link.ID = "someID";

    label.Controls.Add(link)
}

protected void Link_OnCommand(object sender, CommandEventArgs e) {
    Response.Write(e.CommandArgument);
}

But I can't get the Link_OnCommand method to be called. One more thing that my be relevant to this problem : the UserControl is inside an UpdatePanel. I also tried to make the link trigger a full postback :

ScriptManager s = (ScriptManager)this.Page.Master.FindControl("__scriptManager");
s.RegisterPostBackControl(link);

... but it doesn't change much, the page is fully reloaded but no event is fired.

EDIT: As requested in the comments, here are more details about the nesting :

MasterPage
  PlaceHolder
    Page
      UpdatePanel
        UserControl1
          FormView
            PlaceHolder
              UserControl2
                LinkButton

This means that UserControl2 is dynamically loaded.

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

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

发布评论

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

评论(3

[旋木] 2024-07-27 04:26:16

层次结构中的一个控件缺少 ID。 设置 ID,事件就会触发。 这也会对 AjaxControlToolkit 造成严重破坏(确保每个扩展程序都有一个 ID,否则客户端行为通常会无法加载)。

One of your controls in the hierarchy is missing an ID. Set the ID, and the event will fire. This also causes havoc with the AjaxControlToolkit (make sure every extender has an ID or clientside behaviors will often fail to load).

月下凄凉 2024-07-27 04:26:16

我记得也遇到过这个问题。 我没有找到真正的问题,但我所做的是将事件处理程序连接到 aspx 标记中,并且它起作用了。

您还可以尝试在页面生命周期的某个早期阶段挂钩事件处理程序。

尝试 PreInit 事件处理程序。

有关页面生命周期的详细信息请参见此处

I remember having this problem as well. I didn't find the real problem, but what I did, was hook up the eventhandler in the aspx markup and it worked.

You might also try to hook the eventhandler in some earlier stage of the Page Life Cycle.

Try the PreInit eventhandler.

More info on the Page Life Cycle here.

对岸观火 2024-07-27 04:26:16

对于ajax,我认为你可以创建一个可重用的类

public void registerUCAsyncPostBack(Page page, WebControl webcontrol, UserControl usercontrol){
            Control ControlAjaxNew = null;
            if (webcontrol.GetType() == typeof(LinkButton))
            {
                ControlAjaxNew = (LinkButton)usercontrol.FindControl(webcontrol.ID);
            }
            ScriptManager.GetCurrent(page).RegisterAsyncPostBackControl(ControlAjaxNew);
        }

for the ajax, I think you can create a reusable class

public void registerUCAsyncPostBack(Page page, WebControl webcontrol, UserControl usercontrol){
            Control ControlAjaxNew = null;
            if (webcontrol.GetType() == typeof(LinkButton))
            {
                ControlAjaxNew = (LinkButton)usercontrol.FindControl(webcontrol.ID);
            }
            ScriptManager.GetCurrent(page).RegisterAsyncPostBackControl(ControlAjaxNew);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文