사실 이하의 내용들은 ASP.NET 3.5가 지원되는 환경이면 더욱 간단하게 구현할 수 있다. 부득이하게 2.0을 써야 하는 상황이 자주 생겨 작성해 보았다.
XML 파일을 데이터소스로 하여 페이징해서 보여주기 ¶
여기서 사용할 예제 XML 소스는 다음과 같다.
<?xml version="1.0" encoding="utf-8" ?>
<links>
<link no="1" contentsType="XML" title="Sample XML" />
<link no="2" contentsType="HTML" title="Sample HTML" />
</links>
ASP.NET 2.0 ¶
우선 GridView를 이용하면 매우 간단하게 표를 만들 수 있다.
Default.aspx :
Default.aspx.cs :
결과 :
ASP.NET 2.0에서 페이징을 구현하는 한 가지 방법은 PagedDataSource를 이용하는 것이다. 여기서는 각각 XML 내용 및 페이지 번호 목록을 보여줄 두 개의 Repeater를 만들어 구현해본다.
<asp:GridView ID="grid1" runat="server" />
XmlDataSource source = new XmlDataSource();
source.DataFile = "XMLFile.xml";
grid1.DataSource = source;
grid1.DataBind();
no | contentsType | title |
1 | XML | Sample XML |
2 | HTML | Sample HTML |
ASP.NET 2.0에서 페이징을 구현하는 한 가지 방법은 PagedDataSource를 이용하는 것이다. 여기서는 각각 XML 내용 및 페이지 번호 목록을 보여줄 두 개의 Repeater를 만들어 구현해본다.
DataSet xmlSource = new DataSet();
xmlSource.readXml(Server.MapPath("XMLFile.xml"));
PagedDataSource pds = new PagedDataSource();
pds.DataSource = xmlSource.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 10;
pds.CurrentPageIndex = currentPage;
xmlRepeater.DataSource = pds;
xmlRepeater.DataBind();
//페이징
DataTable dt = new DataTable();
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageText");
for (int i = 0; i < pds.PageCount; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i + 1;
dt.Rows.Add(dr);
}
pageList.DataSource = dt;
pageList.DataBind();
XPath를 이용해 검색한 결과를 불러들이려면 먼저 XmlDocument를 만든 뒤 검색하여 DataSet의 소스로 넣는다.
DataSet xmlSource = new DataSet();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("learn_link_list.xml"));
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/links/link[@contentsType='XML']");
XmlDocument newXml = new XmlDocument();
newXml.AppendChild(newXml.CreateProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\""));
newXml.AppendChild(newXml.CreateElement("links"));
foreach (XmlNode node in nodeList)
{
XmlNode append = newXml.ImportNode(node, true);
newXml.DocumentElement.AppendChild(append);
}
xmlSource.ReadXml(new XmlNodeReader(newXml));
PagedDataSource pds = new PagedDataSource();
pds.DataSource = xmlSource.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 10;
pds.CurrentPageIndex = currentPage;
xmlRepeater.DataSource = pds;
xmlRepeater.DataBind();
//페이징
//...