I have created 3 LinkButton
they all have same onclick
event, now I have to find which button has called onclick event and it’s ID
<asp:LinkButton ID="LinkButton1" runat="server" onclick="lnk_Click"开发者_如何学编程/>
<asp:LinkButton ID="LinkButton2" runat="server" onclick="lnk_Click"/>
<asp:LinkButton ID="LinkButton3" runat="server" onclick="lnk_Click"/>
From the sender object of course:
LinkButton btn = (LinkButton)sender;
btn.ID
protected void MyBtnHandler(Object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
switch (btn.CommandName)
{
case "ThisBtnClick":
DoWhatever(btn.CommandArgument.ToString());
break;
case "ThatBtnClick":
DoSomethingElse(btn.CommandArgument.ToString());
break;
}
}
You're function should have a "sender" object
.
void lnk_Click(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
if (btn != null)
{
String id = btn.ID;
//etc
}
}
Cast the sender to LinkButton
, from there you can get the id of the button that was clicked.
LinkButton lb = (LinkButton)sender;
string ID = lb.ID; //the id of the button that was clicked
You can do
LinkButton lb = sender as LinkButton;
and then access it's properties. However, if your logic differs enough it may be good practice to seperate out into more than one handler.
精彩评论