如何验证在 Page_Load 上的 asp.net 中单击了哪个 LinkButton
如何检查在页面的 Page_Load
中单击了哪个 LinkButton
。 这是为了避免调用服务,以便它只执行其事件中存在的内容。
How do I check which LinkButton
is clicked in the Page_Load
of the page.
This is to avoid calls to service so that it only executes what is present in its event.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Page_Load
中唯一应该运行的是您希望每次请求时始终发生的代码,或者您只想在回发检查中运行一次的代码。例如:您的
LinkButton
代码应该完全独立于点击处理程序中:现在,如果您的
LinkButton
在GridView
中使用,Repeater
或某种需要绑定来填充它的控件,那么您可能需要实现一个RowCommand
事件处理程序来确定哪个LinkButton
是按下。您还可以将数据绑定到其CommandArgument
属性,以将一些唯一的行特定数据传递给偶数处理程序。如果您有一堆
LinkButton
都使用完全相同的处理程序,最坏的情况是强制转换sender
,然后比较ID
值。如果我对你的问题偏离了主题,请发表评论,我会尽力解决。
编辑:根据您的评论,由于其他一些限制,您似乎必须知道它在
Page_Load
中的哪个Button
。好吧,在Page_Load
中没有干净的方法可以做到这一点,但它可以完成。您需要检查Request.Form
键并检查特定的按钮名称(只有被单击的Button
才应包含在键中)。例如:我认为没有任何其他干净的方法可以解决它。如果框架在
sender
属性之一中包含导致回发的控件,那就太好了。另一个编辑:这完全让我忘记了。您需要对
Button
执行上述编辑,因为它不会填充__EVENTTARGET
。由于您使用的是LinkButton
,您可以使用以下命令来获取导致回发的控件:我已经使用
LinkButton
对其进行了测试,它确实按预期工作。希望最终能解决您的问题:)
The only thing that should be running in your
Page_Load
is the code you want to happen always with everyt request, OR have the code that you only want to run once wrapped in a post back check. For example:Your
LinkButton
code should be all on it's own in a click handler:Now if your
LinkButton
is used in aGridView
,Repeater
or some type of control that requires binding to fill it in, then you will probably need to implement aRowCommand
event handler to figure out whichLinkButton
is pressed. You can also bind data to it'sCommandArgument
property to pass some unique row specific data to the even handler.If you have a bunch of
LinkButton
s all using the exact same handler, the worst case scenario is to cast thesender
and then compareID
values.If I am way off the mark on your question, just leave a comment and I will try and sort it out.
EDIT: Based on your comment it sounds like you have to know which
Button
it is in thePage_Load
due to some other constraints. Well there is no clean way of doing it in thePage_Load
but it can be done. You will need to check theRequest.Form
keys and check for a specific button name (only theButton
that was clicked should be included in the keys). For example:I don't think the is any other clean way to get around it. It would have been nice if the framework included the control that caused the postback in one of the
sender
properties.Another Edit: This totally slipped my mind. The above edit it was you need to do for a
Button
since it does not populate__EVENTTARGET
. Since you are using aLinkButton
you can use the following to get what control caused the post back:I have tested this with a
LinkButton
and it does work as expected.Hope that finally solves your problem :)
我曾经不得不这样做,因为在事件处理程序中为时已晚。您可以使用 javascript 编写被单击到隐藏字段的按钮的 ID。您可以在 Page_Load 中检索它。如果我没记错的话,另一种方法是查看 _EVENTTARGET 隐藏字段,已经有一段时间了。
I used to have to do this, because in event handler it was too late. You could use javascript to write an Id of the button that was clicked into hidden field. Than you can retrieve it in the Page_Load. Other way would be to look into _EVENTTARGET hidden field if I remember correctly, it's been a while.
您的意思是您只是试图在页面上的其他回发事件被触发时阻止执行 Page_Load 代码吗?如果是这种情况,请将其包含在:(
恕我直言,这是 WebForms 事件模型所需的一点破解,但事实就是如此。)
或者我误解了您的问题?
至于标识特定的 LinkButton,可以使用 CommandName 参数:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.commandname.aspx
Do you mean that you're just trying to keep the Page_Load code from being executed when other post-back events on the page are fired? If that's the case, wrap it in:
(IMHO this is a bit of a hack necessitated by the WebForms event model, but it is what it is.)
Or am I misunderstanding your question?
As for identifying a specific LinkButton, you can use the CommandName parameter:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.commandname.aspx
您还可以声明 LinkButton(或 Control,或其他)类型的变量,并让按钮在单击事件中使用对自身的引用来更新该变量,然后检查 Load 时的值。
You could also declare a variable of type LinkButton (or Control, or whatever) and have the button(s) update that with a reference to themselves in the click event, then check the value at Load.
单独的处理程序是此处的方法,但您可以为每个按钮提供唯一的
ItemCommand
属性,并根据需要过滤这些属性。Separate handlers are the way to go here, but you could give each button a unique
ItemCommand
property, filter on those as necessary.