简体   繁体   English

如何使用if语句在aspx转发器控件中添加文本框或标签?

[英]How to add textbox or label in aspx repeater control by using if statement?

i would like to add textbox or label by using if statement. 我想使用if语句添加文本框或标签。 My coming data "-" or any price value. 我即将到来的数据“-”或任何价格值。 if data is equal to "-" (which means empty...), use label. 如果数据等于“-”(表示为空...),请使用标签。 OR if data is equal to "45 or 56 or etc" (which means price), use textbox. 或者,如果数据等于“ 45或56等等”(表示价格),请使用文本框。 But error return to me while running below codes. 但是在以下代码下运行时,错误返回给我。

Error: "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. – " in if Eval blog.... 错误:“仅在数据绑定控件的上下文中使用Eval(),XPath()和Bind()之类的数据绑定方法。–“如果是Eval博客...。

         <td align="left">

                                <%  if (Eval("SERVICE_AMOUNT").ToString() != "-")
                                    { %>
                                 <asp:TextBox ID="priceTextBox" runat="server" Text='<%# FieldFormat(Eval("SERVICE_AMOUNT")) %>' AutoPostBack="true"></asp:TextBox>
                                 <%}
                                    else
                                    { %>
                                    <asp:Label runat="server" ID="lblServiceAmount" Text='<%# FieldFormat(Eval("SERVICE_AMOUNT")) %>'></asp:Label>
                                    <%} %>
                                </td>

I suggest you to place both controls in .aspx and then implement your hiding logic in code-behind in ItemDataBound event. 我建议您将两个控件都放在.aspx中,然后在ItemDataBound事件的代码隐藏中实现隐藏逻辑。

An example: 一个例子:

protected void RepeaterItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    {
        string serviceAmount = DataBinder.Eval(e.Item.DataItem, "SERVICE_AMOUNT").ToString();

        var priceTextBox = e.Item.FindControl("priceTextBox") as TextBox;
        var lblServiceAmount = e.Item.FindControl("lblServiceAmount") as Label;

        priceTextBox.Visible = serviceAmount != "-";
        lblServiceAmount.Visible = !priceTextBox.Visible;

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM