[英]Repeater in ASP.net
I used repeater in asp.net. 我在asp.net中使用了repeater。 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: 这里是我在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! 谢谢!
You would need to handle the OnItemDataBound event, and then change the visibility of the control. 您需要处理OnItemDataBound事件,然后更改控件的可见性。 An example of this is shown below:
这方面的一个例子如下所示:
ASPX Page 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>
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();
}
}
I would take a look at the ItemDataBound
event of the Repeater. 我将看一下Repeater的
ItemDataBound
事件。 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: 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. 它既快又脏,但它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.