• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 852
  • Last Modified:

Getting value from datalist cell

Hi,

I need to retrieve the value of cell of a row in a datalist. I think the following would work for a datagrid, but I can't find a way to do the equivalent in a datalist:

dim quantity as String = DataBinder.Eval(e.Item.DataItem, "Qty").ToString()

I know that e.Item.Cells(x) doesn't apply here either.

Does e.Item.DataItem exists for a datalist?

Regards.
0
ralbion
Asked:
ralbion
1 Solution
 
NetDeveloperCommented:
yes, e.Item.DataItem does exist for a datalist
0
 
tqvdangCommented:
You can retreive the value of cell of a row in ItemCreated event of DataList. You have to wait until all controls are rendered in order to retrieve them.

Here is code in VB.NET for ItemCreated event of DataList from MSDN Library.

      Sub Item_Created(sender As Object, e As DataListItemEventArgs)

         If e.Item.ItemType = ListItemType.Item Or _
             e.Item.ItemType = ListItemType.AlternatingItem Then


            ' Retrieve the Label control in the current DataListItem.
            Dim PriceLabel As Label = _
                CType(e.Item.FindControl("PriceLabel"), Label)

            ' Retrieve the text of the CurrencyColumn from the DataListItem
            ' and convert the value to a Double.
            Dim Price As Double = Convert.ToDouble( _
                (CType(e.Item.DataItem, DataRowView)).Row.ItemArray(2).ToString())

            ' Format the value as currency and redisplay it in the DataList.
            PriceLabel.Text = Price.ToString("c")

         End If

      End Sub
0
 
hismightinessCommented:
In order for the example by tqvdang to work, there needs to be a Label control in the cell with the ID of "PriceLabel".  Then, this is a very easy way for you to do what you're looking for.  I have used it a lot and it also works with other Server Controls.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ralbionAuthor Commented:
Hi,

Thanks for your replies

Ok, so why doesn’t this line:

dim quantity as String = CType(e.Item.DataItem, DataRowView).Row.ItemArray(0).ToString()

work in the DataList_ItemCommand event?

My table is generated row by row “on the fly” from user input via dropdown lists etc. When the form is submitted a new row is generated and rendered. Each row has a “delete” linkbutton which removes the associated row (via e.Item.Itemindex). This all works fine, but before each row is removed from the datatable, I need to pick up the value of the first field of the row.

If I wrap the first column with a  label tag, as in:

<td><asp:Label runat="server" ID="QtyLabel"><%# DataBinder.Eval(Container.DataItem, "Qty") %></asp:Label></td>

in the itemtemplate, can I reference this label for any one row?
0
 
hismightinessCommented:
Yes, you can.  However, you would have to loop through the rows:

        Dim objLabel As System.Web.UI.WebControls.Label
        Dim item As DataListItem
        For Each item In DataList1.Items
            objLabel = CType(item.FindControl("Label1"), System.Web.UI.WebControls.Label)
            LabelValue = objLabel.Text
        Next

In order to have an explicit example, I would need to know exactly what you are trying to do.  This example would simple overwrite the value of the string over and over again with the Label's text value.
0
 
ralbionAuthor Commented:
Yes, I don't think I'm explaining myself...

Basically, I'm displaying data about items a user is ordering. The data are the properties of an item, where the item is always the same but the properties can be different for each one. These properties (e.g. size, colour, type) are selected from radio button lists and dropdown lists. When an item’s properties are all selected, the form is submitted and the properties for this item are then assigned to a row in a datatable and displayed in a datalist. The user can then continue to select properties for more items, which are added to the datatable, and so on.

The datalist rows all have a “delete” feature, such that, obviously, any row (item) can be deleted from the table (for when the user makes a mistake etc.).  Up to here, my app. works fine. What I need to know is how I can retrieve the value (selected previously by the user) of a particular field of the row that is being deleted, since this value is required by another part of the application.

It’s not really very complicated, but perhaps I’m going about it the wrong way. I suppose I could just store all the values of this table column in an array or something as they are selected, and extract them later from there. But it seems an awkward way to deal with this when I already have a datalist created. Do you understand? Can I use a datagrid in the same way as a datalist? It seems there are methods for datagrids that solve my problem, but I don’t know whether they’re generally suitable or not.
0
 
hismightinessCommented:
If I am understanding you correctly, then my preference for this would be a DataGrid.  There is built-in functionality to Delete records and find ID's within this Web Control.  You can add a Delete column and assign the DataKey field to handle this much easier.
0
 
ralbionAuthor Commented:
Hi,

Ok, After a day of banging my head against a wall, I've found the reason(s) for this not working...

After changing the datalist cell to a label control I still couldn't get a value for "quantity" in the following:
 
dim quantity as String = (CType(e.Item.FindControl("QtyLbl"), label)).Text

BUT, I realised that my label controls were in the format:

<asp:Label ID="QtyLbl" runat="server"><%# Container.DataItem("Qty")  %></asp:label>

which is seemly incorrect. They needed to be in the format:

<asp:Label ID="QtyLbl" runat="server" Text='<%# Container.DataItem("Qty")  %>'/>

And now it works! Points to me.
0
 
hismightinessCommented:
I don't think there are any exceptions...  All Server Controls should be done this way.  Try to never have any in-line code outside of the tags (as you have found out).  Sorry I didn't catch that for you - but I am glad you got figured out!
0
 
hismightinessCommented:
I believe tqvdang should be awarded the points with a grade of "B" for the answer.  If were not for the text attribute being incorrect, it would be an "A" because this appears to be the best solution, given the situation.
0
 
ralbionAuthor Commented:
Ok, I agree.

Thanks to all who contributed.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now