ASP.Net/Language Basics/For Each

Материал из .Net Framework эксперт
Версия от 11:53, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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 for string (C#)

<%@ Page Language="C#" %>
<script runat="server">
void page_Load()
{
  string[] arrCities = new string[3];
  arrCities[0]=("A");
  arrCities[1]=("B");
  arrCities[2]=("C"); 
  foreach (string item in arrCities) {
    lblOut.Text += item + "<BR>";
  }
}
</script>
<html>
    <head>
    </head>
    <body>
        <form runat="server">
        <asp:Label runat="server" ID="lblOut"/><br/>
        </form>
    </body>
</html>



For each loop to reverse a string (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Function ValidateCard(cardNumber As String) As Boolean
      Dim reverseNumber As String
      Dim ch As Char
    
      For Each ch in cardNumber
        reverseNumber = ch & reverseNumber
      Next
    
      Dim doubledAlternates As String
      Dim thisDigit, index As Integer
    
      For index = 1 to reverseNumber.Length
        thisDigit = CInt(reverseNumber.SubString(index - 1, 1))
        If index mod 2 = 0 Then
          thisDigit = thisDigit * 2
        End If
        doubledAlternates = doubledAlternates & thisDigit
      Next
    
      Dim sum As Integer
      For index = 1 to doubledAlternates.Length
        sum = sum + CInt(doubledAlternates.SubString(index - 1, 1))
      Next
    
      ValidateCard = sum <> 0 AND sum mod 10 = 0
    
    End Function
    
    Sub Button1_Click(sender As Object, e As EventArgs)
      If ValidateCard(TextBox1.Text) Then
        Label1.Text = "That"ll do nicely sir!"
      Else
        Label1.Text = "There"s nothing for you here!"
      End If
    End Sub
</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            Enter the number: 
            <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
        </p>
        <p>
            <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Check Card Number"></asp:Button>
        </p>
        <p>
            <asp:Label id="Label1" runat="server"></asp:Label>
        </p>
    </form>
</body>
</html>