2011-12-08 31 views
1

我想讓我的第一個公共.net網站,但我有一個小問題。 這是一個網上商店,我想在新產品圖片上展示,如果該產品不比當前日期超過30天?在30天內顯示圖片ASP.NET

標記:

<asp:Repeater ID="rptProducts" runat="server">   
     <ItemTemplate> 
      <table cellpadding="5" cellspacing="5" class="tblSales"> 
       <tr> 
        <td> 
         <h3> 
          <asp:Label ID="Label1" Text='<%#Eval("ProductName")%>' runat="server" /></h3> 
         <h2> 
       <a href='<%# "Products.aspx?ID=" + Eval("ProductID") %>'>Read More</a></h2> 
        </td> 
        <td > 
         <h4> 
          <asp:Label ID="Label2" Text='<%#Eval("ProductPrice")%>' runat="server" />,-</h4>  
          <div> 
<img alt="New product" src="images/NewProduct.png" /><!--This picture should show in 30 days--> 
           </div> 
        </td> 
       </tr> 
       <hr /> 
      </table> 
     </ItemTemplate> 
</asp:Repeater> 

代碼隱藏:

public partial class sale : System.Web.UI.Page 
{ 
    ProductsTableAdapter p = new ProductsTableAdapter(); 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     GetData(); 
    } 

    protected void GetData() 
    { 
     rptProducts = p.GetProductsByCategoryID(3); 
     rptProducts();   
    } 
} 
+1

IM困惑,你再解釋一下在精確..我的理解是,你必須展示一張不比當前日期更早的圖片?但是30天后怎麼樣 – FosterZ

+2

我不明白問題是什麼......這是不是工作? –

回答

5

編輯:作爲一個用戶指出,這可能是一個誤解所以...

如果你想過濾的項目,被添加並且完全不顯示它們在GetData方法上使用以下LINQ表達式:

rptProducts = p.GetProductsByCategoryID(3).Where(p=>p.ProductAddedDate > DateTime.Now.AddDays(-30)); 

,你需要有一個屬性(在上面的例子中,我加入一個DateTime ProductAddedDate到產品類

如果你想不顯示圖像基礎上的日期,但顯示一切你可以:

在可以設置ASP控制轉發器的ItemDataBound事件(取代與ASP靜態HTML:圖片,其允許用戶使用所述可見性屬性來控制顯示即

<asp:Repeater ID="rptProducts" runat="server" OnItemDataBound="rptProducts_Databound">   
     <ItemTemplate> 
      <table cellpadding="5" cellspacing="5" class="tblSales"> 
       <tr> 
        <td> 
         <h3> 
          <asp:Label ID="Label1" Text='<%#Eval("ProductName")%>' runat="server" /></h3> 
         <h2> 
       <a href='<%# "Products.aspx?ID=" + Eval("ProductID") %>'>Read More</a></h2> 
        </td> 
        <td > 
         <h4> 
          <asp:Label ID="Label2" Text='<%#Eval("ProductPrice")%>' runat="server" />,-</h4>  
          <div> 
<asp:Image ID="ProdImg" runat="server" AlternativeText="New product" ImageUrl='<%# "~/images/" + Eval("ImageFileName")' Visible="True"/><!--This picture should show in 30 days--> 
           </div> 
        </td> 
       </tr> 
       <hr /> 
      </table> 
     </ItemTemplate> 
</asp:Repeater> 

CS代碼隱藏

protected void rptProducts_Databound(object sender, RepeaterItemEventArgs e) 
{ 
    var imgCtrl = (Image)e.Item.FindControl("ProdImg"); 
    var dataItem = (Product)e.Item.DataItem; 
    imCtrl.Visible = dataItem.ProductAddedDate > DateTime.Now.AddDays(-30); 
} 
+0

我不明白這是如何回答Jeppe的問題。如果我正確地閱讀了這個問題,他想要顯示所有產品(在給定類別中),但只顯示不超過30天的產品旁邊的NewProduct-圖標。您的代碼只會刪除所有不超過30天的產品。 –

+0

修改爲包含有條件的asp:Image的其他方法。這應該涵蓋所有的基地 –

0

非常感謝,我發現如何使它:)感謝你,很簡單,但我的工作。 這裏是我做的代碼:

<asp:Image ID="Image2" ImageUrl="~/images/new.png" Visible='<%#GetProductSalesPic(Eval("ProductDate"))%>' runat="server" /> 

C#代碼:

protected bool GetProductSalesPic(object Date) 
    { 
     DateTime actDate = (DateTime)Date; 
     if (DateTime.Now.AddDays(-30) < actDate) 
     { 
      return true; 
     } 
     else 
     { 
     return false; 
     } 
    } 

再次,感謝你,爲快速和良好的響應:d

+0

:)我也打算髮表'使用Eval作爲參數後面的方法調用代碼',但認爲2個例子就足夠了:)雖然做得很好 –