Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It might help to know a little bit more about what your are trying to do. If your end goal is to get any of the properties from your bound object you can just cast the dataItem from the <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewcommandeventargs.aspx" rel="nofollow noreferrer">ListViewCommandEventArgs</a> of the ItemCommand event. You don't need to retain the index or call FindControl at all.</p> <p>Here is an example of how to get the bound Customer object.</p> <p><strong>ListView</strong></p> <pre><code>&lt;asp:ListView runat="server" id="Customers" ItemCommand="Customers_ItemCommand"&gt; &lt;LayoutTemplate&gt; &lt;ul&gt; &lt;asp:placeholder runat="server" id="itemPlaceholder" /&gt; &lt;/ul&gt; &lt;/LayoutTemplate&gt; &lt;ItemTemplate&gt; &lt;li&gt; &lt;asp:Button runat="server" id="Select" CommandName="Select" /&gt; &lt;%# Eval("Name")%&gt; &lt;/li&gt; &lt;/ItemTemplate&gt; &lt;/asp:ListView&gt; </code></pre> <p><strong>CodeBehind</strong></p> <pre><code>public void Page_Load() { if (!Page.IsPostBack) { this.Customers.DataSource = GetCustomers(); this.Customers.DataBind(); } } public void Customers_ItemCommand(object sender, ListViewCommandEventArgs e) { if (e.CommandName == "Select") { if (e.Item.ItemType != ListViewItemType.DataItem) return; var customer = ((ListViewDataItem)e.Item).DataItem as Customer; if (customer != null) { // Now work directly with the customer object. Response.Redirect("viewCustomer.aspx?id=" + customer.Id); } } } </code></pre> <p><strong>Edit:</strong> In addtion when you cast the item to a ListViewDataItem, then you also expose a ((ListViewDataItem)e.Item).<a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewdataitem_properties.aspx" rel="nofollow noreferrer">DataItemIndex</a> property that might help you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload