Search code examples
c#asp.netgridviewitemtemplate

GridView item template linkbutton catch on code behind


I have a gridview in my asp.net project and i used item template like;

<asp:TemplateField>
                  <ItemTemplate>
                        <asp:LinkButton ID = "lnkSil" runat="server" CommandName="bla" 
CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' Text= "Sil" ></asp:LinkButton>
                  </ItemTemplate>
                  </asp:TemplateField>

And I want to catch this from code behind and i used below code ;

((Button)e.Row.Cells[1].Controls[0]).Attributes.Add("onclick", "return confirm('Bu kaydi silmek istediginizden emin misiniz?')");

but I'm failed how can i catch this controls on code behind like this

also i tried

((LinkButton)e.Row.Cells[1].Controls[0]).Attributes.Add("onclick", "return confirm('Bu kaydi silmek istediginizden emin misiniz?')");

Solution

  • if you want to get access to the event without javascript then use the GridView1_RowCommand.

     protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
     {
         int currentRowIndex = Int32.Parse(e.CommandArgument.ToString());
         LinkButton bf = (LinkButton)gv.Rows[currentRowIndex].Cells[1].Controls[0];
         ...
     }