ASP.NET - العنصر Hashtable
- الصفحة السابقة ArrayList WebForms
- الصفحة التالية SortedList WebForms
يحتوي العنصر Hashtable على مشاريع متمثلة في أزواج مفتاح/قيمة.
إنشاء Hashtable
يحتوي العنصر Hashtable على مشاريع متمثلة في أزواج مفتاح/قيمة. يتم استخدام المفتاح كمؤشر، من خلال البحث في المفتاح، يمكن الوصول بسرعة إلى القيمة.
إضافة مشاريع من خلال طريقة Add()
يوجد أدناه رمز إنشاء Hashtable اسمه mycountries، وأضف أربعة عناصر إليه:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New Hashtable mycountries.Add("C","China") mycountries.Add("S","Sweden") mycountries.Add("F","France") mycountries.Add("I","Italy") fini if fini sous </script>
تحديد البيانات
يمكن للعنصر Hashtable إنشاء نصوص القيم تلقائيًا للعناصر التالية:
- asp:RadioButtonList
- asp:CheckBoxList
- asp:DropDownList
- asp:Listbox
لتحديد البيانات المربوطة مع جهاز التحكم RadioButtonList، يرجى إنشاء جهاز التحكم RadioButtonList في صفحة .aspx (بدون أي عناصر asp:ListItem)
<html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" /> </form> </body> </html>
ثم أضف سكريبت بناء القائمة:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New Hashtable mycountries.Add("C","China") mycountries.Add("S","Sweden") mycountries.Add("F","France") mycountries.Add("I","Italy") rb.DataSource=mycountries rb.DataValueField="Key" rb.DataTextField="Value" rb.DataBind() fini if fini sous </script> <html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" /> </form> </body> </html>
ثم نضيف подпрограмة ستُنفذ عند ضغط المستخدم على أي عنصر من جهاز التحكم RadioButtonList. عند ضغط زر اختيار واحد، سيظهر نص في label:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New Hashtable mycountries.Add("C","China") mycountries.Add("S","Sweden") mycountries.Add("F","France") mycountries.Add("I","Italy") rb.DataSource=mycountries rb.DataValueField="Key" rb.DataTextField="Value" rb.DataBind() fini if fini sous sub displayMessage(s as Object,e As EventArgs) lbl1.text="بلدك المفضل هو: " & rb.SelectedItem.Text fini sous </script> <html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" onSelectedIndexChanged="displayMessage" /> <p><asp:label id="lbl1" runat="server" /></p> </form> </body> </html>
ملاحظة:لا يمكنك اختيار طريقة ترتيب العناصر التي تضيفها إلىHashtable. إذا كنت ترغب في ترتيب العناصر حسب الأبجدية أو الرقم، استخدم كائن SortedList.
- الصفحة السابقة ArrayList WebForms
- الصفحة التالية SortedList WebForms