c# - How to access a button and do some work when clicked in ListView ItemTemplate from code behinde? -


.aspx

  <asp:listview id="listviewusers" itemtype="user" runat="server" onitemcommand="listviewusers_itemcommand">                         <itemtemplate>                             <div class="center">                                 <table id="tableusers">                                     <tr>                                         <td><%#item.username%></td>                                         <td><asp:button id="btn_addfriend"                                              runat="server"  text="add friend" /></td>                                     </tr>                                 </table>                             </div>                         </itemtemplate>                                    ... 

.aspx.cs

protected void listviewusers_itemcommand(object sender, listviewcommandeventargs e) {       // how access button, , work when clicked. } 

the correct way of doing add commandname argument button , later catch inside eventhandler. example below:

<asp:button id="btn_addfriend" runat="server"  text="add friend" commandname="add friend" /> 

now can catch command in event handler this:

protected void listviewusers_itemcommand(object sender,listviewcommandeventargs e) {   if(string.equals(e.commandname, "add friend"))   {     // ... work here   } } 

referring msdn article https://msdn.microsoft.com/pl-pl/library/system.web.ui.webcontrols.listview.itemcommand%28v=vs.110%29.aspx


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -