【问题标题】:asp:BoundField view the values with a conditionasp:BoundField 查看有条件的值
【发布时间】:2010-12-22 18:55:39
【问题描述】:

我有一个数据视图:

<asp:BoundField DataField="AccontoAutorizzato" HeaderText="Acconto Aut." 
                        SortExpression="AccontoAutorizzato" dataformatstring="{0:C}"  />

可以用类似的条件隐藏每个值

 Visible=<%# ((Int32)Eval("StatoID") < 2) %>

?

谢谢

【问题讨论】:

    标签: asp.net boundfield


    【解决方案1】:

    Saar's answer 对我不起作用,因为即使绑定应该返回 true 或 false,解释器实际上也无法将条件结果转换为布尔值。

    因此,我使用了Boolean 值的明确选择:

    <asp:TemplateField HeaderText="Acconto Aut." >
         <ItemTemplate>
              <asp:Label ID="lbl" runat="server" Text='<%# Bind"AccontoAutorizzato") %>'
                            Visible='<%# ((int)(Eval("StatoID")) < 2) ? Convert.ToBoolean(0) : Convert.ToBoolean(1) %>' />
         </ItemTemplate>
    </asp:TemplateField>
    

    我希望这能让其他人在将布尔错误应用于 Visible 属性时更容易解决它。

    【讨论】:

      【解决方案2】:

      以下是可能的

      <asp:TemplateField HeaderText="Acconto Aut." >
           <ItemTemplate>
                <asp:Label ID="lbl" runat="server" Text='<%# Bind"AccontoAutorizzato") %>'
                              Visible='<%# ((int)(Eval("StatoID")) < 2) %>' />
           </ItemTemplate>
      </asp:TemplateField>
      

      【讨论】:

      • 我很惊讶这能奏效。我在下面回答了另一种选择。
      • 虽然欢迎使用此代码 sn-p,并且可能会提供一些帮助,但它会是 greatly improved if it included an explanation of howwhy 这解决了问题。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。
      • 正确@TobySpeight!解决方案实际上是避免使用 BoundField,因为它不支持可见性绑定。但是,TemplateField 确实如此。
      猜你喜欢
      • 1970-01-01
      • 2014-05-20
      • 2022-01-27
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 2011-10-14
      • 2023-01-19
      • 2022-11-10
      相关资源
      最近更新 更多