Results 1 to 3 of 3

Thread: listbox onclick options?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    listbox onclick options?

    I am trying to create a little form that has a table with 3 columns, first column would have a single select listbox, second column contains a multiselect listbox and the third and last listbox is for results. I want to be able to select the first column and based on what I click it loads the third column with what has already been selected (if so). Then I want to be able to select as many items in the second column and push a button to add to the third column.

    So I don't see an onclick for a listbox. Are there any options to do something like this?

    here is my code:
    VB Code:
    1. <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" Trace="false" debug="true"%>
    2. <%@ import Namespace="System.Data.SqlClient" %>
    3. <%@ Import Namespace="System.Data" %>
    4. <%@ Import Namespace="System.IO" %>
    5.  
    6. <script runat="server">
    7.     Sub Page_Load
    8.     '-----------------------------------------------------------------------
    9.     '
    10.     '-----------------------------------------------------------------------
    11.         If Not IsPostBack Then
    12.             BindDataGrid("products_fullmodel")
    13.         End if
    14.     End Sub
    15.    
    16.     Sub BindDataGrid(sSortField as String)
    17.     '-----------------------------------------------------------------------
    18.     '
    19.     '-----------------------------------------------------------------------
    20.         Dim conPubs as SqlConnection
    21.         Dim cmdProducts as SqlCommand
    22.         Dim cmdProduct as SqlCommand
    23.         Dim dtrProducts as SqlDataReader
    24.         Dim dtrProduct as SqlDataReader
    25.            
    26.         'Retrieve records from database
    27.         conPubs = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("MS_SQL_CONN"))
    28.         cmdProducts = New SqlCommand("SELECT products_fullmodel, products_id FROM why3s_products ORDER BY " & sSortField, conPubs)
    29.        
    30.         cmdProduct = New SqlCommand("SELECT product_name, product_id FROM why3s_product ORDER BY product_name", conPubs)
    31.            
    32.         conPubs.Open()
    33.  
    34.         dtrProduct = cmdProduct.ExecuteReader()
    35.         lstProduct.DataSource = dtrProduct
    36.         lstProduct.DataTextField = "product_name"
    37.         lstProduct.DataValueField = "product_id"
    38.         lstProduct.DataBind()
    39.         dtrProduct.Close()
    40.            
    41.         dtrProducts = cmdProducts.ExecuteReader()
    42.         lstProducts.DataSource = dtrProducts
    43.         lstProducts.DataTextField = "products_fullmodel"
    44.         lstProducts.DataValueField = "products_id"
    45.         lstProducts.DataBind()
    46.         dtrProducts.Close()
    47.         conPubs.Close()
    48.     End Sub
    49.    
    50.     Sub sbLoadSelected(s As Object, e as EventArgs)
    51.     '-----------------------------------------------------------------------
    52.     '
    53.     '-----------------------------------------------------------------------
    54.         Dim conPubs as SqlConnection
    55.         Dim cmdSelected as SqlCommand
    56.         Dim dtrSelected as SqlDataReader
    57.  
    58.         'Retrieve records from database
    59.         conPubs = New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("MS_SQL_CONN"))
    60.         cmdSelected = New SqlCommand("SELECT products_fullmodel, products_id FROM why3s_products where product_id = " & lstProduct.SelectedValue & " ORDER BY products_fullmodel", conPubs)
    61.         lblProduct.Text = lstProduct.SelectedValue
    62.         lstSelected.Items.Clear()
    63.        
    64.         conPubs.Open()
    65.  
    66.         dtrSelected = cmdSelected.ExecuteReader()
    67.         'Bind to DropDownList
    68.         lstSelected.DataSource = dtrSelected
    69.         lstSelected.DataTextField = "products_fullmodel"
    70.         lstSelected.DataValueField = "products_id"
    71.         lstSelected.DataBind()
    72.         dtrSelected.Close()
    73.  
    74.         conPubs.Close()        
    75.     End Sub
    76.  
    77.     Sub sbShow(s As Object, e as EventArgs)
    78.     '-----------------------------------------------------------------------
    79.     '
    80.     '-----------------------------------------------------------------------
    81.         lblProduct.Text = lstProduct.SelectedValue
    82.     End Sub
    83.    
    84. </script>
    85.  
    86. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    87. <html>
    88. <head>
    89. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    90. <title>Untitled Document</title>
    91. </head>
    92. <body>
    93. <form id="frmAdd" name="frmAdd" action="addproduct.aspx" method="post" enctype="multipart/form-data" runat="server">
    94. <table>
    95.     <tr>
    96.      <td valign="top"><asp:ListBox ID="lstProduct" runat="server" Height="500" OnSelectedIndexChanged="sbLoadSelected"></asp:ListBox></td>
    97.      <td valign="top"><asp:ListBox ID="lstProducts" runat="server" SelectionMode="Multiple" Height="500"></asp:ListBox></td>
    98.      <td valign="top"><asp:ListBox ID="lstSelected" runat="server"></asp:ListBox></td></tr>
    99.     <tr>
    100.       <td><asp:Label ID="lblProduct" runat="server" /></td>
    101.       <td><asp:Button ID="Submit" CommandName="cmdShow" OnClick="sbShow" runat="server" /></td>
    102.       <td>&nbsp;</td>
    103.     </tr>
    104. </table>
    105. </form>
    106. </body>
    107. </html>

  2. #2
    Addicted Member
    Join Date
    Aug 2004
    Location
    Cape Town, South Africa
    Posts
    149

    Re: listbox onclick options?

    You need to add an AutoPostBack = True property to your listbox in your html. When the selectedindex changes, it will post back and run the event u have defined (sbLoadSelected). You can put the code in there...

    VB Code:
    1. Public Sub sbLoadSelected(ByVal sender As Object, ByVal e As System.EventArgs)
    2.     '...code here
    3. End Sub

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    Re: listbox onclick options?

    Thanks, this should work.

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