1

I have a listview with some data bound to it.

In this data are column for an address.

How would I go about accessing these data items in code behind so I can concatenate them into one easy variable and miss out columns that have no data in, i have fields:

address address1 town county postcode

I don't have a problem with the concatenation just accessing the data items.

Thanks J.

UPDATED

I am getting data out via a dataset and binding it to a listview.

Is it possible to access data items in the code behind to format or do whatever i want with them then showing it in the list view such as, concatenating the address fields into one variable?

so instead of writing:

DataBinder.Eval(Container.DataItem, "address") & ", " & DataBinder.Eval(Container.DataItem, "address1") & ", " & DataBinder.Eval(Container.DataItem, "town") etc...

in the actual list view i could do this in the code behind in a string variable then show the variable in the list view?

    'select command
    Dim cmdSchedule As SqlCommand = New SqlCommand()
    cmdSchedule.Connection = keypadSQL
    cmdSchedule.CommandText = "spSchedule"
    cmdSchedule.CommandType = CommandType.StoredProcedure

    'data adapter
    Dim daSchedule As SqlDataAdapter = New SqlDataAdapter
    daSchedule.SelectCommand = cmdSchedule

    'data set
    Dim dsSchedule As DataSet = New DataSet()
    daSchedule.Fill(dsSchedule, "Schedule")

    lvSchedule.DataSource = dsSchedule
    lvSchedule.DataBind()
    cmdSchedule.Dispose()

1 Answer 1

1

First put your items into accessible controls in the ListView, such as a label or literal.

<asp:ListView ID="ListView1" runat="server">
    <ItemTemplate>
        <asp:Label ID="lblAddress" runat="server" Text="<%= Eval("address") %>" />
    </ItemTemplate>
</asp:ListView>

Then you can loop through the items and using FindControl, pull each string out individually.

Dim items As List(Of ListViewDataItem) = ListView1.Items
For Each item As ListViewDataItem In items
    Dim strAddress As String = CType(item.FindControl("lblAddress"), Label).Text
Next

UPDATED

I'd think the best way would be to format it in SQL Stored Procedure and return it as a new field. Something like this:

SELECT *, address + ', ' + address1 + ', ' + town ', ' + county + ', ' postcode AS fullAddress 
FROM ...

Then you'd just have to use <%= DataBinder.Eval(Container.DataItem, "fullAddress") %> to get the formatted address. You could even format it with HTML in the SP as long as you're weary of potential injection attacks (not sure of the original address input method).

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the info, although i don't really want to display the whole address as the whole point is to format the address as a string then display the formatted string to the user.
I must be misunderstanding your question. I was only using "address" as an example. In reality you would have different labels for each field (i.e.: address address1 town county postcode). Maybe if you told me the end goal I could help a little better?
nick, i've updated my question a bit, hopefully i've explained a bit more an example of what i'm trying to do.
Ah, yes I was highly misunderstanding your question. Take a look at my update and see what you think. The only other option I can think of is to break into the filled dataset before binding it, looping through the DataRows. I like the SQL SP idea much more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.