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

发布于 2025-01-07 18:00:30 字数 3952 浏览 0 评论 0原文

我有 2 个嵌套列表视图...每个都有不同的数据源...我想要这样的内容:

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();

     }
 }

I have 2 nested listview... each with different data source ... i want to have something like this:

February 2012

  • 10: article title
  • 04: article title

January 2012

  • 20: article title
  • 24: article title

But now I have

February 2012

  • 10: article title

February 2012

  • 04: article title

... and so on ...
my code is something like this:

<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>

and code behind:

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();

     }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咽泪装欢 2025-01-14 18:00:30

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

我想提供一些步骤来满足您的要求。

  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事件。获取内部列表视图的实例和当前外部列表视图项的 DataKey 值。使用它,您可以编写一个通过传递月份和年份来获取文章详细信息的方法。然后绑定内部ListView。

这肯定可以满足要求。

Using this approach you can't achieve your requirement.

I would like to provide some steps to go on with your requirement.

  1. Add a DataKey as [MnthYr] for the Outer ListView. Then change the outer SELECT Query such that you will get all the distinct Article Months something similar to below query

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

  2. Then add OnItemDataBound Event for the Outer ListView. Get the instance of the inner List View and DataKey value of the current Outer ListView item. Using that you can write a Method of get the Article details by passing Month and Year. Then Bind the inner List View.

This would surely get the requirement done.

↙温凉少女 2025-01-14 18:00:30
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;
    }
}

希望你从现在开始就能得到它:)

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;
    }
}

Hope you will get it from now :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文