Latest web development tutorials
×

ASP.NET コース

ASP.NET コース ASP.NET 簡単な紹介

WP コース

WebPages 簡単な紹介 WebPages Razor WebPages レイアウト WebPages フォルダ WebPages 全体的な状況 WebPages フォーム WebPages オブジェクト WebPages ファイル WebPages ヘルパー WebPages WebGrid WebPages チャート WebPages Email WebPages PHP WebPages リリース WebPages 例

WP リファレンスマニュアル

WebPages カテゴリ WebPages セキュリティ WebPages データベース WebPages 郵便局 WebPages ヘルパー

ASP.NET Razor

Razor 簡単な紹介 Razor 文法 Razor C# 変数 Razor C# 循環 Razor C# ロジック Razor VB 変数 Razor VB 循環 Razor VB ロジック

ASP.NET MVC

MVC 簡単な紹介 MVC アプリケーション MVC フォルダ MVC レイアウト MVC コントローラ MVC ビュー MVC データベース MVC モデル MVC セキュリティ MVC HTML ヘルパー MVC リリース MVC リファレンスマニュアル

WF コース

WebForms 簡単な紹介 WebForms ページ WebForms コントロール WebForms イベント WebForms フォーム WebForms ViewState WebForms TextBox WebForms Button WebForms データバインディング WebForms ArrayList WebForms Hashtable WebForms SortedList WebForms XML ファイル WebForms Repeater WebForms DataList WebForms データベース接続 WebForms マスターページ WebForms ナビゲーション WebForms 例

WF リファレンスマニュアル

WebForms HTML WebForms Controls WebForms Validation

ASP.NET Repeaterコントロール

リピータ制御リストを表示するための制御プログラムを繰り返して結合しています。


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の "名前空間をインポートします。 我々は、この名前空間は、DataSetオブジェクトを使用する必要があります。 次の命令は、.aspxページの上部に含まれています:

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

次に、このXMLファイルの読み込みDataSetを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>要素の内容が最初にレンダリングされ、一度だけ出力され、DataSetにそれぞれ「記録」に対応する<ItemTemplateに>要素の内容が繰り返され、最終的には、<FooterTemplateの>要素の内容出力は一度だけ表示されます。

<html>
<body>

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

<HeaderTemplate>
...
</HeaderTemplate>

<ItemTemplate>
...
</ItemTemplate>

<FooterTemplate>
...
</FooterTemplate>

</asp:Repeater>
</form>

</body>
</html>

その後、我々は、DataSetを作成するためのスクリプトを追加し、Repeaterコントロールにmycdcatalog DataSetをバインドします。 その後、Repeaterコントロールによって、および<%#Container.DataItem( "フィールド名")%>バインドするデータ項目<ItemTemplateに>エリアセルを移入するためにHTMLタグを使用します。

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

デモ>>