我有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();
}
}
plsss幫助meee ...我如何繼續...我嘗試使用數據集和dataadapter ...但沒有我得到所有排序,如果錯誤.. @month缺少,或者'lvDayArticle'上定義DataSource和DataSourceID。刪除一個定義。我該如何繼續......? – 2012-02-23 11:33:31
這一次給你完整的代碼 – suryakiran 2012-02-24 06:08:18
順便說一句刪除內部的數據源控制 – suryakiran 2012-02-24 06:23:34