Latest web development tutorials

ASP.NET Repeater 컨트롤

중계기 제어리스트 표시 제어 프로그램을 반복 할 수밖에 없다.


Repeater 컨트롤에 바인딩 데이터 집합

중계기 제어리스트 표시 제어 프로그램을 반복 할 수밖에 없다. 리피터 제어는 데이터베이스 테이블, XML 파일, 또는 다른 항목의리스트에 결합 될 수있다. 여기, 우리는 Repeater 컨트롤에 XML 파일을 결합하는 방법을 보여줍니다.

우리의 예에서 우리는 다음과 같은 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"네임 스페이스를 가져옵니다. 우리는 데이터 집합 개체 작업이 공간이 필요합니다. 다음 명령은 .ASPX 페이지의 상단에 포함되어 있습니다 :

<%@ Import Namespace="System.Data" %>

다음으로, XML 파일의 데이터 집합을 만들 때 페이지가 처음로드이 XML 파일 로딩 데이터 집합 :

<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을> 요소의 내용이 반복되고, 마지막으로 <FooterTemplate 영역> 요소 내용 출력은 한 번만 나타납니다

<html>
<body>

<form runat="server">
<asp:Repeater id="cdcatalog" runat="server">

<HeaderTemplate>
...
</HeaderTemplate>

<ItemTemplate>
...
</ItemTemplate>

<FooterTemplate>
...
</FooterTemplate>

</asp:Repeater>
</form>

</body>
</html>

그런 다음 우리는 데이터 집합을 생성하는 스크립트를 추가하고, Repeater 컨트롤에 mycdcatalog 데이터 집합을 결합한다. 그런 다음에 의해 Repeater 컨트롤을 채우는 HTML 태그를 사용 <% # Container.DataItem ( "필드 명") %> 데이터 항목 <ItemTemplate을> 지역 셀을 결합하는 방법 :

<%@ 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>

당신은 출력 행을 번갈아의 모양을 설명하는 데 사용되는 <AlternatingItemTemplate> 요소에 <ItemTemplate을> 요소를 게시 할 수 있습니다. 아래의 예에서, 테이블은 밝은 회색 배경으로 다른 모든 라인을 표시됩니다

<%@ 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>

데모 >>