get value by index and not column name...

G

Guest

When using a repeater control I want to get the database value by index
instead of using the column name. How can I do this? My current repeater code
looks like this:

<asp:repeater id="rprtroplinks" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<A href="<%# DataBinder.Eval(Container.DataItem,
"Specimen_Link")%>">Link</A>
<br>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:repeater>
 
K

Karl Seguin

Mike:
I don't think you can do it via the DataBinder.Eval function, but you can do
it by casting Container.DataItem to the appropriate type. If you are
binding to a Dataset, DataView or DataTable then it'll be a DataRowView. If
you are binding to an DataReader then it'll be a DbDataRecord

You can learn more about this explicit casting at
http://support.microsoft.com/default.aspx?scid=kb;en-us;307860

Once you have that, you can do:

<%# ctype(Container.DataItem, DataRowView)(0) %>
or in C#
<%# ((DataRowView)Container.DataItem)[0] %>

switch DataRowView to DbDataRecord if you are binding to a DataReader.

Also, don't forget to add <%@ Import Namespace="System.Data" %> for
DataRowView and <%@ Import Namespace="System.Data.Common" %> for
DbDataRecord...

Hope this helps,
Karl
 

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

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top