Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ID of item in ASP.NET Listview on button click

I have a button on each row of a listview.
I have a method on the click event in which I would like to get the ID of the data item.
I placed a hidden field in the list view to store the ID.
However,

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

I have tried several different event args, but only the blank event args is working with this button, so I don't have anything in the event args.
I tried to set the command of the button to call commandEventArgs, but no matter what event args I use besides "eventArgs" I get:

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

How do I get the event args into this button?

EDIT

Button is in ItemTemplate section of 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>
like image 834
hyperGeoMetric Avatar asked Mar 11 '13 04:03

hyperGeoMetric


1 Answers

You can use like this

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

Check out the API

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

If you have a button then simply

 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

Edit 1

Suppose that you have a property "id" then you can use like this

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

Edit 2

As it is case sensitive and you have

 DataKeyNames="Id"

So you should use as follows

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

and use it like follows

 int i = Convert.ToInt32(myButton.CommandArgument.ToString());
like image 196
शेखर Avatar answered Nov 16 '22 17:11

शेखर