2011-03-30 54 views
2

是否可以僅輸出存儲在數據表中的值的第一個實例?我可以只輸出數據表中第一個值的實例嗎?

我的例子是這樣=我有一個數據表,其中每行包含一個月的名稱。該表按月份按升序排列。我只想輸出表格中第一次出現的月份名稱 - 如果一月份有五個條目,則文本「1月」只需要輸出一次。

我正在使用中繼器 - 下面的代碼。 Month中的值只有在與上一行中的值不同時纔會更改。

<asp:Repeater ID="rptEvents" runat="server"> 
    <ItemTemplate> 
    <h5><%#Eval("Month")%></h5>  
     <p> 
      <strong><asp:HyperLink ID="hlEvents" runat="server" Text='<%#Eval("Title") %>' NavigateUrl='<%#Eval("QuickLink") %>'></asp:HyperLink></strong><br /> 
    <%#Eval("Date") %><br /> 
    <%#Eval("Description") %> 
    </p> 
    </ItemTemplate> 
</asp:Repeater> 
+0

難道你不同時從數據庫中選擇?或者甚至在給該中繼器提供數據源時? – 2011-03-30 05:39:00

回答

0

那麼,我不知道,但如何在數據庫端做呢?
SELECT MIN(DISTNCT月),標題,從快速鏈接標題,快速鏈接

UPDATE:
好像我被誤解了。如何使用一種方法?

<h5><%#GetMonthName(Eval("Month"))%></h5> 

而在後臺代碼,定義一個類範圍的變量來保存比上月名稱,並在你的方法把它比作當前的月份名稱:

// this is a private class-scope variable. 
private static string prevMonthName = string.Empty; 

public string GetMonth(string curMonthName) 
{ 
    if (curMonthName == prevMonthName) 
    { 
    return string.Empty; 
    } 
    prevMonthName = curMonthName; 
    return curMonthName; 
} 
+0

我已經非常使用你的解決方案 - 而不是在控件中使用方法,我直接在填充數據表時調用GetMonth。作品一種享受。 – Nathan 2011-03-31 00:21:19

+0

@Nathan:很高興聽到它可以幫助你。祝你好運。 – Kamyar 2011-03-31 21:54:28

2

我建議有2箇中繼器。外將綁定到一個月列表和一個月內內的所有條目:

<asp:Repeater ID="MonthList" runat="server" OnItemDataBound="MonthList_ItemDataBound"> 
    <ItemTemplate> 
     <h2><%# Container.DataItem %></h2> 
     <asp:Repeater ID="ItemList" runat="server"> 
      <ItemTemplate><%# Eval("Name") %></ItemTemplate> 
     </asp:Repeater> 
    </ItemTemplate> 
</asp:Repeater> 

對於這個標記後面的代碼可能看起來像:

public partial class _Default : System.Web.UI.Page 
{ 
    public class Entry 
    { 
     public string Month {get;set;} 
     public string Name {get;set;} 
    } 

    List<Entry> entries = new List<Entry>() 
    { 
     new Entry() { Month = "Jan", Name = "name1"}, 
     new Entry() { Month = "Jan", Name = "name2"}, 
     new Entry() { Month = "Feb", Name = "name3"}, 
     new Entry() { Month = "Aug", Name = "name4"}, 
     new Entry() { Month = "Dec", Name = "name5"}, 
     new Entry() { Month = "Dec", Name = "name6"} 
    }; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      MonthList.DataSource = entries.GroupBy(x => x.Month).Select(x => x.Key); 
      MonthList.DataBind(); 
     } 
    } 

    protected void MonthList_ItemDataBound(object sender, RepeaterItemEventArgs e) 
    { 
     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
     { 
      var itemList = (Repeater)e.Item.FindControl("ItemList"); 
      var month = (string)e.Item.DataItem; 
      itemList.DataSource = entries.Where(x => x.Month == month); 
      itemList.DataBind(); 
     } 
    } 
} 

我用內存中的集合的樣本和LINQ將其轉換。首先,我們按月分組數據源。然後,在MonthList_ItemDataBound中,對於每個被綁定的月份,我們查找內部中繼器並將其與通過當前(DataItem)月份過濾的集合綁定。希望這可以幫助。

+0

聽起來像一個很好的選擇 - 它將如何工作? – Nathan 2011-03-30 06:07:29

+0

用樣本更新了我的答案。 – UserControl 2011-03-30 07:09:07

相關問題