Get item id in ASP.NET Listview when button is clicked

I have a button in each line of the list.
I have a click event method in which I would like to get the item id.
I placed hidden fieldin the list to save the identifier.
However

ListView1.FindControl("hiddenField") is returning null. 

I tried several different event arguments, but only this command works with this button, so there is nothing in the event arguments.
I tried to set the command button call commandEventArgs, but no matter what args events I use besides "eventArgs", I get:

No overload for 'btnManage_Click' matches delegate 'System.EventHandler'

How do I get the args event in this button?

EDIT

The button is located in the ItemTemplate section of the following ListView.

<asp:ListView ID="ListView1" runat="server" DataSourceID="LinqDataSource1" EnableModelValidation="True" DataKeyNames="Id" InsertItemPosition="LastItem" >

 <LayoutTemplate>
              <table id="Table2" class="table" runat="server">
                <tr id="Tr1" runat="server">
                  <td id="Td1" runat="server">
                    <table runat="server" id="itemPlaceholderContainer" style="background-color: #FFFFFF; border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px; font-family: Verdana, Arial, Helvetica, sans-serif;" border="1">
                      <tr id="Tr2" runat="server" style="background-color: #DCDCDC; color: #000000;">
                        <th id="Th1" runat="server"></th>
                        <th id="Th2" runat="server">Name</th>
                        <th id="Th3" runat="server">Manage</th>
                      </tr>
                      <tr runat="server" id="itemPlaceholder"></tr>
                    </table>
                  </td>
                </tr>
                <tr id="Tr3" runat="server">
                  <td id="Td2" runat="server" style="text-align: center; background-color: #CCCCCC; font-family: Verdana, Arial, Helvetica, sans-serif; color: #000000;"></td>
                </tr>
              </table>
            </LayoutTemplate>
+5
source share
3 answers

You can use this as

 <asp:Button runat="server" CommandArgument='<%# DisplayIndex %>'/>

Check API

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewcommandeventargs.aspx

If you have a button, just

 protected void templateButton_OnClick(object sender, EventArgs e)
 {
     Button myButton = (Button)sender;
 }

More details

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemcommand.aspx

Change 1

Suppose you have an id property and you can use this as

  CommandArgument='<%#Eval("id")' 

Edit 2

Because it is case sensitive and you have

 DataKeyNames="Id"

So you should use the following

CommandArgument='<%#Eval("Id")' 

and use it as it should

 int i = Convert.ToInt32(myButton.CommandArgument.ToString());
+7
source
You can try this: 

ListView lv = (ListView)sender 

Here, the sender is a parameter of the buttonclick event. then try to get the information.

0

, LinkButton:

<asp:LinkButton ID="LinkButton1" 
                runat="server" 
                Text="Quick View" 
                Font-Size="Small" 
                OnCommand="QuickView" 
                CommandArgument='<%# Eval("Hoarding_Id") %>' >   
</asp:LinkButton>

...

0

All Articles