Datalist control is bound by an element of the array ???

B

bienwell

Hi all,

Data displayed on the datalist control is bound by the column name of the
dataset like this :

<%# DataBinder.Eval(Container.DataItem, "title")%>

Could I use an element of the array (i.e. index=0) which has the name
"title" in place of it ? For example:

<%# DataBinder.Eval(Container.DataItem, Array(0)) %>

Is there anything wrong with the syntax ? Please give me your advise.

Thanks in advance.
 
B

bienwell

Scott,

After reading the document from the http://odetocode.com/Articles/278.aspx
link, I feel it does not like what I want to express. This document
mentionned about multiple values that we can load into the array "HexValue"
and we can bind data for this array. My question is different : I stored
the column names from the SQL statement into the array "ArrField" : For
example : "select Year, Port, SUM(Trucks) Trucks,
SUM(Empty_Truck_Containers) Empty_Truck_Containers, SUM(Trains) Trains FROM
table1 WHERE Year IN (1996,1997,1998) AND Port= 'AK:Alcan' GROUP BY Year,
Port ORDER BY Port, Year ". With this query, I stored columns "Trucks",
"Empty_Truck_Containers", "Trains" into the array "ArrFields" and refer
them later in data binding. The field names in this array may change depends
on the selection from users. Like this, each column has multiple values in
each Year/Month. In the <itemtemplate>, I will use the For Loop to go
through these fields : The syntax like this :

<tr>
<% Dim i as integer
For i=0 to UBound(ViewState("ArrField")) %>
<td>
<%# DataBinder.Eval(Container.DataItem, Array(i)) %>
</td>
<% Next %>
</tr>

I've got an error : "i is not declared "

I cannot specify the column names in data binding because they are
variables.
Do you have any ideas ? Thanks again.

========================================================
 
S

Scott Allen

Hi bienwell:

I think I see what you are trying to achieve now.

What the article was trying to express was that you aren't restricted
to just using DataBinder in the data binding expressions. You could
invoke some other method of your class to spit out a string.

I'm not quite sure what you want to do in the code below. Did you want
a cell for each entry in the array? If so, it's probably easier to
nest another control inside the DataList cell - like put a Repeater,
DatList, DataGrid inside the cell.

HTH,
 
B

bienwell

Scott,

What I want to do is to use the array of field names for data binding
instead of specifying the field name:
For Example: Users might select field names "Trains", Trucks" in order to
know the Total of Trains and Trucks from the table. In my table, there are
the fieds : Trucks, Trains, Passengers, Pedestrians .... that have the value
number for Month, Year. I cannot specify the field names in data binding
like this:
<td>
<%# DataBinder.Eval(Container.DataItem, "Trains" %>
</td>
<td>
<%# DataBinder.Eval(Container.DataItem, "Trucks" %>
</td>

because the field names are variables and depend on the selection from users
in the first page. I collected the field names from the selection and saved
into the array
"ArrayField" and do the For Loop for databinding

<%# DataBinder.Eval(Container.DataItem, Array(i)) %>

The problem now is the syntax error. It does not know the field names from
the array and gave me an error of the index in the array "i is not declared
". If I replace <%# DataBinder.Eval(Container.DataItem, Array(i)) %> by
<%# DataBinder.Eval(Container.DataItem, Array(0)) or <%#
DataBinder.Eval(Container.DataItem, Array(1)) or <%#
DataBinder.Eval(Container.DataItem, Array(2)) ... then I didn't get the
error.

I hope you get my idea this time. Sorry for my English that is not good.
Please send me e-mail if you're not clear about it.

Thanks again.


==================================================
 
Joined
Nov 6, 2007
Messages
1
Reaction score
0
Hi,

I realize that this is an old post, but having run into the same problem, I thought the solutions might be useful to someone else.

Assuming c# syntax.. this expression would work to display value if the DataItem contains an array. In my case, it is a string array. This would display the 1st element in the string array.

<%# ((String[])Container.DataItem)[0] %>

Carol Skelly
Iatek
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top