ASP.NET - Repeater 컨트롤러
- 이전 페이지 WebForms XML 파일
- 다음 페이지 WebForms DataList
Repeater 컨트롤러는 반복된 아이템 목록을 표시하는 데 사용되며, 이 아이템들은 컨트롤러 안에 제한됩니다.
인스턴스
DataSet을 Repeater 컨트롤러에 바인딩
Repeater 컨트롤러는 반복된 아이템 목록을 표시하는 데 사용되며, 이 아이템들은 컨트롤러 안에 제한됩니다. Repeater 컨트롤러는 데이터베이스 테이블, XML 파일 또는 다른 아이템 목록에 바인딩 될 수 있습니다. 여기서, XML 파일을 Repeater 컨트롤러에 바인딩하는 방법을 보여드리겠습니다.
이 예제에서 사용할 다음 XML 파일("cdcatalog.xml")를 사용하겠습니다:
<?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> </cd> <cd> <title>Greatest Hits</title> <artist>Dolly Parton</artist> <country>USA</country> <company>RCA</company> <price>9.90</price> <year>1982</year> </cd> <cd> <title>Still got the blues</title> <artist>Gary Moore</artist> <country>UK</country> <company>Virgin records</company> <price>10.20</price> <year>1990</year> </cd> <cd> <title>Eros</title> <artist>Eros Ramazzotti</artist> <country>EU</country> <company>BMG</company> <price>9.90</price> <year>1997</year> </cd> </catalog>
이 XML 파일을 확인해 주세요:cdcatalog.xml
먼저, "System.Data" 네임스페이스를 가져오세요. 이 네임스페이스는 DataSet 객체와 함께 작업하기 위해 필요합니다. .aspx 페이지의 상단에 다음 명령을 포함하세요:
<%@ Import Namespace="System.Data" %>
그런 다음, 이 XML 파일에 DataSet을 생성하고, 페이지가 최초로 로드될 때 XML 파일을 DataSet에 로드합니다:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml")) end if end sub
그런 다음 .aspx 페이지에서 Repeater 컨트롤을 생성합니다. 《HeaderTemplate》 요소의 내용은 출력에서 한 번만 나타나고, 《ItemTemplate》 요소의 내용은 DataSet의 "record"에 대응하여 반복적으로 나타나며, 마지막으로 《FooterTemplate》의 내용은 출력에서 한 번만 나타납니다:
<html> <body> <form runat="server"> <asp:Repeater id="cdcatalog" runat="server"> <HeaderTemplate> ... </HeaderTemplate> <ItemTemplate> ... </ItemTemplate> <FooterTemplate> ... </FooterTemplate> </asp:Repeater> </form> </body> </html>
그런 다음 DataSet을 생성하여 mycdcatalog DataSet을 Repeater 컨트롤에 바인딩하도록 스크립트를 추가합니다. 또한 HTML 태그를 사용하여 이 Repeater 컨트롤을 채우고, 데이터 항목을 《ItemTemplate》 부분의 셀에 바인딩하는 방법을 보여줍니다: <%#Container.DataItem("fieldname")%> 메서드를 사용합니다:
<%@ Import Namespace="System.Data" %> <script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml")) cdcatalog.DataSource=mycdcatalog cdcatalog.DataBind() end if end sub </script> <html> <body> <form runat="server"> <asp:Repeater id="cdcatalog" runat="server"> <HeaderTemplate> <table border="1" width="100%"> <tr> <th>Title</th> <th>Artist</th> <th>Country</th> <th>Company</th> <th>Price</th> <th>Year</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><%#Container.DataItem("title")%></td> <td><%#Container.DataItem("artist")%></td> <td><%#Container.DataItem("country")%></td> <td><%#Container.DataItem("company")%></td> <td><%#Container.DataItem("price")%></td> <td><%#Container.DataItem("year")%></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </form> </body> </html>
《AlternatingItemTemplate》 사용
《ItemTemplate》 요소 뒤에 《AlternatingItemTemplate》 요소를 추가하여 대체 행의 외관을 설명할 수 있습니다. 아래의 예제에서 테이블의 각 행마다 짙은 회색 배경을 표시합니다:
<%@ Import Namespace="System.Data" %> <script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml")) cdcatalog.DataSource=mycdcatalog cdcatalog.DataBind() end if end sub </script> <html> <body> <form runat="server"> <asp:Repeater id="cdcatalog" runat="server"> <HeaderTemplate> <table border="1" width="100%"> <tr> <th>Title</th> <th>Artist</th> <th>Country</th> <th>Company</th> <th>Price</th> <th>Year</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><%#Container.DataItem("title")%></td> <td><%#Container.DataItem("artist")%></td> <td><%#Container.DataItem("country")%></td> <td><%#Container.DataItem("company")%></td> <td><%#Container.DataItem("price")%></td> <td><%#Container.DataItem("year")%></td> </tr> </ItemTemplate> <AlternatingItemTemplate> <tr bgcolor="#e8e8e8"> <td><%#Container.DataItem("title")%></td> <td><%#Container.DataItem("artist")%></td> <td><%#Container.DataItem("country")%></td> <td><%#Container.DataItem("company")%></td> <td><%#Container.DataItem("price")%></td> <td><%#Container.DataItem("year")%></td> </tr> </AlternatingItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </form> </body> </html>
《SeparatorTemplate》 사용
《SeparatorTemplate》 요소는 각 레코드 간의 구분자를 설명하는 데 사용될 수 있습니다. 아래의 예제는 테이블 행 사이에 수평선을 삽입합니다:
<%@ Import Namespace="System.Data" %> <script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycdcatalog=New DataSet mycdcatalog.ReadXml(MapPath("cdcatalog.xml")) cdcatalog.DataSource=mycdcatalog cdcatalog.DataBind() end if end sub </script> <html> <body> <form runat="server"> <asp:Repeater id="cdcatalog" runat="server"> <HeaderTemplate> <table border="0" width="100%"> <tr> <th>Title</th> <th>Artist</th> <th>Country</th> <th>Company</th> <th>Price</th> <th>Year</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><%#Container.DataItem("title")%></td> <td><%#Container.DataItem("artist")%></td> <td><%#Container.DataItem("country")%></td> <td><%#Container.DataItem("company")%></td> <td><%#Container.DataItem("price")%></td> <td><%#Container.DataItem("year")%></td> </tr> </ItemTemplate> <SeparatorTemplate> <tr> <td colspan="6"><hr /></td> </tr> </SeparatorTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </form> </body> </html>
- 이전 페이지 WebForms XML 파일
- 다음 페이지 WebForms DataList