问题
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.
回答1:
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:
protected void Page_Load(object sender, EventArgs e)
{
// Put the all code you need to run with EVERY request here
// Then add a post back check for any code you want to ONLY run on the very
// first request to the page.
if (!IsPostBack)
{
// Code you only want to run the first time. Usually setup code to initalize
// DropDownLists, Grids or pre populate a form, etc...
}
}
Your LinkButton
code should be all on it's own in a click handler:
protected void yourLinkButton_Click(object sender, EventArgs e)
{
// code you want to execute when your button is clicked. This will run AFTER
// the Page_Load has finished.
}
Now if your LinkButton
is used in a GridView
, Repeater
or some type of control that requires binding to fill it in, then you will probably need to implement a RowCommand
event handler to figure out which LinkButton
is pressed. You can also bind data to it's CommandArgument
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 the sender
and then compare ID
values.
protected void yourLinkButton_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
if (btn.ID.Equals("Some control name you want to compare"))
{
// do something
}
}
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 the Page_Load
due to some other constraints. Well there is no clean way of doing it in the Page_Load
but it can be done. You will need to check the Request.Form
keys and check for a specific button name (only the Button
that was clicked should be included in the keys). For example:
if (Request.Form.AllKeys.Contains("yourButton1"))
{
// then do something based on yourButton1
}
else if (Request.Form.AllKeys.Contains("yourButton2"))
{
// then do something based on yourButton2
}
// etc...
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 a LinkButton
you can use the following to get what control caused the post back:
string controlNameThatCausedPostBack = Request.Form["__EVENTTARGET"];
I have tested this with a LinkButton
and it does work as expected.
Hope that finally solves your problem :)
回答2:
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.
回答3:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack && Request.Form["__EVENTTARGET"] == LinkButton1.UniqueID)
{
// LinkButton1 was clicked
}
}
回答4:
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:
if (!IsPostBack)
(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
回答5:
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.
回答6:
Separate handlers are the way to go here, but you could give each button a unique ItemCommand
property, filter on those as necessary.
来源:https://stackoverflow.com/questions/3551353/how-to-verify-which-linkbutton-is-clicked-in-asp-net-on-a-page-load