Stick to Learning asp.net--(12)

Source: Internet
Author: User
Tags foreach definition insert sort tostring
asp.net objects and structured data


Collection



As an array of collections: The following is a simple example:
<% @Page language= "C #"%>
<script runat= "Server" language= "C #" >
void Page_Load ()
{
string[] Animalarray = new String[5]
{"Dog", "Cat", "Elephant", "Lion", "Cat"};
MyLabel.Text = Animalarray.getvalue (2). ToString () + "<font color=red>" +array.indexof (Animalarray, "Cat") + "</font>";
}
</script>
<asp:label id= "MyLabel" runat= "Server"/>

The output of this program is clearly: Elephant.

The following is a program that is traversed somewhere in an array:
<% @Page language= "C #"%>
<script runat= "Server" language= "C #" >
void Page_Load ()
{
int intcounter =-1;
string[] Animalarray = new String[5]
{"Dog", "Cat", "Elephant", "Lion", "Cat"};
Todo
{
intcounter = Array.indexof (Animalarray, "Cat", intcounter+1);
mytext.innerhtml + + "animalarray[" + intcounter + "]<br/>";
while (intcounter!= array.lastindexof (Animalarray, "Cat"));
}
</script>
The string "Cat" occurs in the following elements:
<br/>
<div id= "MyText" runat= "Server"/>



Reverses the order of elements in an array: Array.reverse (Array1);
Sort the elements: Array.Sort (Array1);

Using arrays:


<% @Page language= "C #"%>
<script runat= "Server" language= "C #" >
void Page_Load ()
{
string[] Animalarray = new String[5]
{"Dog", "Cat", "Elephant", "Lion", "Cat"};
Array.reverse (Animalarray);
foreach (String stranimal in Animalarray)
{
MYDROPDOWNLIST.ITEMS.ADD (Stranimal);
}
}
</script>
<form id= "Form1" method= "POST" runat= "Server" >
<asp:dropdownlist id= "mydropdownlist" runat= "Server"/>
</form>


Data binding:
A common feature of a collection is that you can specify a collection as a data source by adding a pair of statements.
As in the previous program:

Mydropdownlist.datasource=animalarray;
Mydropdownlist.databind ();

ArrayList


Definition: Arraylins myarraylist=new ArrayList ();
Each new item will automatically be added to the end of the document!
The previous program can be modified like this:

<% @Page language= "C #"%>
<script runat= "Server" language= "C #" >
void Page_Load ()
{
ArrayList animalarraylist = new ArrayList ();
Animalarraylist.add ("Dog");
Animalarraylist.add ("Cat");
Animalarraylist.add ("Elephant");
Animalarraylist.add ("Lion");
Animalarraylist.add ("Cat");
Mydropdownlist.datasource = animalarraylist;
Mydropdownlist.databind ();
}
</script>
<form id= "Form1" method= "POST" runat= "Server" >
<asp:dropdownlist id= "mydropdownlist" runat= "Server"/>
</form>
Some methods of ArrayList:
Add to:
Myarraylist.add ("Pig");
Insert:
Myarraylist.insert (3, "long");
Delete:
Myarraylist.removeat (3);
OR
Myarraylist.remove ("Cat");

HashTable



Create Hashtable:hashtable myhashtable=new HashTable ();

Two ways to add values:
Myhashtable.add ([UK], "Hongkong");
OR
myhashtable[uk]= "Hongkong";

Hashtable.add () takes two parameters, one for keys and one for values. Both belong to the type object. The value passed for the key is an integer, so it must be boxed to be passed as an object. The value passed for the value is a string, which is a reference type, so no boxing of strings. Get one point for each correct one.

e.g.:
<% @Page language= "C #" debug= "true"%>
<script runat= "Server" language= "C #" >
void Page_Load (object source, EventArgs e)
{
Hashtable myhashtable = new Hashtable ();
myhashtable["UK"] = "United Kingdom";
myhashtable["US"] = "United States";
myhashtable["DE"] = "Germany";
if (!) ( Page.IsPostBack))
{
foreach (DictionaryEntry Item in myhashtable)
{
ListItem Newlistitem = new ListItem ();
Newlistitem.text = Item.Value.ToString ();
Newlistitem.value = Item.Key.ToString ();
MYDROPDOWNLIST.ITEMS.ADD (Newlistitem);
}
}
}
void Click (object source, EventArgs e)
{
MyLabel.Text = MyDropDownList.SelectedItem.Value;
}
</script>
<form runat= "Server" >
<asp:dropdownlist id= "mydropdownlist" runat= "Server"/>
<asp:button id= "MyButton" runat= "Server" text= "OK" onclick= "click"/>
<br/><br/>
<asp:label id= "MyLabel" runat= "Server" text= ""/>
</form>


SortedList


Like Hashtable, where the value sort is sorted by the health value, not the value!
Use:
<% @Page language= "C #" debug= "true"%>
<script runat= "Server" language= "C #" >
void Page_Load (object source, EventArgs e)
{
SortedList mysortedlist = new SortedList ();
mysortedlist["Armadillo"]= "any of a family. Small bony plates";
mysortedlist["Amaryllis"]= "an autumn-flowering ... Hippeastrum or Sprekelia] ";
mysortedlist["Zebra"]= "any of several fleet ... white or buff";
mysortedlist["Artichoke"]= "a tall composite herb ... cooked as a vegetable";
if (!) ( Page.IsPostBack))
{
foreach (DictionaryEntry Item in mysortedlist)
{
ListItem Newlistitem = new ListItem ();
Newlistitem.text = Item.Key.ToString ();
Newlistitem.value = Item.Value.ToString ();
MYDROPDOWNLIST.ITEMS.ADD (Newlistitem);
}
}
}
void Click (object source, EventArgs e)
{
MyLabel.Text = MyDropDownList.SelectedItem.Value;
}
</script>
<form runat= "Server" >
Pick A WORD from the list:
<asp:dropdownlist id= "mydropdownlist" runat= "Server"/>
<asp:button id= "MyButton" runat= "Server" text= "OK" onclick= "click"/>
<br/><br/>
<b>definition: </b>
<asp:label id= "MyLabel" runat= "Server" text= ""/>
</form>



(380)




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.