CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2007
    Posts
    210

    page not picking up listbox items

    Hi all,

    hopefully this is easy.

    I have two listboxes on my pages, one of which is populated with a list of types from a database. The second listbox next to it contains will contain all those associated with the product i am entering.

    using javascript i move the items between the tables. this is all working.

    now when i click the add button (this will iterate through the added types in the second list box and add the associations to the database). it is saying the number of items in the list is 0? can anyone explain why please?

    i though it may be something to do with the postback but his is protected.

    Code:
    <%@ Page Language="vb" AutoEventWireup="true" CodeFile="add_product.aspx.vb" Theme="Admin" Inherits="add_product" debug="true"%>
    <!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 id="Head1" runat="server">
    		<title>Add a New Product</title>
    ...
    				<tr>
    					<td>
    						<asp:listbox CssClass="stdListBox" id="availableTypes" runat="server"/>
    						<asp:listbox CssClass="stdListBox" id="productTypes" runat="server"/>
    					</td>
    				</tr>
    ...
    				<tr>
    					<td align="center" colspan=2><asp:button id="nextPage" text="Next >>" OnCommand="AddProductClick" runat="server"/> </td>
    				</tr>
    Code:
        Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    
    	'*************** AUTH STUFF *************************************
    	Dim MM_authFailedURL As String = "..\admin_access_denied.aspx"
    	Dim MM_grantAccess As Boolean = false
    
    	If Session("MM_Username") <> "" Then
       		MM_grantAccess = true
    	End If
    	
    	If Not MM_grantAccess Then
    		Response.Redirect(MM_authFailedURL)
    	End If
    	'*****************************************************************
    
    	If Not Page.IsPostBack Then
    
    	Dim dt As DataTable
    	
    	oQry = "SELECT supplierID, name FROM suppliers ORDER BY name"
            SupplierDropDown.DataSource = CreateDataSource(oQry)
    	SupplierDropDown.DataTextField = "name"
    	SupplierDropDown.DataValueField = "supplierID"
            SupplierDropDown.DataBind()
    
    	'initialize the image datagrid
    	dt = new DataTable("ProductImages")
    	dt.Columns.Add(new DataColumn("imageID", GetType(Integer)))
    	dt.Columns.Add(new DataColumn("image", GetType(String)))
    	dt.NewRow()
    
    	If Not (Request.QueryString("ID")Is Nothing) Then
    		
    		productID = Request.QueryString("ID")
    		
    		'set supplier
    		SupplierDropDown.SelectedItem.Value = productID
    
    		'now do lookup for rest of information
    		oQry = "SELECT * FROM products WHERE productID=" + productID
    		Dim pDataSet As DataSet = CreateDataSource(oQry)
    
    		productName.Text = pDataSet.Tables(0).Rows(0).Item("name")
    		briefDescription.Text = pDataSet.Tables(0).Rows(0).Item("brief_des")
    		detailedDescription.Text = pDataSet.Tables(0).Rows(0).Item("full_des")
    		labelInfo.Text = pDataSet.Tables(0).Rows(0).Item("label")
    		directions.Text = pDataSet.Tables(0).Rows(0).Item("directions")
    
    		'now select all sizes not associated
    		oQry = "SELECT * FROM sizes WHERE sizeID NOT IN (SELECT sizeID from productSize WHERE productID=" + productID + ") ORDER BY sizes.size"
    		pDataSet = CreateDataSource(oQry)
    		availableSizes.DataSource = pDataSet
    		availableSizes.DataTextField = "size"
    		availableSizes.DataValueField = "sizeID"
    	        availableSizes.DataBind()
    
    		'and select all those which are associated
    		oQry = "SELECT * FROM sizes,productSize WHERE sizes.sizeID = productSize.sizeID AND productSize.productID=" + productID + " ORDER BY size"
    		pDataSet = CreateDataSource(oQry)
    		productSizes.DataSource = pDataSet
    		productSizes.DataTextField = "size"
    		productSizes.DataValueField = "sizeID"
    	        productSizes.DataBind()
    
    		'do the same for all the types
    		oQry = "SELECT * FROM types WHERE typeID NOT IN (SELECT typeID from productType WHERE productID=" & productID & ") ORDER BY types.type"
    		pDataSet = CreateDataSource(oQry)
    		availableTypes.DataSource = pDataSet
    		availableTypes.DataTextField = "type"
    		availableTypes.DataValueField = "typeID"
    	        availableTypes.DataBind()
    
    		oQry = "SELECT * FROM types,productType WHERE types.typeID = productType.typeID AND productType.productID=" & productID & " ORDER BY type"
    		pDataSet = CreateDataSource(oQry)
    		productTypes.DataSource = pDataSet
    		productTypes.DataTextField = "type"
    		productTypes.DataValueField = "typeID"
    	        productTypes.DataBind()
    
    		'now check for images
    		oQry = "SELECT * FROM ((images INNER JOIN productImages ON images.imageID = productImages.imageID) INNER JOIN products ON products.productID = productImages.productID) WHERE products.productID=" + productID
    		'response.write(oQry)
    		pDataSet = CreateDataSource(oQry)
    
    		Dim i As Integer = 0 
    		For i = 0 To pDataSet.Tables(0).Rows.Count-1
    			Dim newImageRow As DataRow = dt.NewRow()
    
    			newImageRow("imageID") = pDataSet.Tables(0).Rows(i).Item("imageID")
    			newImageRow("image") = pDataSet.Tables(0).rows(i).Item("location")
    		Next		
    	Else
    		'select all sizes
    		oQry = "SELECT * FROM sizes ORDER BY size"
    		Dim pDataSet As DataSet = CreateDataSource(oQry)
    		availableSizes.DataSource = pDataSet
    		availableSizes.DataTextField = "size"
    		availableSizes.DataValueField = "sizeID"
    	        availableSizes.DataBind()
    
    		'select all types
    		oQry = "SELECT * FROM types ORDER BY type"
    		pDataSet = CreateDataSource(oQry)
    		availableTypes.DataSource = pDataSet
    		availableTypes.DataTextField = "type"
    		availableTypes.DataValueField = "typeID"
    	        availableTypes.DataBind()
    	End If	
    
    	'now databind datagrid
    	imageDatagrid.DataSource = dt
    	imageDatagrid.DataBind()
    
    	Session("imagesDataTable")=dt
    
    	End If
    	
    	Response.Write("always getting to here?? <br>")
    
        End Sub
    Last edited by flynny1st; April 23rd, 2008 at 06:39 AM. Reason: oops didnt add code correctly

  2. #2
    Join Date
    Mar 2008
    Posts
    72

    Re: page not picking up listbox items

    I didn't read your code, but it probably has to do with viewstate. Javascript doesn't add the items to the viewstate, so when you post back, the control still thinks it's empty.

    There's probably a better way to get around it, but what I've done in the past is build a comma delimited string in a hidden textbox (using javascript) before I postback, then split it in the server-side code.

  3. #3
    Join Date
    Mar 2007
    Posts
    210

    Re: page not picking up listbox items

    Hi Mike,

    thanks for the reply, i see.

    so what you are saying is i would have a hidden label and on the onclick javascript add method append to the string and on the remove remove from the string?

    would this not have the same problem as before with it not being seeing the labels string?

    (sorry a newb as you can tell!)

  4. #4
    Join Date
    Mar 2008
    Posts
    72

    Re: page not picking up listbox items

    Make it a hidden textbox, not a label. This is not an asp:TextBox Visible=false - this is an asp:TextBox with a style="visibility:hidden;".

    Textboxes and Listboxes deal with viewstate differently, so you will get the value of the textbox on postback.

    I'd build the comma delimited list in the javascript onSubmit handler, rather than adding and removing and dealing with the complication that entails.

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

Featured