List View Problems...



  • Hi,

    Im sure that Im having way too manu problems with list views!

    What I want to do is have a list view which shows the shopping basket on the site. I want the quantity column to always be a text box, what I then want is to have an "Update" button at the bottom of the page.

    Up to this point all is fine... The problem is when that button is clicked how do I loop through the list view items to do the update?
    Using a repeater or datalist this was relativly easy, now it seems that I cant actually access the data item any more..

    What I have tried is (is pseudo code)

    Button_Clicked(params)

    {

    foreach (ListViewItem in lvBasket.Items)

    {

    attempt to get the data item

    call the method that updates the basket datasource.

    }

    }

     

    As I said, Im sure this shouldnt be that difficult, but at this time of night its got me stumpped....



  •  What language/framework/etc are you using? Do you want to write a web application or what othe setup are you in? What kind of an "update" do you want to do?



  • @PSWorx said:

    What language/framework/etc are you using? Do you want to write a web application or what othe setup are you in? What kind of an "update" do you want to do?

    Judging by the mentioning of a website and the use of the foreach keyword and typical .net framework class names, I'd say wonkoTheSane is using asp.net.

    @wonkoTheSane said:

    The problem is when that button is clicked how do I loop through the list view items to do the update?
    Using a repeater or datalist this was relativly easy, now it seems that I cant actually access the data item any more..

    Clicking the button triggers a postback. During a postback, the default behaviour of templated list controls like repeaters is to reconstruct the template contents from information in the viewstate. The DataItem property isn't recovered this way. (Probably this is a design decision that was made because you can bind any kind of object to a list as a datasource, where the object does not necessarily have to be serializable.)

    What you want to do, wonko, is store some kind of unique identifier and use that to recover the DataItem, probably by requerying the database for it. This is all very easy if you have an update button for every item in the list, in which case you can just set the button's CommandArgument property during the ItemDataBound event in the initial HTTP request. For a single update button at the bottom of the list, the problem becomes slightly more interesting. You'll probably want to store some hidden input field in each ListViewItem, containing the unique identifier for that ListViewItem's DataItem. When looping through the ListViewItems, you can query for that hidden input with the FindControl() method, pull out the unique identifier and re-retrieve the DataItem.



  • Yeah, Ragnax is right, the data item simply is not returned when the page is resubmitted. The following are ways you can get it back:

    1) Data keys: If you specify DataKeyNames for your ListView and your DataSource is a typical one (Like a DataTable or an object with bindable parameters) then that field will be recorded for each row. Typically you'll set this to ID. Then there's an array of data keys in the control which you can look through to find the ID you need to go get your object.

    2) CommandArgument: If your button is within the ListView row, you can set a CommandName and CommandArgument. The argument you'd typically databind to an ID or something so it's easy to look up when it posts back. Given your example shows a click event rather than an itemcommand event I suspect this doesn't apply.

    3) HiddenField: Simply put an asp:HiddenField in your ListView and then loop through the rows in your listview and call FindControl on each to grab the hidden fields.

     

    All of the above assume if you can get an ID of some sort you can recover your data object. Hope that helps ya.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.