Latest web development tutorials

تحكم ASP.NET مكرر

لا بد قائمة التحكم مكرر لتكرار برنامج مراقبة للعرض.


بيانات ربط السيطرة مكرر

لا بد قائمة التحكم مكرر لتكرار برنامج مراقبة للعرض. قد تكون ملزمة لسيطرة مكرر إلى جدول قاعدة بيانات، ملف 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" %>

بعد ذلك، إنشاء DataSet لملف 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. <HeaderTemplate> يتم تقديم محتوى العنصر الأول ومرة ​​واحدة فقط في الإخراج، و<ItemTemplate> محتويات عنصر المقابلة إلى DataSet كل "السجل" ويتكرر، وأخيرا، <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 لسيطرة مكرر. ثم استخدام علامات 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>

مظاهرة >>