views:

1509

answers:

2

Hello everyone,

I have an question, On my page i have a ListView control, I also have a button that has CommandArgument. I know i can read and find a control in the ListView as :

ListView.Items[i].FindControl("controlname");

and my button in ListView is like that

 asp:Button ID="WisdomButton" runat="server" CommandName="UpdateWisdom"  CommandArgument='<%# need my index for Item[i] to post here %>' 
                                  OnCommand="UpdateWisdom" Text="Update"   />

I want to add index value in runtime to the CommantParameter, so when i go to the Function onCommand i will know exactly from what row[i] i Need to get my controls from ListView.

So my question is, how do i dinamicly add index of ListView.Rows[i] into CommmandArgument for the Button in runtime ?

Thanks in advance.

+1  A: 

Check out the API

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

The ListViewCommandEventArgs item has an index, IE it is already available in the argument

ListViewDataItem dataItem = (ListViewDataItem)e.Item;
int i = dataItem.DisplayIndex;

But from here you will have access to those controls

e.Item.FindConrol("controlName");

If you are calling the method a different way you could aways assign the index through an ItemDataBound Event

void MyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    ((Button)e.Item.FindControl("WisdomButton")).CommandArgument = ((ListViewDataItem)e.Item).DisplayIndex;
}

OR try something like this for giggles

<asp:Button runat="server" CommandArgument='<%# DisplayIndex %>'/>
// OR
<asp:Button runat="server" CommandArgument='<%# NextIndex() %>'/>
Steve
Feel like it close to what i need, but cannot make your solution to work...
Dmitris
e.Item gives you the selected/clicked ListViewDataItem row which you can then find the child controls. This ListViewDataItem control retains its own index so you don't need to create your own
Steve
A: 

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 ListViewCommandEventArgs of the ItemCommand event. You don't need to retain the index or call FindControl at all.

Here is an example of how to get the bound Customer object.

ListView

<asp:ListView runat="server" id="Customers" ItemCommand="Customers_ItemCommand">
  <LayoutTemplate>
    <ul>
      <asp:placeholder runat="server" id="itemPlaceholder" />
    </ul>
  </LayoutTemplate>
  <ItemTemplate>
    <li>
      <asp:Button runat="server" id="Select" CommandName="Select" />
      <%# Eval("Name")%>
    </li>
  </ItemTemplate>
</asp:ListView>

CodeBehind

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);
    }
  }
}

Edit: In addtion when you cast the item to a ListViewDataItem, then you also expose a ((ListViewDataItem)e.Item).DataItemIndex property that might help you.

bendewey
For the <asp:Button runat="server" id="Select" CommandName="Select" />For it's commandArgument, i am trying to set value that is index of array that ListView creates
Dmitris
The point is you might not need the index if you can just get direct access to your dataItem.
bendewey
I added an edit to show the DataItemIndex if you need that.
bendewey