Vivian River
Vivian River

Reputation: 32400

How to get result of #Eval("X") in my C# code?

I have some asp code with an asp:Repeater object.

I am familiar with using <%# Eval("field") to print dataitem.field to the HTML code. However, what can I do if I want to get the result of Eval("field") saved to a string literal for further processing?

Update: I feel like I owe an apology for not being more specific. As the first answerer suggetss, I am planning to use the result in an ItemTemplate. However, what about field of the current record that are not strings? What if I have some complex type that contains all sorts of weird ** and I want to refer to the fields in my item template, and not as strings?

Upvotes: 3

Views: 745

Answers (5)

BeemerGuy
BeemerGuy

Reputation: 8269

Rice,
Have you tried casting the return of Eval() to whatever type you're expecting?

Upvotes: 0

Neil
Neil

Reputation: 737

As Arief said, you need to use a server control for this.

To access the data in the code behind, you need to loop through the repeater items, find the control, and then access the value.

So, assuming you have a repeater with a literal in the item template (ltlFieldId), here is how you can access the value stored in the literal:

For Each ri As RepeaterItem In MyRepeater.Items
   Dim ltlFieldId As Literal = ri.FindControl("ltlFieldId")
   Dim FieldId As Integer = CType(ltlFieldId.Text, Integer)
Next

Upvotes: 1

Erik van Brakel
Erik van Brakel

Reputation: 23830

Another way to approach this is to use a specialized view class which is either built from the data you're using, or is simply a wrapper around your class. Deal with the 'complex' operations in your C# code and expose the result as a property on the class you pass to your aspx file.

Upvotes: 1

Jacob
Jacob

Reputation: 78890

I'm not sure if this is what you're asking for, but if you want to do an Eval in a context outside of .aspx markup, you can use the DataBinder.Eval method directly in your code.

Upvotes: 2

Arief
Arief

Reputation: 6085

You should use Server Control for this:

<asp:Repeater ID="aRepeater" runat="server">
    <ItemTemplate>
        <asp:HiddenField ID="SomeHiddenField" runat="server" value='<%# Eval("FieldID") %>' />
    </ItemTemplate>
</asp:Repeater>

You can retrieve the value of "FieldID" later by accessing the "Value" property of the HiddenField control.

Upvotes: 1

Related Questions