views:

356

answers:

6

Hello there,

I have a asp:ListView control that I bind with a List.

When Editing records in this ListView control, I can always get the Unique Id of record being edited by using:

int id = Convert.ToInt32(lstView1.DataKeys[e.NewEditIndex].Value);

Is it possible to get the whole object that is being edited, using any of the ListView properties?

Thank you!

+1  A: 

When you click on the edit for a given item in the listview the ItemCommand event gets fired. The arguments for that event tell that you can get the list item for which that event was fired. You will have to typecast that item properly to get the information you require. The itemcommand event looks like this

    protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
    {

    }

you have e.Item to use from the ListViewCommandEventArgs.

Bootcamp
in _ItemCommand event handler, I tried following ListViewDataItem objCurrentItem = (ListViewDataItem)e.Item; CustomObject obj = (CustomObject)objCurrentItem.DataItem; } But objCurrentItem.DataItem always comes out to be null here... However this works fine in _ItemDataBound handler. Any idea?
inutan
+3  A: 

I just figured that out,

We can get the object being edited using following code-

protected void lstView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    ListViewDataItem objCurrentItem = (ListViewDataItem)e.Item;
    **CustomObject obj = (CustomObject)objCurrentItem.DataItem;**

    if (objCurrentItem.DisplayIndex == lstView1.EditIndex)
    {
        TextBox txtTitle = (TextBox)objCurrentItem.FindControl("txtTitle");
        txtTitle.Text = obj.Title;
    }
}
inutan
+1  A: 

This seems a good solution. I am glad you posted this code here.

Bootcamp
+2  A: 

Here is the answer to your comment to my question:

Yes, the reason it is null in itemcommand and works fine in itemdatabound is that the location itemcommand is not correct for reading this value. You will always get the DataItem null in ItemCommand, no matter what you do. The reason lies in the control life cycle. The control gets initialized, created and then only does any other event related to the control can fire. During control creation the CreateControlHierarchy is called which then uses the DataBind event to create and databind the child controls. At that time the DataItem is live and is not null. Before that and after that it is always null, because its role lies only for that much time span.

By the way the DataItem you are looking at is the item from the related datasource that is being used to databind the listview. The datasource is used only during databinding, hence the DataItem is available only during Item Databound.

Hope this helps !

Bootcamp
Thank you for taking time explaining this to me. Can you think of any work around to get the Object being edited in _ItemUpdating or _ItemCommand event handler? Right now, I have stored my List<Objects> in ViewState and just retrieving the object being edited by filtering with lstView1.DataKeys[e.ItemIndex].Value in _ItemUpdating handler.
inutan
+1  A: 

You item updating you don't have the object available for modifying. You only have the collection of the properties and their values in the new values and old values collections that you get from event arguments. I suppose you can edit the properties of the item over there. Its more or less similar to editing the object itself, because eventually these property values will get transferred to the object using reflection.

Bootcamp
+1  A: 

I thought you would appreciate some of my answers :)

Were you able to solve your problem ? If not send me your code on my email id [email protected] and i shall try and provide you a solution .

Bootcamp