Skip to content

Dynamically adding and binding items to asp:DropDownList

August 7, 2011

I had recently discovered an alternative way to populate a dynamically drop down list for asp.net controls when trying to find a way to get round a problem which only worked on old asp.net controls, only to later to find out that this wasn’t the cause of the problem, it was something else.

Anyway, I used a vb.net script to dynamically populate an asp.net drop down list, by adding some color items to an ArrayList in vb.net, and binding the ArrayList to the dropdown list in script, which I didn’t know was possible. It also includes code on how to dynamically bind different font items to a drop down list aswell. Here is the code.

<%@ Page Language=“VB” %>
<script runat=“server”>
    Dim ColorList(6as 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>

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: