2012-02-19 52 views
0

我有2個嵌套的ListView ...每個不同的數據源顯示按日期數據...我想有這樣的事情:如何嵌套的ListView

2012年2月

  • 10:文章標題
  • 04:文章標題

2012年1月

  • 20:文章標題
  • 24:文章標題

但現在我有

2012年2月

  • 10:文章標題

2012年2月

  • 04:文章標題

...等等... 我的代碼是這樣的:

<asp:ListView ID="lvMonthYear" runat="server" DataSourceID="SqlDataSource1" 
    ItemPlaceholderID="PlaceHolder2" DataKeyNames="MnthYr" 
    onitemdatabound="lvMonthYear_ItemDataBound1"> 
    <ItemTemplate> 
     <h1> 
      <asp:Label ID="lblMonthYear" runat="server" Text='<%# Eval("MnthYr") %>'/> </h1> 
     <asp:ListView ID="lvDayArticle" runat="server" DataKeyNames="artid" ItemPlaceholderID="PlaceHolder2" > 
      <ItemTemplate> 
       <li runat="server"> 
        <asp:Label ID="lblDay" runat="server" Text='<%# Eval("artdate","{0:dd}") %>' />: 
        <asp:LinkButton ID="lblTitle" runat="server" Text='<%# Eval("title") %>' PostBackUrl='<%#Bind("artid","Articol.aspx?art={0}") %>' 
         CssClass="LinkButton1" /> 
       </li> 
      </ItemTemplate> 
      <LayoutTemplate> 
       <ul> 
        <asp:PlaceHolder runat="server" ID="PlaceHolder2" /> 
       </ul> 
      </LayoutTemplate> 
      <EmptyDataTemplate> 
       Nu există niciun articol.<br /> 
      </EmptyDataTemplate> 
     </asp:ListView> 

    </ItemTemplate> 
    <EmptyDataTemplate> 
       Nu există niciun articol.<br /> 
      </EmptyDataTemplate> 
    <LayoutTemplate> 
     <asp:PlaceHolder runat="server" ID="PlaceHolder2" /> 
    </LayoutTemplate> 
</asp:ListView> 
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ASConnectionString %>" 
    SelectCommand="SELECT DISTINCT (DATENAME(MONTH, [artdate]) + ' ' + CONVERT (varchar, YEAR([artdate]))) AS [MnthYr] FROM as_Articles ORDER BY [MnthYr] DESC"> 
</asp:SqlDataSource> 

和後面的代碼:

protected DataSet GetArticleds(string Month, string Year) 
    { 
     DataSet articleDataSet=new DataSet(); 
     ConnectionStringSettings cs; 
     cs = ConfigurationManager.ConnectionStrings["ASConnectionString"]; 
     String connString = cs.ConnectionString; 
     SqlConnection dbConnection = new SqlConnection(connString); 
     string query = "SELECT [artid], [title], [artdate] FROM [as_Articles] WHERE DATENAME(MONTH,[artdate])[email protected] AND CONVERT(VARCHAR,YEAR([artdate]))[email protected] ORDER BY [artdate] DESC"; 
     SqlCommand dbCommand = new SqlCommand(query, dbConnection); 
     dbCommand.Parameters.Add(new SqlParameter("@strMonth", Month)); 
     dbCommand.Parameters.Add(new SqlParameter("@strYear", Year)); 
     SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(dbCommand); 
     try 
     { 
      sqlDataAdapter.Fill(articleDataSet); 
     } 
     catch { } 
      return articleDataSet; 
    } 


protected void lvMonthYear_ItemDataBound1(object sender, ListViewItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListViewItemType.DataItem) 
    { 
     ListViewDataItem currentItem = (ListViewDataItem)e.Item; 
     DataKey currentDataKey = this.lvMonthYear.DataKeys[currentItem.DataItemIndex]; 
     ListView lvDayArticle = (ListView)currentItem.FindControl("lvDayArticle"); 

     string strMonthYear = Convert.ToString(currentDataKey["MnthYr"]); 

     string strMonth = strMonthYear.Split(' ')[0]; 
     string strYear = strMonthYear.Split(' ')[1]; 
     lvDayArticle.DataSource = GetArticleds(strMonth, strYear); 
     lvDayArticle.DataBind(); 

    } 
} 

回答

1
protected void lvMonthYear_ItemDataBound(object sender, ListViewItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListViewItemType.DataItem) 
    { 
     ListViewDataItem currentItem= (ListViewDataItem)e.Item; 
     DataKey currentDataKey = this.lvMonthYear.DataKeys[currentItem.DataItemIndex]; 
     ListView lvDayArticle = (ListView)currentItem.FindControl("lvDayArticle"); 
     string strMonthYear = Convert.ToString(currentDataKey["MnthYr"]); 
     string strMonth = strMonthYear.Split(' ')[0]; 
     string strYear = strMonthYear.Split(' ')[1]; 
     lvDayArticle.DataSource = GetArticleds(strMonth,strYear); 
     lvDayArticle.DataBind(); 
    } 
} 

protected DataSet GetArticleds(string Month, string Year) 
{ 
    string strCommand="SELECT [artid], [title] FROM [as_Articles] WHERE DATENAME(MONTH,[artdate])[email protected] AND YEAR([artdate])[email protected]"; 
    List<SqlParameter> sqlparam = new List<SqlParameter>(); 
    sqlparam.Add(new SqlParameter("@strMonth", SqlDbType.VarChar, 3) { Value = Month }); 
    sqlparam.Add(new SqlParameter("@strYear", SqlDbType.SmallInt) { Value = Year }); 
    SqlConnection con = new SqlConnection("ConnectionString"); 
    SqlCommand cmd = new SqlCommand(strCommand, con); 
    cmd.Parameters.AddRange(sqlparam.ToArray()); 
    SqlDataAdapter da = new SqlDataAdapter(cmd); 
    using (DataSet ds = new DataSet()) 
    { 
     da.Fill(ds); 
     return ds; 
    } 
} 

希望你從現在得到它:)

+0

plsss幫助meee ...我如何繼續...我嘗試使用數據集和dataadapter ...但沒有我得到所有排序,如果錯誤.. @month缺少,或者'lvDayArticle'上定義DataSource和DataSourceID。刪除一個定義。我該如何繼續......? – 2012-02-23 11:33:31

+0

這一次給你完整的代碼 – suryakiran 2012-02-24 06:08:18

+0

順便說一句刪除內部的數據源控制 – suryakiran 2012-02-24 06:23:34

2

使用此方法你不能達到你的要求。

我想提供一些步驟來繼續您的要求。

  1. 爲外部ListView添加一個DataKey作爲[Mnth]。然後更改外部SELECT查詢,以便您可以獲得與以下類似的所有不同的Article Month(年月日)類似的查詢

    SELECT DISTINCT DATENAME(MONTH,[artdate])+''+ CONVERT(VARCHAR,YEAR([artdate])) AS [Mnth] FROM [as_Articles] ORDER BY [artdate] DESC

  2. 然後爲外部ListView添加OnItemDataBound事件。獲取當前外部ListView項目的內部List View和DataKey值的實例。使用該功能,您可以通過傳遞月份和年份來編寫獲取文章詳細信息的方法。然後綁定內部列表視圖。

這肯定會得到要求。

+0

THX ...我會嘗試這個......我做了類似的東西,但不是的ItemDataBound – 2012-02-20 15:17:25