tags:

views:

22

answers:

2

I have suppliers table with id int value for each supplier. I'm trying to edit columns of this table inside of ListView.

i'm trying to access e arg but it doesn't have any data on id of the row i'm trying to update?

+1  A: 

use the sender instead of the e

protected void ListView1_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
{
    if ((sender as ListView) != null)
    {

        ListView l = (ListView)sender;
        l.SelectedIndex;
        l.etc..............
        DataKey key = l.SelectedDataKey;
        object k = key.Values["foo"];        
    }
 }
Caspar Kleijne
Thanks, @Caspar Kleijne. How do i get id sql value of current item i edit?
eugeneK
@Caspar Kleijne, i get null object exception on Response.Write(((ListView)sender).SelectedDataKey.Value.ToString()); I did add to ListView DataKeyNames='id'
eugeneK
is the id in your listview? ( Besides that it declared as a datakeyname? )
Caspar Kleijne
nopes... should i add it in non-editable control?
eugeneK
yep, make the item Visible=false and Readonly=true (it won't be send to the client)
Caspar Kleijne
works, thanks!!!!
eugeneK
Stateless ASP.NET is. You remember must that... always...
Caspar Kleijne
Actually, that was the clue to your whole problemm.... the event (ee) should have the propertis as well now...
Caspar Kleijne
+1  A: 

You need to set the ListView.DataKeyNames to contain the property name of the uniqueId

<asp:ListView runat="server" ID="LvSample" DataKeyNames="SupplierId"/>

Then the dataKey will be available in the update events. It is worth nothing that you might want to use the ItemUpdating event instead of the ItemUpdated as this happens before Update occurs.

        protected void Page_Load(object sender, EventArgs e)
    {
        LvSample.ItemUpdating += new EventHandler<ListViewUpdateEventArgs>(LvSample_ItemUpdating);
        LvSample.ItemUpdated += new EventHandler<ListViewUpdatedEventArgs>(LvSample_ItemUpdated);

    }

    void LvSample_ItemUpdated(object sender, ListViewUpdatedEventArgs e)
    {
        var supplierId = e.NewValues["SupplierId"];
    }

    void LvSample_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {
        var supplierId = e.Keys["SupplierId"];
    }
iain