C# 动态创建 LinkButton 命令事件处理程序
所以我这里有一个奇怪的情况...我有一个 System.Web.UI.WebControls.WebParts.EditorPart 类。 它呈现一个“搜索”按钮,当您单击此按钮时,它的 clickHandler 方法会执行数据库搜索,并为其返回的每一行动态创建一个 LinkButton,设置 CommandName 和 CommandArgument 属性并添加 CommandEventHandler 方法,然后添加 LinkButton 控件到页面。
问题是,当您单击 LinkButton 时,它的 CommandEventHandler 方法永远不会被调用,看起来页面只是回发到按下原始“搜索”按钮之前的位置。
我看过一些帖子说您需要在 OnLoad() 或其他一些早期方法中添加事件处理程序,但直到用户告诉我们要搜索什么并点击“搜索”按钮之前,我的 LinkButton 甚至还没有创建。关于如何处理这个问题有什么想法吗?
谢谢!
So I have a weird situation here... I have an System.Web.UI.WebControls.WebParts.EditorPart class. It renders a "Search" button, when you click this button, it's clickHandler method does a DB search, and dynamically creates a LinkButton for each row it returns, sets the CommandName and CommandArgument properties and adds a CommandEventHandler method, then adds the LinkButton control to the page.
The problem is, when you click a LinkButton, its CommandEventHandler method is never called, it looks like the page just posts back to where it was before the ORIGINAL "Search" button was pressed.
I have seen postings saying that you need to add the event handlers in OnLoad() or some other early method, but my LinkButtons haven't even been created until the user tells us what to search for and hits the "Search" button... Any ideas on how to deal with this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我最喜欢的技巧:)
我们的场景是首先渲染一个控件。 然后使用用户的一些输入,呈现进一步的控件并让它们响应事件。
这里的关键是状态——你需要知道控件到达PostBack时的状态——所以我们使用ViewState。 那么这个问题就变成了先有鸡还是先有蛋的问题。 ViewState 在
LoadViewState()
调用之后才可用,但您必须在该调用之前创建控件才能正确触发事件。诀窍是重写 LoadViewState() 和 SaveViewState() 以便我们可以控制事物。
(请注意,下面的代码很粗糙,仅凭记忆,可能存在问题)
This is my favorite trick :)
Our scenario is to first render a control. Then using some input from the user, render further controls and have them respond to events.
The key here is state - you need to know the state of the control when it arrives at PostBack - so we use ViewState. The issue becomes then a chicken-and-egg problem; ViewState isn't available until after the
LoadViewState()
call, but you must create the controls before that call to have the events fired correctly.The trick is to override
LoadViewState()
andSaveViewState()
so we can control things.(note that the code below is rough, from memory and probably has issues)
您需要在加载中重新添加动态创建的控件,以便它们可以位于页面层次结构中并触发其事件。
You need to re-add the dynamically created controls, in the onload, so that they can be in the page hierarchy and fire their event.
我刚刚想出的一个肮脏的技巧是创建与真实按钮具有相同 ID 的虚拟 LinkButton。
假设您要在 Pre_Render 处创建一个 LinkButton“foo”(为时已晚),然后在 Page_Load 处创建一个虚拟 foo:(
其中“dummyButtons”只是页面上的一个占位符,其可见性设置为 false。)
它很丑,但很有效。
A dirty hack I just came up with, is to create dummy LinkButtons with the same IDs as the real buttons.
So let's say you are going to create a LinkButton "foo" at Pre_Render (which is too late), then also create a dummy foo at Page_Load:
(Where "dummyButtons" is just a PlaceHolder on the page with Visibility set to false.)
It's ugly, but it works.