ASP.NET - ArrayList ऑब्जैक्ट

ArrayList ऑब्जैक्ट एक अकेला डाटा मूल्य वाले वस्तुओं के समूह है।

उदाहरण

ArrayList DropDownList

ArrayList RadioButtonList

ArrayList बनाएं

ArrayList ऑब्जैक्ट एक अकेला डाटा मूल्य वाले वस्तुओं के समूह है।

ArrayList में वस्तुओं को Add() मेथड के माध्यम से जोड़ा जाता है。

नीचे दिए गए कोड एक नया ArrayList ऑब्जैक्ट बनाता है, जिसे mycountries कहा जाता है, और चार वस्तुओं को जोड़ता है:

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("China")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
end if
end sub
</script>

डिफ़ॉल्ट में, एक ArrayList ऑब्जैक्ट 16 एंट्री को समाविष्ट करता है।TrimToSize() मेथड के माध्यम से ArrayList को अंतिम आकार कर सकते हैं:

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("China")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
end if
end sub
</script>

Sort() मेथड के माध्यम से, ArrayList अक्षराक्षर क्रम या संख्या क्रम के अनुसार भी क्रमबद्ध कर सकता है:

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("China")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
  mycountries.Sort()
end if
end sub
</script>

विलोम क्रमबद्ध करने के लिए, Sort() मेथड के बाद Reverse() मेथड लागू करें:

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("China")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
  mycountries.Sort()
  mycountries.Reverse()
end if
end sub
</script>

डाटा को ArrayList से बांधें

ArrayList ऑब्जैक्ट निम्नलिखित कंट्रोल के लिए स्वचालित रूप से टेक्स्ट और मूल्य बना सकता है:

  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox

यदि आप डाटा को RadioButtonList कंट्रोल से बांधने के लिए चाहते हैं, तो सबसे पहले एक .aspx पृष्ठ में RadioButtonList कंट्रोल बनाएं (ध्यान दें कि कोई asp:ListItem एलीमेंट नहीं है):

<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>

तब बिल्डिंग सूची जोड़ने वाले स्क्रिप्ट को और सूची में की गई वैल्यू को इस RadioButtonList नियंत्रक को बांधने के लिए:}}

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("China")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
  mycountries.Sort()
  rb.DataSource=mycountries
  rb.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>
</body>
</html>

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

RadioButtonList नियंत्रक का DataSource अधिकारिता इस ArrayList को सेट किया गया है, जो इस RadioButtonList नियंत्रक के डेटा स्रोत को परिभाषित करता है।RadioButtonList नियंत्रक के DataBind() विधि नियंत्रक को डेटा स्रोत से बांधती है।

टिप्पणी:डेटा वैल्यू को कंट्रोलर के Text और Value अधिकारिता के रूप में इस्तेमाल किया जाता है।यदि विभिन्न Text के Value को जोड़ना है, तो यह या तो Hashtable वस्तु का उपयोग कर सकते हैं या SortedList वस्तु का उपयोग कर सकते हैं।