asp.netimagebuttondatalistitemcommand

how to know which image button I click in datalist


I want to change imageurl which image button I click in datalist.So I must get a diffence propertie of imagebutton in datalist.My code is below;

<asp:DataList ID="datalistcevaplar" runat="server" 
          Width="740px" OnItemCommand="datalistcevaplar_ItemCommand" >
<ItemTemplate>
           <div class="divcvponay">
            <asp:ImageButton ID="imgbtncevaponayla" runat="server" OnCommand="tiklanan" ImageUrl="~/resimler/cevaponaybeyaz.jpg"/>
            </div>
 </ItemTemplate>
</asp:DataList>

and my cs.codes;

 protected void datalistcevaplar_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName=="tiklanan")
    {

    }
}

why it doesnt fire datalistcevaplar_ItemCommand event.And how do I get which imagebutton I clicked in datalist


Solution

  • If you just want a change your selected image url, first of all you must add CommandName your ImageButton element as a below.

    <asp:ImageButton ID="imgbtncevaponayla" runat="server" CommandName="btnimgbtncevaponayla" ImageUrl="~/resimler/cevaponaybeyaz.jpg"/>
    

    You find which button selected on your .aspx.cs file and you change that image url like that:

    if (e.CommandName == "btnimgbtncevaponayla")
    {
        ImageButton btn = e.CommandSource as ImageButton;
        btn.ImageUrl = "~/resimler/different.jpg";
    }