【问题标题】:ASP.NET using Bind/Eval in .aspx in If statementASP.NET 在 If 语句中使用 .aspx 中的 Bind/Eval
【发布时间】:2011-08-01 13:47:56
【问题描述】:

在我的 .aspx 中,我希望根据来自绑定的值添加一个 If 语句。我尝试了以下方法:

<% if(bool.Parse(Eval("IsLinkable") as string)){ %>                    
        monkeys!!!!!!
        (please be aware there will be no monkeys, 
        this is only for humour purposes)
 <%} %>

IsLinkable 是来自 Binder 的布尔值。我收到以下错误:

InvalidOperationException
Databinding methods such as Eval(), XPath(), and Bind() can only
be used in the context of a databound control.

【问题讨论】:

    标签: c# if-statement eval bind webforms


    【解决方案1】:

    我们需要查看您的其余代码,但错误消息给了我一些提示。只有在数据绑定控件中时才能使用 Eval。诸如转发器、数据网格等之类的东西。

    如果您在数据绑定控件之外,您可以将值加载到代码隐藏中的变量中并将其公开。然后您可以在 ASPX 上使用它进行条件处理。

    【讨论】:

      【解决方案2】:

      您需要将您的逻辑添加到 ListView 的ItemDataBound 事件中。在 aspx 中,您不能在 DataBinder 的上下文中使用 if 语句:&lt;%# if() %&gt; 不起作用。

      看看这里:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound.aspx

      将为绑定到您的 ListView 的每个项目引发事件,因此事件中的上下文与项目相关。

      例如,看看你是否可以根据你的情况进行调整:

      protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
      {
          if (e.Item.ItemType == ListViewItemType.DataItem)
          {
              Label monkeyLabel = (Label)e.Item.FindControl("monkeyLabel");
              bool linkable = (bool)DataBinder.Eval(e.Item.DataItem, "IsLinkable");
              if (linkable)
                 monkeyLabel.Text = "monkeys!!!!!! (please be aware there will be no monkeys, this is only for humour purposes)";
          }
      }
      

      【讨论】:

      • 我认为问题在于他将语句嵌入到需要表达式的东西中。
      • @asawyer 听起来对我来说是一个很好的解释。我还没有真正考虑过这种方式,但它非常有意义。
      • 这也适用于Repeater控件。
      【解决方案3】:

      我很确定您可以执行以下操作

      (请注意,我没有方便的编译器来测试确切的语法)

      text = '<%# string.Format("{0}", (bool)Eval("IsLinkable") ? "Monkeys!" : string.Empty) %>'
      

      是的,这是 c# 并且您使用的是 vb.net,因此您需要使用 vb 语法作为三元运算符。

      编辑 - 能够进入一个简单的数据绑定情况,就像一个魅力。

      【讨论】:

      • OP 没有使用vb 而是c#(见标签),所以你的例子是相关的。我认为您需要以&lt;%# 开头,但如果我错了,请纠正我。
      • @Bazzz 哦,看,它是 c#。甚至更好!另外你是正确的,它应该是
      【解决方案4】:

      每当我需要处理数据绑定控件中的条件时,我都会使用OnItemDataBound 事件。

      所以你可以这样做:

      protected void DataBound_ItemDataBoundEvent() {
           bool IsLinkable            = (bool)DataBinder.Eval(e.Item.DataItem, "IsLinkable");  
           if(IsLinkable) {
                //do stuff
           }                                     
      
      }
      

      【讨论】:

        【解决方案5】:

        对于FormView 控制参考this link

        这里是示例代码。我的 aspx 页面 FormView 控件如下所示:

        <asp:FormView ID="fv" runat="server" Height="16px" Width="832px"  
        CellPadding="4" ForeColor="#333333" ondatabound="fv_DataBound"> 
            <ItemTemplate>
                <table>
                    <tr>
                        <td align="left" colspan="2" style="color:Blue;">
                            <asp:Label ID="lblPYN" runat="server" Text='<%# Eval("PreviousDegreeYN") %>'></asp:Label> 
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:FormView>
        

        我正在检查 &lt;%# eval("PreviousDegreeYN") %&gt; 的值

        如果我的eval("PreviousDegreeYN") == True,我想在我的标签“lblPYN”中显示Yes

        protected void fv_DataBound(object sender, EventArgs e)
        {
            FormViewRow row = fv.Row;
            //Declaring Variable lblPYN
            Label lblPYN;
            lblPYN = (Label)row.FindControl("lblPYN");
            if (lblPYN.Text == "True")
            {
                lblPYN.ForeColor = Color.Blue;
                lblPYN.Text = "Yes";
        
            }
            else
            {
                lblPYN.ForeColor = Color.Blue;
                lblPYN.Text = "No";
        
            }
        }
        

        【讨论】:

          【解决方案6】:

          如果您在 Bazzz 的回答中遇到 e.Item.DataItem 问题,请尝试

          protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
          {
              using (ListViewDataItem listViewDataItem = (ListViewDataItem) e.Item)
              {
                  if (listViewDataItem != null)
                  {
                      Label monkeyLabel = (Label)e.Item.FindControl("monkeyLabel");
                      bool linkable = (bool)DataBinder.Eval(listViewDataItem , "IsLinkable");
                      if (linkable)
                         monkeyLabel.Text = "monkeys!!!!!! (please be aware there will be no monkeys, this is only for humour purposes)";
                  }
              }
          }
          

          【讨论】:

            【解决方案7】:

            我知道这个答案有点晚了,但这里值得我解决这个问题:

            <%# (bool)Eval("IsLinkable") ? "monkeys!!!!!!" : "" %>
            

            【讨论】:

              【解决方案8】:

              您可以创建一个方法来评估值并返回您想要的值。

              <%# IsLinkableABool( Eval("IsLinkable") ) %>
              

              在后面的代码中你可以创建如下方法

              protected String IsLinkableABool(String isLinkable)
              {
                  if (isLinkable == Boolean.TrueString)
                  {
                       return "monkeys!!!!!! (please be aware...";    
                  }
                  else
                  {
                       return String.Empty;
                  }
              }
              

              【讨论】:

              • 真棒解决方案...对不起上限!你拯救了我的一天:)
              【解决方案9】:

              你可以使用asp:PlaceHolder 并且在 Visible 中可以放 eval。如下所示

                 <asp:PlaceHolder ID="plc" runat="server" Visible='<%# Eval("IsLinkable")%>'>
                     monkeys!!!!!!
                     (please be aware there will be no monkeys, this is only for humour purposes)
                 </asp:PlaceHolder>
              

              【讨论】:

                【解决方案10】:

                设置条件aspx页面不是一个好主意。也很乱。 你可以使用三元运算符。但我建议你使用网格视图的行数据绑定事件。 步骤 1-转到网格视图属性。单击照明按钮以列出所有事件。 第 2 步 - 在 rowdatabound 上命名并双击

                protected void onrow(object sender, GridViewRowEventArgs e)

                   {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        TableCell statusCell = e.Row.Cells[8];//Means column 9
                
                        if (statusCell.Text == "0")
                        {
                            statusCell.Text = "No Doc uploaded";
                
                        }
                        else if (statusCell.Text == "1")
                        {
                            statusCell.Text = "Pending";
                        }
                        else if (statusCell.Text == "2")
                        {
                            statusCell.Text = "Verified";
                        }
                    }
                }
                

                【讨论】:

                  【解决方案11】:

                  天哪,这花了很长时间才弄清楚......

                  <asp:PlaceHolder runat="server" Visible='<%# Eval("formula.type").ToString()=="0" %>'> Content </asp:PlaceHolder>

                  formula.type 是链接表的 int 列。感谢其他为我解决问题所做的贡献。

                  【讨论】:

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