Results 1 to 4 of 4

Thread: DropDownList Issue

  1. #1

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    DropDownList Issue

    Hi All

    I'm trying to learn ASP.Net (VB)

    I'm having a question on a dropdown list. I have populated the list using a dataset ( a list of databases).

    The dropdown list code
    asp.net Code:
    1. <asp:DropDownList ID="ddlDBs" runat="server" AutoPostBack="True" Width="192px">
    2.         </asp:DropDownList><br />

    The code to populate
    vb.net Code:
    1. Protected Sub getDatabaseNames()
    2.         Dim oDA As SqlClient.SqlDataAdapter
    3.         Dim oDS As DataSet = New DataSet
    4.         Dim oCmd As SqlClient.SqlCommand = New SqlClient.SqlCommand
    5.         Try
    6.             Dim SQL As String = String.Empty
    7.             SQL = "Select Name From sys.databases Where Name NOT IN ('master','msdb','model','tempdb') Order by Name"
    8.  
    9.             If Me.sqlConnection.State = ConnectionState.Closed Then
    10.                 Me.openConn()
    11.             End If
    12.             oDA = New SqlClient.SqlDataAdapter(SQL, Me.sqlConnection)
    13.            
    14.             oDA.Fill(oDS)
    15.             Me.ddlDBs.DataSource = oDS.Tables(0).DefaultView
    16.             Me.ddlDBs.DataTextField = "Name"
    17.             Me.ddlDBs.DataBind()
    18.             Me.ddlDBs.ClearSelection()
    19.  
    20.         Catch ex As Exception
    21.             Me.lblMessages.Text = ex.Message
    22.         Finally
    23.             If oDS IsNot Nothing Then
    24.                 oDS.Dispose()
    25.             End If
    26.             If oCmd IsNot Nothing Then
    27.                 oCmd.Dispose()
    28.             End If
    29.         End Try
    30.         If Me.sqlConnection.State = ConnectionState.Open Then
    31.             Me.closeConn()
    32.         End If
    33.  
    34.     End Sub

    This part works. I get a list of databases in the list. My problem is when I make a selection from the list. I always get the first item in the list as selected not what I actually select.

    Here is the code for the selected index change
    vb.net Code:
    1. Protected Sub ddlDBs_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlDBs.SelectedIndexChanged
    2.         Response.Write("The database selected is: " & Me.ddlDBs.SelectedItem.Text & System.Environment.NewLine)
    3.         'Response.Write("The database selected is: " & Me.ddlDBs.SelectedIndex)
    4.     End Sub
    Last edited by GaryMazzone; Nov 14th, 2009 at 02:43 PM. Reason: Highligting
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: DropDownList Issue

    Hey,

    Rather than use SelectedItem, try SelectedValue.

    Also, I know it may seem "easy" to use Response.Write, I have found that it can actually confuse matters when it comes to debugging and layouts etc, so I would encourage you not to use it.

    Rather write out to a Label Control, or simply do a Debug.Write to see it in the Output Window.

    Hope that helps!!

    Gary

  3. #3

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: DropDownList Issue

    Tried Still always comes back to the first item in the list box. I can change the item. I see the item get selected but as soon as I let go of the mouse button the selection goes back to the first item.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: DropDownList Issue

    Hey,

    Can you strip this down to the bare minimum and try the following:

    Code:
    Public Partial Class WebForm4
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            DropDownList1.Items.Add("1")
            DropDownList1.Items.Add("2")
            DropDownList1.Items.Add("3")
            DropDownList1.Items.Add("4")
            DropDownList1.Items.Add("5")
            DropDownList1.Items.Add("6")
        End Sub
    
        Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DropDownList1.SelectedIndexChanged
            Label1.Text = DropDownList1.SelectedValue.ToString()
        End Sub
    End Class
    Code:
    <&#37;@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm4.aspx.vb" Inherits="VBWebApplication.WebForm4" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
            </asp:DropDownList>
        
        </div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </form>
    </body>
    </html>
    This works exactly how I would expect it to.

    Gary

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