如何在嵌套列表视图中按日期显示数据
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用这种方法你无法达到你的要求。
我想提供一些步骤来满足您的要求。
为外部 ListView 添加一个 DataKey 作为 [MnthYr]。然后更改外部 SELECT 查询,以便您将获得所有不同的文章月份,类似于下面的查询
SELECT DISTINCT DATENAME(MONTH,[artdate])+' '+CONVERT(VARCHAR,YEAR([artdate])) AS [MnthYr] FROM [as_Articles] ORDER BY [artdate] DESC
然后为外部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.
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
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.
希望你从现在开始就能得到它:)
Hope you will get it from now :)