ASP.net中的中继器

im *_*ess 2 c# asp.net repeater

我在asp.net中使用了repeater.我的问题是不知道如何隐藏转发器中的字段.有正常价格和现在价格,如果正常价格等于零,它将隐藏字段,如果不是,它将显示正常价格的价值.我希望你能帮忙解决这个问题.

这里是我在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>   
Run Code Online (Sandbox Code Playgroud)

谢谢!

Mun*_*Mun 10

您需要处理OnItemDataBound事件,然后更改控件的可见性.这方面的一个例子如下所示:

ASPX

<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>
Run Code Online (Sandbox Code Playgroud)

代码背后

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();

   }
}
Run Code Online (Sandbox Code Playgroud)


Chr*_*tal 5

我会看一下ItemDataBoundRepeater 的事件.它将触发转发器中的每个项目,并允许您更轻松地执行任何代码隐藏(如隐藏标签).

编辑:对于您的具体示例,由于您也在格式化价格,因此可能更容易调用自定义方法来呈现价格,如下所示:

ASPX:

<%#RenderPrice((decimal)Eval("lb_sellingprice"))%>
Run Code Online (Sandbox Code Playgroud)

方法:

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

它既快又脏,但它有效.