एसपीएनईटी - रीपीटर कंट्रोल

रीपीटर कंट्रोल एक दोहराए हुए आइटम्स सूची दिखाने के लिए उपयोग किया जाता है जो कंट्रोल में सीमित हैं

डेटासेट को रीपीटर कंट्रोल से बांधा जाता है

रीपीटर कंट्रोल एक दोहराए हुए आइटम्स सूची दिखाने के लिए उपयोग किया जाता है जो कंट्रोल में सीमित हैं।रीपीटर कंट्रोल को डाटाबेस तालिका, XML फ़ाइल या अन्य आइटम्स सूची से बांधा जा सकता है।यहाँ, हम एक 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 फ़ाइल को पृष्ठ के पहले लोड होने पर 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 नियंत्रक से बांधते हैं। हम इस Repeater नियंत्रक को HTML टैगों से भरते हैं और <%#Container.DataItem("fieldname")%> विधि के द्वारा डाटा आयाम को <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> का उपयोग करके

आप <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>

इस उदाहरण को दिखाएं