ASP.NET - DataList नियंत्रक

DataList नियंत्रक, Repeater नियंत्रक की तरह, इस नियंत्रक के अंदर सीमित परियोजनाओं की दोहरी सूची दिखाने के लिए उपयोग किया जाता है। लेकिन, DataList नियंत्रक डेटा प्रोजेक्ट पर डिफ़ॉल्ट रूप से तालिका जोड़ देता है。

DataSet को DataList नियंत्रक से बांधें

DataList नियंत्रक, Repeater नियंत्रक की तरह, इस नियंत्रक के अंदर सीमित परियोजनाओं की दोहरी सूची दिखाने के लिए उपयोग किया जाता है। लेकिन, DataList नियंत्रक डेटा प्रोजेक्ट पर डिफ़ॉल्ट रूप से तालिका जोड़ देता है। DataList नियंत्रक डाटाबेस तालिका, XML फ़ाइल या अन्य परियोजना सूची के लिए बांधा जा सकता है। यहाँ, हम XML फ़ाइल को DataList नियंत्रक से बांधने के लिए दिखाएंगे。

हम नीचे दिए गए XML फ़ाइल ("cdcatalog.xml") का उपयोग करके उदाहरण में इस्तेमाल करेंगे:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
  <title>इम्पायर बर्लेस्क</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 पृष्ठ में एक DataList कंट्रोल बनाते हैं。<HeaderTemplate> एलीमेंट की सामग्री आउटपुट में केवल एक बार दिखाई देती है, तथा <ItemTemplate> एलीमेंट की सामग्री DataSet में "record" के प्रत्येक रिकॉर्ड के लिए दोहराई जाती है, अंत में, <FooterTemplate> की सामग्री आउटपुट में केवल एक बार दिखाई देती है:

<html>
<body>
<form runat="server">
<asp:DataList id="cdcatalog" runat="server">
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
<FooterTemplate>
...
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>

तब हम डेटासेट बनाने वाले स्क्रिप्ट जोड़ते हैं, और इस mycdcatalog डेटासेट को डेटालिस्ट नियंत्रक से बांधते हैं. हम इन एलीमेंटों को इसी तरह डेटालिस्ट नियंत्रक को भरने के लिए भी उपयोग करते हैं: शीर्षक वाले <HeaderTemplate>, दिखाने वाले डाटा वाले <ItemTemplate>, और टेक्स्ट वाले <FooterTemplate>. ध्यान दें कि डेटालिस्ट का gridlines एट्रिब्यूट "both" के रूप में सेट किया गया है, ताकि टेबल किनारा दिखाया जा सके:

<%@ 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:DataList id="cdcatalog"
gridlines="both" runat="server">
<HeaderTemplate>
My CD Catalog
</HeaderTemplate>
<ItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</ItemTemplate>
<FooterTemplate>
कॉपीराइट codew3c.com
</FooterTemplate>
</asp:DataList>
</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:DataList id="cdcatalog"
runat="server"
cellpadding="2"
cellspacing="2"
borderstyle="inset"
backcolor="#e8e8e8"
width="100%"
headerstyle-font-name="Verdana"
headerstyle-font-size="12pt"
headerstyle-horizontalalign="center"
headerstyle-font-bold="true"
itemstyle-backcolor="#778899"
itemstyle-forecolor="#ffffff"
footerstyle-font-size="9pt"
footerstyle-font-italic="true"
<HeaderTemplate>
My CD Catalog
</HeaderTemplate>
<ItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</ItemTemplate>
<FooterTemplate>
कॉपीराइट codew3c.com
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>

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

उपयोग <AlternatingItemTemplate>

आप <ItemTemplate> एलीमेंट के बाद <AlternatingItemTemplate> एलीमेंट जोड़ सकते हैं, इस तरह आप विकल्पी वाले वाले की दिखावट को वर्णित कर सकते हैं. आप डेटालिस्ट नियंत्रक के अंदर <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:DataList id="cdcatalog"
runat="server"
cellpadding="2"
cellspacing="2"
borderstyle="inset"
backcolor="#e8e8e8"
width="100%"
headerstyle-font-name="Verdana"
headerstyle-font-size="12pt"
headerstyle-horizontalalign="center"
headerstyle-font-bold="True"
itemstyle-backcolor="#778899"
itemstyle-forecolor="#ffffff"
alternatingitemstyle-backcolor="#e8e8e8"
alternatingitemstyle-forecolor="#000000"
footerstyle-font-size="9pt"
footerstyle-font-italic="True">
<HeaderTemplate>
My CD Catalog
</HeaderTemplate>
<ItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</ItemTemplate>
<AlternatingItemTemplate>
"<%#Container.DataItem("title")%>" of
<%#Container.DataItem("artist")%> -
$<%#Container.DataItem("price")%>
</AlternatingItemTemplate>
<FooterTemplate>
© codew3c.com
</FooterTemplate>
</asp:DataList>
</form>
</body>
</html>

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