ASP.Net/Data Binding/DropDownList — различия между версиями

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

Текущая версия на 11:52, 26 мая 2010

ArrayList Data binding for asp:DropDownList (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Dim ColorList(6) as String
    Dim FontList as new ArrayList()
    
    Sub Page_Load
      ColorList(0) = "Red"
      ColorList(1) = "Orange"
      ColorList(2) = "Yellow"
      ColorList(3) = "Green"
      ColorList(4) = "Blue"
      ColorList(5) = "Indigo"
      ColorList(6) = "Violet"
    
      FontList.Add("Times New Roman")
      FontList.Add("Arial")
      FontList.Add("Verdana")
      FontList.Add("Comic Sans MS")
    
      If Not Page.IsPostback
        Dim ColorName as String
    
        For Each ColorName in ColorList
          ddlColorList.Items.Add(ColorName)
        Next
    
        ddlFontList.DataSource = FontList
        ddlFontList.DataBind()
    
      End If
    End Sub
    
    Sub btnSelectColor_Click(sender As Object, e As EventArgs)
      lblOutputMessage.Text = "You selected " & _
        ddlColorList.SelectedItem.Value & " text written in " & _
        ddlFontList.SelectedItem.Value
      lblOutputMessage.ForeColor = _
        System.Drawing.Color.FromName(ddlColorList.SelectedItem.Text)
      lblOutputMessage.Font.Name = _
        ddlFontList.SelectedItem.Text
    
    End Sub
    
    Sub btnAddFont_Click(sender As Object, e As EventArgs)
      FontList.Add(txtAddFont.Text)
    
      ddlFontList.DataSource = FontList
      ddlFontList.DataBind()
    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>
        </p>
        <p>
            Then select a font sytle from the list: 
            <asp:DropDownList id="ddlFontList" runat="server"></asp:DropDownList>
        </p>
        <p>
            &nbsp;<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>
        <p>
            Enter a new font for the list:<asp:TextBox id="txtAddFont" runat="server"></asp:TextBox>
            <asp:Button id="btnAddFont" onclick="btnAddFont_Click" runat="server" Text="Add New Font"></asp:Button>
        </p>
    </form>
</body>
</html>



Bind arraylist elements to asp dropdown and get selected one (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Sub Page_Load()
        if Not Page.IsPostBack Then
            Dim items As New ArrayList()
            items.Add("Apples")
            items.Add("Oranges")
            DropDownList1.DataSource = items
            DropDownList1.DataBind()
        End If
    End Sub
    Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Label1.Text = DropDownList1.SelectedItem.Text
    End Sub
</script>
<html>
<head id="Head1" runat="server">
    <title>Show IsPostBack</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:DropDownList
        id="DropDownList1"
        Runat="server" />
    
    <asp:Button
        id="Button1"
        Text="Select"
        OnClick="Button1_Click" 
        Runat="server" />
    
    You selected:
    <asp:Label  
        id="Label1"
        Runat="server" />
    
    </div>
    </form>
</body>
</html>



Bind ArrayList to asp:dropdownlist (VB.net)

<%@Page language="vb" %>
<script runat="server" language="vb">
Sub Page_Load()
    Dim AnimalArrayList as new ArrayList()
    AnimalArrayList.Add("Dog")
    AnimalArrayList.Add("Cat")
    AnimalArrayList.Add("Elephant")
    AnimalArrayList.Add("Lion")
    AnimalArrayList.Add("Cat")
    MyDropDownList.DataSource = AnimalArrayList
    MyDropDownList.DataBind()
End Sub
</script>
<html>
<form id="Form1" method="post" runat="server">
<asp:dropdownlist id="MyDropDownList" runat="server" />
</form>
</html>



Bind Color name to asp:dropdownlist (VB.net)

<%@ Page Language="VB" %>
<html>
<head>
   <title>Collection DataBinding Example</title>
   <script runat="server">
      Dim FontColor As String
      Sub Page_Load()
         If Not IsPostBack Then
            Dim Colors As New ArrayList()
            Colors.Add("Red")
            Colors.Add("Green")
            Colors.Add("Blue")
            Color.DataSource = Colors
            DataBind()
            Color.SelectedIndex = 0
         End If
         Output.Text = "The value for FontColor is " & _
            Color.SelectedItem.Value & "."
         Output.ForeColor = System.Drawing.Color.FromName(Color.SelectedItem.Value)
         End Sub
  </script>
</head>
<body>
   <h1>Collection DataBinding Example</h1>
   <form runat="server">
      Choose a color from the list for the font color:
      <asp:dropdownlist id="Color" autopostback="True" runat="server"/>
      <br/>
      <asp:label id="Output" runat="server"/>
   </form>
</body>
</html>



Bind DataTable to asp:DropDownList (VB.net)

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    If Not IsPostBack Then
        Dim MyDT As New DataTable
        Dim MyRow As DataRow
        MyDT.Columns.Add(New DataColumn("DepartmentID", _
            GetType(Int32)))
        MyDT.Columns.Add(New DataColumn("DepartmentName", _
            GetType(String)))
        MyRow = MyDT.NewRow()
        MyRow(0) = 1
        MyRow(1) = "Marketing"
        MyDT.Rows.Add(MyRow)
        MyRow = MyDT.NewRow()
        MyRow(0) = 2
        MyRow(1) = "Sales"
        MyDT.Rows.Add(MyRow)
        MyRow = MyDT.NewRow()
        MyRow(0) = 3
        MyRow(1) = "Support"
        MyDT.Rows.Add(MyRow)
        MyRow = MyDT.NewRow()
        MyRow(0) = 4
        MyRow(1) = "Customer Service"
        MyDT.Rows.Add(MyRow)
        ddl2.DataSource = MyDT
        ddl2.DataBind()
        ddl2.Items(3).Selected=True
    End If
End Sub
Sub ddl1_Clicked(sender As Object, e As EventArgs)
    lblMessage.Text = "Preferred office color: " _
        & ddl1.SelectedItem.Text & "<BR>"
End Sub
Sub ddl2_Clicked(sender As Object, e As EventArgs)
    lblMessage2.Text = "ID of Department you work in: " _
        & ddl2.SelectedItem.Value & "<BR>"
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>DropDownList Control Sample Page</TITLE>
</HEAD>
<BODY >
<form runat="server">
<Font Face="Tahoma">
<asp:Label
    id="lblMessage"
    runat="server"
    Font-Bold="True"
    Text="Preferred office color:"
/>
<BR>
<asp:DropDownList
    id="ddl1" 
    runat="server"
    AutoPostBack="True"
    OnSelectedIndexChanged="ddl1_Clicked"
>
    <asp:ListItem>Blue</asp:ListItem>
    <asp:ListItem>Red</asp:ListItem>
    <asp:ListItem>Green</asp:ListItem>
    <asp:ListItem Selected>Purple</asp:ListItem>
    <asp:ListItem>Black</asp:ListItem>
    <asp:ListItem>Gold</asp:ListItem>
</asp:DropDownList>
<HR>
<asp:Label
    id="lblMessage2"
    runat="server"
    Font-Bold="True"
    Text="ID of Department you work in:<BR>"
/>
<BR>
<asp:DropDownList 
    id="ddl2" 
    runat="server"
    AutoPostBack="True"
    OnSelectedIndexChanged="ddl2_Clicked"
    DataTextField="DepartmentName"
    DataValueField="DepartmentID"
    BackColor="LightYellow"
    ForeColor="DarkRed"
>
</asp:DropDownList>
</Font>
</Form>
</BODY>
</HTML>



Bind Hashtable to DropDownList (VB.net)

<%@ Page Language="VB" %>
<script runat="server">
    Dim ColorList(6) as String
    Dim FontList as new ArrayList()
    Dim QuoteList as new Hashtable()
    
    Sub Page_Load
      ColorList(0) = "Red"
      ColorList(1) = "Orange"
      ColorList(2) = "Yellow"
      ColorList(3) = "Green"
      ColorList(4) = "Blue"
      ColorList(5) = "Indigo"
      ColorList(6) = "Violet"
    
      FontList.Add("Times New Roman")
      FontList.Add("Arial")
      FontList.Add("Verdana")
      FontList.Add("Comic Sans MS")
      
      QuoteList.Add ("Quotation", "Quotation. ")
      QuoteList.Add ("Joke", "Joke")
      QuoteList.Add ("Wisdom", "Wisdom")
      QuoteList.Add ("Saying", "Saying")
    
      If Not Page.IsPostback
        Dim ColorName as String
    
        For Each ColorName in ColorList
          ddlColorList.Items.Add(ColorName)
        Next
    
        ddlFontList.DataSource = FontList
        ddlFontList.DataBind()
        
        ddlQuoteList.DataSource = QuoteList.Keys
        ddlQuoteList.DataBind()
    
      End If
    End Sub
    
    Sub btnSelectColor_Click(sender As Object, e As EventArgs)
      lblOutputMessage.Text = QuoteList(ddlQuoteList.SelectedItem.Text)
      lblOutputMessage.ForeColor = _
        System.Drawing.Color.FromName(ddlColorList.SelectedItem.Text)
      lblOutputMessage.Font.Name = _
        ddlFontList.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>
        </p>
        <p>
            Then select a font sytle from the list: 
            <asp:DropDownList id="ddlFontList" runat="server"></asp:DropDownList>
        </p>
        <p>
            Finally, choose your output message: 
            <asp:DropDownList id="ddlQuoteList" runat="server"></asp:DropDownList>
        </p>
        <p>
            &nbsp;<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>



Use ArrayList to fill dropdown value (C#)

<%@ Page Language="C#" Debug="true" %>
<script runat="server">
    void Page_Load()
    {
         ArrayList list = new ArrayList();
         list.Add("none");
         list.Add("Canada Post");
         list.Add("UPS");
    
         list.Insert(1,"FedEx");
    
         MyDropDownList.DataSource = list;
         MyDropDownList.DataBind();
    }
</script>
<html>
<head>
    <title>ArrayList Example</title>
</head>
<body>
    <form id="Form1" method="post" runat="server">
        <asp:dropdownlist id="MyDropDownList" runat="server"></asp:dropdownlist>
    </form>
</body>
</html>