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.
相关问题
- Carriage Return (ASCII chr 13) is missing from tex
- Generic Generics in Managed C++
- How to store image outside of the website's ro
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
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
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 :)
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.
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.
Separate handlers are the way to go here, but you could give each button a unique
ItemCommand
property, filter on those as necessary.