Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > VS.NET 2002/2003
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 18th, 2003, 11:03 AM
Registered User
 
Join Date: Aug 2003
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default creating dynamic list

in classic asp, iterating through an array or recordset will create the required options for a selection box. e.g.:

<select>
   <%do while not rs.eof%>
      <option value="<%=rs(0)%>"><%=rs(1)%></option>
   <%rs.MoveNext%>
   <%loop%>
</select>

how do i do the same with asp:dropdownlist in aspx file, and my code for dynamic content in a user control ascx file?

Reply With Quote
  #2 (permalink)  
Old August 19th, 2003, 07:15 AM
Authorized User
 
Join Date: Aug 2003
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Page: dropdownlist.ascx (user control)

Code:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="dropdownlist.ascx.vb" Inherits="dropDownList.dropdownlist" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
codebehind:
Code:
Imports System.Data.SqlClient

Public Class dropdownlist
    Inherits System.Web.UI.UserControl

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        If Not Page.IsPostBack Then
            Binddata()
        End If
    End Sub


    Sub Binddata()

        'create connection using pubs database
        Dim sConn As String = "server=localhost;User Id=sa;Password=password;Initial Catalog=pubs"
        Dim myConn As New SqlConnection(sConn)

        Dim sSQL As String = "SELECT stor_id, stor_name FROM Stores ORDER BY stor_name"

        Dim myCommand As New SqlCommand(sSQL, myConn)

        myConn.Open()

        ' create a datareader
        Dim dr As SqlDataReader
        dr = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

        ' setup the text and value fields for the drop down list
        DropDownList1.DataTextField = "stor_name"
        DropDownList1.DataValueField = "stor_id"

        ' databind the reader to the dropdownlist
        DropDownList1.DataSource = dr
        DropDownList1.DataBind()

    End Sub
End Class
Reply With Quote
  #3 (permalink)  
Old August 19th, 2003, 10:59 PM
Registered User
 
Join Date: Aug 2003
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thx for responding.

while trying to make it work, i attempted to do it all within the aspx file, but did not succeed. i want to bind my asp:dropdownlist to Session("AllGroups"), a one-dimensional array. here's my code that raised an error:

the html code:

<tr><td>
<asp:label for="selGroups">Select your default group:</asp:label>
<asp:dropdownlist id="selGroups" runat="server">
</asp:dropdownlist></td></tr>

<tr><td style="line-height:20px;"> </td></tr>

<tr><td><asp:label for="selForums">Select your default forum:</asp:label>
<asp:dropdownlist id="selForums" runat="server">
<asp:listitem>Network</asp:listitem>
<asp:listitem>Social</asp:listitem>
</asp:dropdownlist></td></tr>

the runtime binding code is:

<script language="VB" runat="server">
...
Sub Page_Load()
        selGroups.DataSource=Session("AllGroups")
        selGroups.DataBind()
        selForums.DataSource=Session("AllGroups")
        selForums.DataBind()
End Sub


Reply With Quote
  #4 (permalink)  
Old August 20th, 2003, 03:07 AM
Authorized User
 
Join Date: Aug 2003
Location: , , .
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The ascx (control) code would be :

Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            BindData()

        End If

    End Sub

    Sub BindData()

        If Session("MyList") Is Nothing Then
            DropDownList1.Visible = False
        Else
            Dim arrList As ArrayList
            arrList = Session("MyList")

            DropDownList1.DataSource = arrList
            DropDownList1.DataBind()
        End If

    End Sub
Then create the session as follows:
Code:
        Dim arrList As New ArrayList
        arrList.Add("Oranges")
        arrList.Add("Apples")
        arrList.Add("Bananas")

        Session("MyList") = arrList
Reply With Quote
  #5 (permalink)  
Old September 2nd, 2003, 03:13 AM
Registered User
 
Join Date: Aug 2003
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thx

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic List Content cyberddindia Classic ASP Basics 3 October 24th, 2006 10:29 AM
dynamic list al-hijjawi ASP.NET 1.0 and 1.1 Basics 3 April 7th, 2006 11:05 AM
Tell about dynamic list boxes shyamprasad Classic ASP Components 3 February 8th, 2005 02:38 PM
how to build a dynamic list javacal Classic ASP Basics 4 September 18th, 2003 04:18 AM
Dynamic List Boxes swhite Access VBA 10 September 11th, 2003 01:27 PM



All times are GMT -4. The time now is 12:11 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.