Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

A few days ago, I wrote about issues with implementing a ListView in ASP.NET. Now, with all of the other code written, I'm having trouble saving changed items in a ListView.

A few things of note:

  • The Save button is not part of the ListView proper; it calls the GetListViewItems() method, which in turns call the Save() method.
  • The Listview.DataBind() event is invoked when a button is pressed requesting the records to be updated
  • The Listview shows text using <%#Eval("Key.Name") %> and a named DropDownList using <%#Eval("Value") %>

Getting The Items From the ListView

public void GetListViewItems()
{
 List<Foo> Result = FooManager.CreateFooList();
 DropDownList ddl = null;
 ListViewItem Item = null;
    try
      {
       foreach (ListViewDataItem item in lvFooList.Items)
         {
          Item = item;
          ddl = ((DropDownList) (Item.FindControl("ddlListOfBars")));
          if (//something is there)
           {
            Foo foo = FooManager.CreateFoo();
            foo.Id = item.DataItemIndex; //shows null
            int barId = int.Parse(ddl.SelectedItem.Value); //works just fine
            foo.barId = barId;
            Result.Add(foo);
           }
         }
      }
   catch (Exception ex)
     {
             //Irrelevant for our purposes
     }
}

DataBinding the ListView

The code to databind the ListView is shown here in my previous question.

Question(s):

  1. Why is it that when I iterate through the ListViewDataItem in the Listview that each item is null?
  2. How can I retrieve the Foo.Id from the Dictionary?
  3. What else might I be missing?
  4. What would I use if I wanted to get that Id Programmatically based on what items were shown? As it is now, the current ListView is shown based on what Foos were selected. Those Foos selected are then displayed, and the user can change the Bar in the DropDownList, hit Save, and those changes are propogated.

Update

As it turns out, my problem was what leppie had said; and that was that I needed to specify DataKeyNames and use those to retain the information from the ListView.

Here's the code I added:

try
{
   int DataKeyArrayIndex = 0;
   foreach (ListViewDataItem item in lvFooList.Items)
     {
      Item = item;
      ddl = ((DropDownList) (Item.FindControl("ddlListOfBars")));
      if (//something is there)
       {
        Foo foo = FooManager.CreateFoo();
        Foo tempFoo = FooManager.CreateFoo();
        if (lvFooList != null)
        {
             tempFoo = ((Foo)(lvFooList.DataKeys[DataKeyArrayIndex].Value));
        }
         
        foo.Id = tempFoo.Id;
        int barId = int.Parse(ddl.SelectedItem.Value); //works just fine
        foo.barId = barId;
        Result.Add(foo);
        DataKeyArrayIndex++;
     }
   }
}

And then in the .ascx file, I added DataKeyNames="Key", like so:

<asp:ListView ID="lvFooList" runat="server" DataKeyNames="Key">

This allowed me to use the Key from my previous post to determine which Foo was being looked at.

Any critiques of this approach, as well as methods for making it better are greatly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
276 views
Welcome To Ask or Share your Answers For Others

1 Answer

Some quick answers:

  1. Your need to use databinding for that to work, in other words, assign to DataSource and call DataBind(). EDIT: seems you are doing that. But remember it wont persist between postbacks, just the DataKey (see below).

  2. If I recall correctly, you need to specify the DataKeyNames, and they can be retrieved from the DataKey property then.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...