ASP.NET টিউটোরিয়াল :[পর্বঃ ১৪]:: ASP.NET Web Forms দিয়ে রিপিটেড লিস্ট কনট্রোল

ASP.NET টিউটোরিয়াল :[পর্বঃ ১৪]:: ASP.NET Web Forms দিয়ে রিপিটেড লিস্ট কনট্রোল
লেখকঃ মোস্তাফিজুর ফিরোজ ।

এক জিনিস বার বার যদি আসে তাহলে কেমন লাগে? খুবই বিরক্ত তাই না? আসুন আজ আমরা তাই ASP.NET Web Forms দিয়ে রিপিটেড লিস্ট কনট্রোল করা শিখবো । তাহলে আর মেজাজ গরম হবে না ।

রিপিটার কন্ট্রোলে ডাটাসেট বাইন্ড
রিপিটার কনট্রোল হল কন্ট্রোলে বাউণ্ড হওয়া লিস্টের রিপিট লিস্ট দেখানো । রিপিটার কনট্রোল সাধারণত ডাটাবেজ টেবিল, XML file অথবা অনেকগুলো আইটেম এর সাথে বাউন্ড হয়ে থাকে । এখন আমি দেখাবো XML file কে কিভাবে একটি রিপিটার কন্ট্রোলের সাথে বাইন্ড করা যায় ।
এখানে উদাহরণ হিসেবে “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>

এটাকে এখন cdcatalog.xml এই নামে সেভ করে ওপেন করেই দেখুন না ।

প্রথমে “System.Data” নামে কোনো নেমস্পেস কে ইম্পোর্ট করতে হবে । ডাটাসেট অবজেক্ট এর কাজের জন্য এই নেমস্পেসকে দরকার পড়বে । .aspx পেজের সবার উপরে নিচের কোডটুকু যোগ করে নেই ।

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

তারপর XML file এর জন্য একটা ডাটাসেট তৈরী করে নেই । পেজটি যখন প্রথম লোড নিবে তখন ডাটাসেট থেকে XML file টি লোড নিবে ।

<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 page এ আরেকটা রিপিটার কনট্রোল তৈরী করবো । এর ভিতরে <HeaderTemplate> টি প্রথমে এবং একবারই সম্পাদন করবে । অবশ্য <ItemTemplate> এলিমেন্ট টি ডাটাসেটের রেকর্ড এর উপর নির্ভর করে রিপিট হতে পারে । কিন্তু <FooterTemplate> এলিমেন্ট টি কিন্তু একবারই সম্পাদিত হয়ে নিচের মত আউটপুট দিবে ।

<html>
<body>

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

<HeaderTemplate>

</HeaderTemplate>

<ItemTemplate>

</ItemTemplate>

<FooterTemplate>

</FooterTemplate>

</asp:Repeater>
form>

</body>
</html>

তারপর আমরা ডাটাসেট তৈরির জন্য স্ক্রিপ্ট যোগ করবো । এটাকে তখন রিপিটার কনট্রোলের mycdcatalog ডাটাসেটের সাথে বাইন্ড করবো । তারপর <%#Container.DataItem(“fieldname”)%> মেথডের মাধ্যমে <ItemTemplate> section এর সাথে রিপিটার কনট্রোল এবং এইচটিএমএল ট্যাগের বাইণ্ড করবো ।

<%@ 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> এর ব্যবহার
আবার <ItemTemplate> এলিমেন্টের পরিবর্তে আমরা <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>

দেখলেন তো নিজের ইচ্ছামত কত রকমের ট্যাগ ব্যবহার করা যাচ্ছে । আপনি একটু ভালভাবে প্র্যাকটিস করলে নিজের মত করে পরিবর্তন করে ব্যবহার করতে পারবেন । তাহলে এবার একটু মেজাজ ঠান্ডা হয়েছে তো ?

Permanent link to this article: http://bangla.sitestree.com/asp-net-%e0%a6%9f%e0%a6%bf%e0%a6%89%e0%a6%9f%e0%a7%8b%e0%a6%b0%e0%a6%bf%e0%a7%9f%e0%a6%be%e0%a6%b2-%e0%a6%aa%e0%a6%b0%e0%a7%8d%e0%a6%ac%e0%a6%83-%e0%a7%a7%e0%a7%aa-asp-net-web-forms-%e0%a6%a6/