im useless
im useless

Reputation: 7451

Repeater in ASP.net

I used repeater in asp.net. My problem is don't know how to hide a fields in repeater. There is a regular price and now price if regular price is equal to zero it will hide the fields and if not it will show the value of the regular price. i hope you can help on this.

here my code in asp:

 <a href="<%=Utility.GetSiteRoot() %>/BookInfo.aspx?SKU=<%# Utility.SKUMask(Eval("lb_sku").ToString()) %>">
               <img width="150px" src='<%# Eval("lb_picturepath")%>'>
             </td>
             <td valign="top">
             <asp:Label ID="lb_titleLabel" runat="server" CssClass="center-head" Text='<%# Eval("lb_title") %>' />
             <p><asp:Label ID="lb_descriptionLabel" runat="server" Text='<%# Eval("lb_description") %>' /></p>
             <div class="price"><%# "Price: " +  decimal.Round((decimal)Eval("lb_sellingprice"),2)%></div>
             </td>
             </tr>
             <tr>
             <td></td>
             <td>
              <a class="addtocart" href="<%=Utility.GetSiteRoot() %>/AddToCart.aspx?SKU=<%# Utility.SKUMask(Eval("lb_sku").ToString()) %>" >Add To Cart</a>
              <a href="<%=Utility.GetSiteRoot() %>/BookInfo.aspx?SKU=<%# Utility.SKUMask(Eval("lb_sku").ToString()) %>"  class="readmore">
              View Details
             </a></td>   

thanks!

Upvotes: 2

Views: 9081

Answers (2)

Mun
Mun

Reputation: 14308

You would need to handle the OnItemDataBound event, and then change the visibility of the control. An example of this is shown below:

ASPX Page

<asp:Repeater ID="MyRepeater" OnItemDataBound="MyRepeater_OnItemDataBound" runat="server">
<ItemTemplate>
    <asp:Label ID="RegularPriceLabel" runat="server" />
    <br/>
    <asp:Label ID="BuyNowPriceLabel" runat="server" />
</ItemTemplate>
</asp:Repeater>

Code Behind

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        MyRepeater.DataSource = GetDataSource();
        MyRepeater.DataBind(); 
    }
}

protected void MyRepeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
   {
        // This will be your data object
        MyEntity o = (MyEntity) e.Item.DataItem;

        // Get the labels
        Label RegularPriceLabel = (Label) e.Item.FindControl("RegularPriceLabel");
        Label BuyNowPriceLabel = (Label) e.Item.FindControl("BuyNowPriceLabel");

        // Only show regular price if it is set
        RegularPriceLabel.Visible = (o.RegularPrice > 0);

        // Populate labels
        RegularPriceLabel.Text = o.RegularPrice.ToString();
        BuyNowPriceLabel.Text = o.BuyNowPrice.ToString();

   }
}

Upvotes: 9

Chris Van Opstal
Chris Van Opstal

Reputation: 37537

I would take a look at the ItemDataBound event of the Repeater. It will fire for every item in the repeater and allow you to do any code-behind (like hiding labels) more easily.

Edit: For your specific example, since you are formatting the price as well, it may be easier to just call a custom method to to render the price, like so:

ASPX:

<%#RenderPrice((decimal)Eval("lb_sellingprice"))%>

Method:

protected string RenderPrice(decimal price) {
    if (price > 0) {
        return "Price: $" + decimal.Round(price);
    } else {
        return string.Empty;
    }
}

It's quick-and-dirty but it works.

Upvotes: 4

Related Questions