本文介绍了如何使用C#ASP.NET转发器控件处理3级层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用新闻文章显示,我想按年显示这些新闻==>月份==>天 。用户界面将显示如下屏幕:
I am working with News articles display, I would like to make those news display by Years ==> Months ==> Days . The UI will display like following screen:
News 2019
>> January (2)
>>01/02/2019 articles
>>01/09/2019 articles
News 2018
>> December (2)
>>12/02/2019 articles
>>12/09/2019 articles
News 2017
>> January (3)
>>01/02/2019 articles
>>01/09/2019 articles
News 2016
>> January (1)
>>01/02/2019 articles
>>01/09/2019 articles
但我只能让它显示如下。
But I only can make it display like following.
News 2019
>> January (2)
News 2018
>> December (2)
News 2017
>> January (3)
我尝试过:
What I have tried:
protected void loadyears()
{
con.Open();
BindMenu();
try {
//Create years for news view
SqlCommand scmdY = new SqlCommand("select distinct year([NewsDate]) as Years from [WebSiteNewsMaint] order by Years desc ", con);
SqlDataAdapter cmdY = new SqlDataAdapter(scmdY);
DataSet ds = new DataSet();
cmdY.Fill(ds, "WSNewsYears");
//Create Months for news view
SqlCommand scmdM = new SqlCommand("select top(100) percent Datename(month,newsdate) as NMDate, MONTH(NewsDate) as MDate, COUNT(Newsid) AS NArticle, year([NewsDate]) as Years from [WebSiteNewsMaint] GROUP BY Datename(month,newsdate) ,month(newsdate),year(newsdate) ORDER BY MONTH(NewsDate) DESC", con);
SqlDataAdapter cmdM = new SqlDataAdapter(scmdM);
cmdM.Fill(ds, "WSNewsMonths");
//Create the relation between the years and months views
ds.Relations.Add("YearMonthRelation", ds.Tables["WSNewsYears"].Columns["Years"], ds.Tables["WSNewsMonths"].Columns["Years"]);
Years.DataSource = ds.Tables["WSNewsYears"];
Page.DataBind();
} catch (SqlException ex)
{
errormessage.Text = ex.Message.ToString();
}
finally
{
con.Close();
}
}
推荐答案
const string Query = @"SELECT DISTINCT
Year(NewsDate) As Years
FROM
WebSiteNewsMaint
ORDER BY
Years DESC
;
SELECT
Year(NewsDate) As Years,
Month(NewsDate) As MDate,
DateName(month, NewsDate) As NMDatem,
Count(1) As NArticle
FROM
WebSiteNewsMaint
GROUP BY
Year(NewsDate) As Years,
Month(NewsDate) As MDate,
DateName(month, NewsDate) As NMDatem
ORDER BY
Years DESC,
MDate DESC
;
SELECT
Year(NewsDate) As Years,
Month(NewsDate) As MDate,
NewsDate,
NewsId
FROM
WebSiteNewsMaint
ORDER BY
Years DESC,
MDate DESC,
NewsDate DESC
;";
using (SqlCommand command = new SqlCommand(Query, con))
{
SqlDataAdapter da = new SqlDataAdapter(command);
// Map the three result sets to friendly names:
// See: https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/dataadapter-datatable-and-datacolumn-mappings#handling-multiple-result-sets
da.TableMappings.Add("Table", "WSNewsYears");
da.TableMappings.Add("Table1", "WSNewsMonths");
da.TableMappings.Add("Table2", "WSNewsDays");
DataSet ds = new DataSet();
da.Fill(ds);
// Relationship from years to months:
ds.Relations.Add("YearMonthRelation", ds.Tables["WSNewsYears"].Columns["Years"], ds.Tables["WSNewsMonths"].Columns["Years"]);
// Relationship from months to days:
ds.Relations.Add("MonthDayRelation",
new[] { ds.Tables["WSNewsMonths"].Columns["Years"], ds.Tables["WSNewsMonths"].Columns["MDate"] },
new[] { ds.Tables["WSNewsDays"].Columns["Years"], ds.Tables["WSNewsDays"].Columns["MDate"] });
Years.DataSource = ds.Tables["WSNewsYears"];
Page.DataBind();
}
在月嵌套转发器中放置天嵌套转发器,并绑定其<$ c $ c> DataSource 属性与你已经为月转发器做的一样。
Put a "days" nested repeater inside the "months" nested repeater, and bind its DataSource
property in the same way that you're already doing for the "months" repeater.
这篇关于如何使用C#ASP.NET转发器控件处理3级层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!