ASP.NET - Repeater کنٹرول

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 控件。 元素中的内容在输出中仅出现一次,而 元素的内容会对应 DataSet 中的 "record" 重复出现,最后, 的内容在输出中仅出现一次:

<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 控件,并通过 <%#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>

ਇਸ ਉਦਾਹਰਣ ਨੂੰ ਦਿਖਾਓ

使用

您可以在 元素后添加 元素,这样就可以描述交替行的外观了。在下面的例子中,该表格中每隔一行就会显示为浅灰色的背景:

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

ਇਸ ਉਦਾਹਰਣ ਨੂੰ ਦਿਖਾਓ