ASP.Net/Collections/Array

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Array.IndexOf and Array.LastIndexOf (VB.net)

<%@Page language="vb" %>
<script runat="server" language="vb">
  Sub Page_Load()
    Dim intCounter As Integer = -1
    Dim AnimalArray(4) As string
    AnimalArray(0) = "Dog"
    AnimalArray(1) = "Cat"
    AnimalArray(2) = "Elephant"
    AnimalArray(3) = "Lion"
    AnimalArray(4) = "Cat"
    Do
      intCounter = Array.IndexOf(AnimalArray, "Cat", intCounter+1)
      MyText.InnerHtml += "AnimalArray(" & intCounter & ")<br/>"
    Loop Until intCounter = Array.LastIndexOf(AnimalArray, "Cat")
  End Sub
</script>
<html>
  The string "Cat" occurs in the following elements:
  <br/>
  <div id="MyText" runat="server" />
</html>



Array.IndexOf(), Array.LastIndexOf() (C#)

<%@Page Language="c#" %>
<script runat="server" Language="c#">
  void Page_Load() 
  {
    int intCounter = -1;
    string[] AnimalArray = new string[5]
                {  "Dog", "Cat", "Elephant", "Lion", "Cat"};
    do
    {
      intCounter = Array.IndexOf(AnimalArray, "Cat", intCounter+1);
      MyText.InnerHtml += "AnimalArray[" + intCounter + "]<br/>";
    } while (intCounter != Array.LastIndexOf(AnimalArray, "Cat")); 
  }
</script>
<html>
  The string "Cat" occurs in the following elements:
  <br/>
  <div id="MyText" runat="server" />
</html>



Array Reverse Demo (VB.net)

<%@Page language="vb" %>
<script runat="server" language="vb">
  Sub Page_Load()
    Dim AnimalArray(4) As String
    Dim strAnimal As String
    AnimalArray(0) = "Dog"
    AnimalArray(1) = "Cat"
    AnimalArray(2) = "Elephant"
    AnimalArray(3) = "Lion"
    AnimalArray(4) = "Cat"
    Array.Reverse(AnimalArray)
    For Each strAnimal In AnimalArray
      MyDropDownList.Items.Add(strAnimal)
    Next
  End Sub
</script>
<html>
<form id="Form1" method="post" runat="server">
<asp:dropdownlist id="MyDropDownList" runat="server" />
</form>
</html>



Change array length (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Dim ColorList(6) as String
    Sub Page_Load
      ColorList(0) = "Red"
      ColorList(1) = "Orange"
      ColorList(2) = "Yellow"
      ColorList(3) = "Green"
      ColorList(4) = "Blue"
      ColorList(5) = "Indigo"
      ColorList(6) = "Violet"
      ReDim Preserve ColorList(7)
      ColorList(7) = "Magenta"
    
      If Not Page.IsPostback
        Dim ColorName as String
    
        For Each ColorName in ColorList
          ddlColorList.Items.Add(ColorName)
        Next
      End If
    End Sub
    
    Sub btnSelectColor_Click(sender As Object, e As EventArgs)
      lblOutputMessage.Text = "You selected " & ddlColorList.SelectedItem.Value
      lblOutputMessage.ForeColor = _
        System.Drawing.Color.FromName(ddlColorList.SelectedItem.Text)
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            Select a color from the list:<asp:DropDownList id="ddlColorList" runat="server"></asp:DropDownList>
            <asp:Button id="btnSelectColor" onclick="btnSelectColor_Click" runat="server" Text="Click here!"></asp:Button>
        </p>
        <p>
            <asp:Label id="lblOutputMessage" runat="server"></asp:Label>
        </p>
    </form>
</body>
</html>



Define and use array (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Dim ColorList(6) as String
    
    Sub Page_Load
      ColorList(0) = "Red"
      ColorList(1) = "Orange"
      ColorList(2) = "Yellow"
      ColorList(3) = "Green"
      ColorList(4) = "Blue"
      ColorList(5) = "Indigo"
      ColorList(6) = "Violet"
            
      If Not Page.IsPostback
        Dim ColorName as String
    
        For Each ColorName in ColorList
          ddlColorList.Items.Add(ColorName)
        Next
      End If
    End Sub
    
    Sub btnSelectColor_Click(sender As Object, e As EventArgs)
      lblOutputMessage.Text = "You selected " & ddlColorList.SelectedItem.Value
      lblOutputMessage.ForeColor = _
        System.Drawing.Color.FromName(ddlColorList.SelectedItem.Text)
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            Select a color from the list:<asp:DropDownList id="ddlColorList" runat="server"></asp:DropDownList>
            <asp:Button id="btnSelectColor" onclick="btnSelectColor_Click" runat="server" Text="Click here!"></asp:Button>
        </p>
        <p>
            <asp:Label id="lblOutputMessage" runat="server"></asp:Label>
        </p>
    </form>
</body>
</html>



For each loop: array (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Dim ColorList(6) as String
    
    Sub Page_Load
      ColorList(0) = "Red"
      ColorList(1) = "Orange"
      ColorList(2) = "Yellow"
      ColorList(3) = "Green"
      ColorList(4) = "Blue"
      ColorList(5) = "Indigo"
      ColorList(6) = "Violet"
      Array.Sort(ColorList)
      
      If Not Page.IsPostback
        Dim ColorName as String
    
        For Each ColorName in ColorList
          ddlColorList.Items.Add(ColorName)
        Next
      End If
    End Sub
    
    Sub btnSelectColor_Click(sender As Object, e As EventArgs)
      lblOutputMessage.Text = "You selected " & ddlColorList.SelectedItem.Value
      lblOutputMessage.ForeColor = _
        System.Drawing.Color.FromName(ddlColorList.SelectedItem.Text)
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            Select a color from the list:<asp:DropDownList id="ddlColorList" runat="server"></asp:DropDownList>
            <asp:Button id="btnSelectColor" onclick="btnSelectColor_Click" runat="server" Text="Click here!"></asp:Button>
        </p>
        <p>
            <asp:Label id="lblOutputMessage" runat="server"></asp:Label>
        </p>
    </form>
</body>
</html>



For Each loop through an Array (VB.net)

<%@Page language="vb" %>
<script runat="server" language="vb">
  Sub Page_Load()
    Dim AnimalArray(4) As String
    Dim strAnimal As String
    AnimalArray(0) = "Dog"
    AnimalArray(1) = "Cat"
    AnimalArray(2) = "Elephant"
    AnimalArray(3) = "Lion"
    AnimalArray(4) = "Cat"
    For Each strAnimal In AnimalArray
      MyDropDownList.Items.Add(strAnimal)
    Next
  End Sub
</script>
<html>
<form id="Form1" method="post" runat="server">
<asp:dropdownlist id="MyDropDownList" runat="server" />
</form>
</html>



Get array by index (VB.net)

<%@Page language="vb" %>
<script runat="server" language="vb">
  Sub Page_Load(Source As Object, E as EventArgs)
    Dim AnimalArray(4) as string
    AnimalArray(0) = "Dog"
    AnimalArray(1) = "Cat"
    AnimalArray(2) = "Elephant"
    AnimalArray(3) = "Lion"
    AnimalArray(4) = "Cat"
    MyLabel.Text = AnimalArray.GetValue(2)
  End Sub
</script>
<html>
<form id="Form1" method="post" runat="server">
<asp:label id="MyLabel" runat="server" />
</form>
</html>



Get array element by index (C#)

<%@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();
  }
</script>
<html>
  <asp:label id="MyLabel" runat="server" />
</html>



Sort an Array (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Dim ColorList(6) as String
    
    Sub Page_Load
      ColorList(0) = "Red"
      ColorList(1) = "Orange"
      ColorList(2) = "Yellow"
      ColorList(3) = "Green"
      ColorList(4) = "Blue"
      ColorList(5) = "Indigo"
      ColorList(6) = "Violet"
      Array.Sort(ColorList)
      
      If Not Page.IsPostback
        Dim ColorName as String
    
        For Each ColorName in ColorList
          ddlColorList.Items.Add(ColorName)
        Next
      End If
    End Sub
    
    Sub btnSelectColor_Click(sender As Object, e As EventArgs)
      lblOutputMessage.Text = "You selected " & ddlColorList.SelectedItem.Value
      lblOutputMessage.ForeColor = _
        System.Drawing.Color.FromName(ddlColorList.SelectedItem.Text)
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            Select a color from the list:<asp:DropDownList id="ddlColorList" runat="server"></asp:DropDownList>
            <asp:Button id="btnSelectColor" onclick="btnSelectColor_Click" runat="server" Text="Click here!"></asp:Button>
        </p>
        <p>
            <asp:Label id="lblOutputMessage" runat="server"></asp:Label>
        </p>
     </form>
</body>
</html>



Sort array reversely (C#)

<%@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>
<html>
<form id="Form1" method="post" runat="server">
<asp:dropdownlist id="MyDropDownList" runat="server" />
</form>
</html>



Use array: find item index (C#)

<%@ Page Language="C#" Debug="true" %>
<script runat="server">
void Page_Load(){
   if (Page.IsPostBack){
       string[] choice = new string[3];
       choice[0] = "1";
       choice[1] = "2";
       choice[2] = "3";
       lblShipper.Text = "Shipper ID for " + txtShipNum.Text + " is " + Array.IndexOf(choice, txtShipNum.Text);
       lblShipper.Visible = true;
   }
}
</script>
<html>
<head>
    <title>Array Example</title>
</head>
<body>
    <form runat="server">
        Please enter your shipper name. 
        (should be "1"  or  "2"  or  "3") 
        <br><asp:TextBox id="txtShipNum" runat="server" width="300px"></asp:TextBox>
        <br />
        Press button to find the Shipper ID number
        <asp:Button id="Button1" runat="server" Text="Submit"></asp:Button>
        <br />
        <asp:Label id="lblShipper" runat="server"></asp:Label>
    </form>
</body>
</html>



Use String Array to store Asp Label Text (C#)

<%@ Page Language="C#" Debug="true" %>
<script runat="server">
    void Page_Load()
    {
    string [] strMarx = new string[6];
    strMarx[0] = "A";
    strMarx[1] = "B";
    strMarx[2] = "C";
    strMarx[3] = "D";
    strMarx[4] = "E";
    strMarx[5] = "F";
    
    lblMarx.Text = strMarx[3];
    string [] strFriends = new string[5];
    strFriends[1] = "Mr. J";
    strFriends[4] = "Mr. G";
    strFriends[3] = "Mrs. S";
    lblFriend.Text = strFriends[4];
    }
</script>
<html>
<head>
    <title>Array Example</title>
</head>
<body>
    <form runat="server">
        A Marx Brother: 
        <asp:Label id="lblMarx" runat="server"></asp:Label><br>
        A friend: 
        <asp:Label id="lblFriend" runat="server"></asp:Label><br>
    </form>
</body>
</html>



Use Two dimensional array (C#)

<%@ Page Language="C#" Debug="true" %>
<script runat="server">
    void Page_Load()
    {
        if(Page.IsPostBack)
        {
            string [,] strClient = new string[4,3]; // 4 people, 3 attributes each
        
            strClient[0,0] = "First Name 1";
            strClient[0,1] = "Last Name 1";
            strClient[0,2] = "111-111-1111";
            strClient[1,0] = "First Name 2";
            strClient[1,1] = "Last Name 2";
            strClient[1,2] = "222-222-2222";
            strClient[2,0] = "First Name 3";
            strClient[2,1] = "Last Name 3";
            strClient[2,2] = "333-333-3333";
            strClient[3,0] = "First Name 4";
            strClient[3,1] = "Last Name 4";
            strClient[3,2] = "444-444-4444";
        
            int intIDnumber = Convert.ToInt32(txtID.Text);
        
            lblNameFirst.Text = strClient[intIDnumber,0];
            lblNameLast.Text = strClient[intIDnumber,1];
            lblTel.Text = strClient[intIDnumber,2];
        
        }
    
    }
</script>
<html>
<head>
    <title>Array Multidimensional Example</title>
</head>
<body>
    <form runat="server">
        Enter a client number (from 0 to 3) 
        <asp:TextBox runat="server" ID="txtID"></asp:TextBox><br>
        
        <asp:Label id="lblNameFirst" runat="server"></asp:Label><br>
        <asp:Label id="lblNameLast" runat="server"></asp:Label><br>
        <asp:Label id="lblTel" runat="server"></asp:Label><br>
        
        <asp:Button runat="server" Text="Button"></asp:Button>
    </form>
</body>
</html>