Results 1 to 2 of 2

Thread: dynamically generating options for select box

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2003
    Location
    sunnyvale, ca (NOT CANADA)
    Posts
    2

    dynamically generating options for select box

    in classic asp i do this:

    <select name=...>
    <%for i=0 to UBound(Session("MyGroupsArray"))%>
    <option><%=Session("MyGropupsArray")(i)%></option>
    <%next%>
    </select>

    in asp.net i did:

    <tr><td>
    <asp:label for="selGroups">Select your default group:</asp:label>
    <asp:dropdownlist id="selGroups" runat="server">
    <%For i = 0 to UBound(Session("AllGroups"))%>
    <asp:listitem><%=Session("AllGroups")(i)%></asp:listitem>
    <%Next%>
    </asp:dropdownlist></td></tr>

    I AM GETTING A BLOOMIN' ERROR !@#$% why?

    how do i code a dropdownlist dynamically in .NET?

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Here's one way you can do it with DataBinding. The aspx page:
    Code:
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="DynoDropDown.aspx.vb" Inherits="localhost.DynoDropDown"%>
    <html>
    	<body>
    		<form runat="server">
    			<asp:DropDownList ID="myDropDown" Runat="server"/>
    		</form>
    	</body>
    </html>
    and the code behind:
    VB Code:
    1. Imports System
    2. Imports System.Collections
    3. Public Class DynoDropDown : Inherits System.Web.UI.Page
    4.     Protected myDropDown As DropDownList
    5.     Protected Overrides Sub OnInit(ByVal e As EventArgs)
    6.         Dim list As New ArrayList(10)
    7.         For i As Int32 = 1 To 10
    8.             list.Add("info" + i.ToString())
    9.         Next
    10.         Session.Item("MyArrayList") = list
    11.         myDropDown.DataSource = CType(Session.Item("MyArrayList"), ArrayList)
    12.         myDropDown.DataBind()
    13.     End Sub
    14. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width