嵌套用户控件中的 LinkButton 未触发 Command 事件
我有一个嵌套的 UserControl
(此控件由另一个 UserControl
动态加载,该控件由 MasterPage 内的 aspx 页面动态加载),我想在其中使用 LinkButton
和 OnCommand
事件。
此按钮必须添加到某个面板,因此我连接到面板的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
层次结构中的一个控件缺少 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).
我记得也遇到过这个问题。 我没有找到真正的问题,但我所做的是将事件处理程序连接到 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.
对于ajax,我认为你可以创建一个可重用的类
for the ajax, I think you can create a reusable class