【问题标题】:ASP.NET: Using Eval in an if statementASP.NET:在 if 语句中使用 Eval
【发布时间】:2013-05-17 20:20:03
【问题描述】:

我有一个中继器。过去,转发器中的每个项目都有一个关联的文本框。但是,现在我为重复项添加了一个属性,该属性需要指定该项是使用文本框、更大的文本框还是复选框。

这是我的 aspx 代码的样子:

<%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="*" />
<%} %>

这不起作用,我收到以下错误: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

我发现了很多关于这个错误的信息,但没有什么能真正帮助解决这个特定问题。

我什至会以正确的方式做这样的事情吗?我对 asp.net 不是超级有经验,所以我对解决这个问题的不同方法持开放态度。如果这是最好的方法,我该如何将逻辑移到后面的代码中才能正常工作?

【问题讨论】:

  • 所有这些代码都在转发器中吗?如果是这样,请编辑您的问题并为其添加标记。

标签: c# asp.net if-statement eval repeater


【解决方案1】:

错误告诉您,您的 if 语句实际上并不在数据绑定上下文中,因此即使您的 Eval 确实有效,它尝试 Eval...“DisplayRule”... 在该行实际上并不存在。

看看这个链接; eval in if statement?

从那些人所说的来看,你的答案可能在于 ElementIfTrue 或 Visible 属性。

所以你可能会得到这样的结果;

<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="*" />

...我认为这并不能解决你所有的问题,但它可能会让你一路走好。

【讨论】:

    猜你喜欢
    • 2011-08-01
    • 2010-11-08
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多