A.D.
A.D.

Reputation: 1116

trying to access in code behind to an ASP:Repeater ItemTemplate

Environment is an oldish application written in ASP 2.0.

I have this code that displays a DB source into an ASP:repeater:

<asp:Repeater ID="Repeater" runat="server"    DataSourceID="DS" OnItemDataBound="On_Repeater_ItemDataBound">
    <ItemTemplate>
        <asp:CheckBox ID="CB" runat="server" Text='<%# Eval("My_DB_Field") %>'></asp:CheckBox>
    </ItemTemplate>
</asp:Repeater>

It works fine. However, I would like to change the code so that sometimes CB.Text = My_DB_Field (as now) and sometimes it equals an other DB field value. So I will need to do that into code behind's On_Repeater_ItemDataBound method.

But how can I access the template item DB fields inside this method?

In other words, what is the equivalent of

Text='<%# Eval("My_DB_Field") %>'

in code behind?

I see that what I am looking for can be accessed via: ((System.Data.DataRowView)(e.Item.DataItem)).Row.ItemArray[12] However, Row.ItemArray is an object[]

So it looks extremely hazardous to hard-code the DB field ID (12). I'd like to know if there is a dictionary like object so I can access it via the DB field name ("My_DB_Field")?

Upvotes: 0

Views: 357

Answers (1)

VDWWD
VDWWD

Reputation: 35514

You can use DataBinder.Eval

string value = DataBinder.Eval(e.Item.DataItem, "My_DB_Field").ToString();

or

DataRowView row = e.Item.DataItem as DataRowView;
string value = row["My_DB_Field"].ToString();

Upvotes: 2

Related Questions