如何在嵌套列表视图中按日期显示数据

时间:2012-02-19 11:58:42

标签: asp.net listview nested

我有2个嵌套的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])=@strMonth AND CONVERT(VARCHAR,YEAR([artdate]))=@strYear 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();

     }
 }

2 个答案:

答案 0 :(得分:2)

使用这种方法无法达到您的要求。

我想提供一些步骤来继续您的要求。

  1. 为外部ListView添加DataKey为[MnthYr]。然后更改外部SELECT查询,以便获得与下面的查询

    类似的所有不同的文章月

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

  2. 然后为外部ListView添加OnItemDataBound事件。获取当前Outer ListView项的内部List View和DataKey值的实例。使用它,您可以通过传递Month和Year来编写获取文章详细信息的方法。然后绑定内部列表视图。

  3. 这肯定会完成要求。

答案 1 :(得分: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])=@strMonth AND YEAR([artdate])=@strYear";
    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;
    }
}

希望你能从现在开始接受它:)