Click here to Skip to main content
15,943,233 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

This is my button code

ASP.NET
<asp:ImageButton ID="Img1" ImageUrl='<%#Eval("ProductImage") %>' runat="server" Width="30px" Height="30px" OnClick="MakeImg1Main" />



Like this i have no.of imagebuttons in my code,
my intension is to know which button is clicked.
how to get the clicked control id in code behind.


Please help me

Thanks in advance
Posted
Updated 28-Sep-12 1:17am
v2

Hi,

if your event code is like this,

for direct event.
C#
protected void MakeImg1Main(object sender, EventArgs e)
{
   //Then try like this.
   string ID = ((ImageButton)sender).ID;
   string CLientID = ((ImageButton)sender).ClientID;
}


with datalist, add item command event for datalist.

add the command name
ASP.NET
<asp:ImageButton ID="Img1" ImageUrl='<%#Eval("ProductImage") %>' runat="server" Width="30px" Height="30px" CommandName="Image" />


C#
//In ItemCommand Event,
if(e.CommandName == "Image")
{
  string ID = ((ImageButton)e.CommandSource).ID;
  string CLientID = ((ImageButton)e.CommandSource).ClientID;
}



Hope it helps.
 
Share this answer
 
v3
Comments
sasipriyakalpana 28-Sep-12 7:19am    
My image button is in datalist.
I tried that but event not fired.
Karthik Harve 28-Sep-12 7:58am    
it may be firing ItemCommand Event. try my updated answer.
Hi,

Give click event as MakeImg1Main to all imagebuttons and from the click event type cast your sender to imagebutton and get its id...

like

C#
private void MakeImg1Main(object sender,EventArgs e)
{
   ImageButton img=(ImageButton)sender;

// this will get you the id of the image button clicked
   Response.Write(img.Id.ToString());
}


Hope it helps..

Krunal
 
Share this answer
 
v2
Comments
sasipriyakalpana 28-Sep-12 7:20am    
I tried this but event was not fired.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900