PFranchise
PFranchise

Reputation: 6752

ASP.NET: Using Eval in an if statement

I have a repeater. In the past, each item in the repeater had an associated text box. But, now I added an attribute to the repeated item that needs to specify if a text box, larger text box, or check box is used for that item.

Here is what my aspx code looks like:

<%if (Eval("DisplayType") == "LargeBox") { %>
    <asp:TextBox ID="largeBoxAnswer" Rows="8" runat="server" Width="200" MaxLength="2000" EvaluationQuestionID='<%# Eval("EvaluationQuestionId") %>' />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="largeBoxAnswer" Display="dynamic" Font-Bold="true" ErrorMessage="*" />
<%} %>
<%else if (Eval("DisplayType") == "CheckBox") { %>
    <asp:TextBox ID="checkBoxAnswer" runat="server" Width="200" MaxLength="100" EvaluationQuestionID='<%# Eval("EvaluationQuestionId") %>' />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="checkBoxAnswer" Display="dynamic" Font-Bold="true" ErrorMessage="*" />
<%} %>
<%else { %>
    <asp:TextBox ID="txtAnswer" runat="server" Width="200" MaxLength="100" EvaluationQuestionID='<%# Eval("EvaluationQuestionId") %>' />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtAnswer" Display="dynamic" Font-Bold="true" ErrorMessage="*" />
<%} %>

This is not working and I get the following error: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

I found quite a bit about this error, but nothing that really helped with this particular issue.

Am I even going about doing something like this the correct way? I am not super experienced with asp.net, so I am open to a different approach to this issue. If this is the best way, how do I move the logic to the code behind so this works properly?

Upvotes: 2

Views: 6609

Answers (1)

Kaushal De Silva
Kaushal De Silva

Reputation: 592

The error is telling you that your if statement is not actually in the databinding context, so even if your Eval did work, what it trys to Eval... "DisplayRule" ... doesnt actually exist at that line.

Have a look at this link; eval in if statement?

From what those guys are saying, your answer may lie in ElementIfTrue, or the Visible property.

So you might end up with something like this;

<asp:TextBox ID="largeBoxAnswer" ElementIfTrue='<%# Eval("DisplayRule") == "LargeBox" %>' Rows="8" runat="server" Width="200" MaxLength="2000" EvaluationQuestionID='<%# Eval("EvaluationQuestionId") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="largeBoxAnswer" Display="dynamic" Font-Bold="true" ErrorMessage="*" />
<asp:TextBox ID="checkBoxAnswer"  ElementIfTrue='<%# Eval("DisplayRule") == "CheckBox" %>' runat="server" Width="200" MaxLength="100" EvaluationQuestionID='<%# Eval("EvaluationQuestionId") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="checkBoxAnswer" Display="dynamic" Font-Bold="true" ErrorMessage="*" />
<asp:TextBox ID="txtAnswer"  ElementIfTrue='<%# Eval("DisplayRule") == "**notsure**" %>' runat="server" Width="200" MaxLength="100" EvaluationQuestionID='<%# Eval("EvaluationQuestionId") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtAnswer" Display="dynamic" Font-Bold="true" ErrorMessage="*" />

... I don't think that solves all of your problems, but it may get you along the way.

Upvotes: 1

Related Questions