简体   繁体   中英

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. 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. 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....

         <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.

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;

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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