?
Solved

Getting value from datalist cell

Posted on 2005-03-02
12
Medium Priority
?
817 Views
Last Modified: 2008-02-01
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
Comment
Question by:ralbion
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 10

Expert Comment

by:NetDeveloper
ID: 13439170
yes, e.Item.DataItem does exist for a datalist
0
 

Accepted Solution

by:
tqvdang earned 225 total points
ID: 13439784
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
 
LVL 9

Expert Comment

by:hismightiness
ID: 13439983
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:ralbion
ID: 13441404
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
 
LVL 9

Expert Comment

by:hismightiness
ID: 13442102
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
 

Author Comment

by:ralbion
ID: 13444847
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
 
LVL 9

Expert Comment

by:hismightiness
ID: 13448979
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
 

Author Comment

by:ralbion
ID: 13453691
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
 
LVL 9

Expert Comment

by:hismightiness
ID: 13453736
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
 
LVL 9

Expert Comment

by:hismightiness
ID: 13680934
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
 

Author Comment

by:ralbion
ID: 13682992
Ok, I agree.

Thanks to all who contributed.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question