Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListBox, Array and URL construction

Status
Not open for further replies.

runmd

Programmer
Aug 12, 2010
34
US
I'm throwing myself into ASP.NET with VB and there is no better way to learn it than by actually doing it. :) I am currently working on a project where I want to utilize a SQLDataSource control, a ListBox control, a Button control, an array and a constructed URL to post a page.

I'm writing a test page with the SQLDataSource object that populates a ListBox control. I also have a Button control and a second ListBox control. SQLDataSource control is pulling in data from oracle to populate the first ListBox. I would like to add the Selected Items to the second ListBox control once I click the button control. I am using an array to store the selected items which is the size of the number of items in the ListBox control. If my array is of size 20, which is the total number of values in the ListBox, and I select three items from the listbox. Once I click the button, 20 items are added to the 2nd control due to the size of the array. I need to find out how to skip over the array items that are empty so that I do not add empty items to the listbox control. My code is below.

Code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        '//Clear ListBox1 Items
        ListBox1.Items.Clear()

        '//ADD ITEMS INTO ARRAY 
        '//FIRST FIND HOW MANY ITEMS IN LISTBOX AND REDIM THE ARRAY FOR SIZE 
        Dim ArrayItems(countyListBox.Items.Count) As String

        For i = 0 To countyListBox.Items.Count - 1
            If countyListBox.Items(i).Selected Then
                ArrayItems(i) = countyListBox.Items(i).Value
            End If
        Next

        '//Add Items to ListBox1
        For i = 0 To countyListBox.Items.Count
            ListBox1.Items.Add(ArrayItems(i))
        Next i

    End Sub

It's fairly straightforward and I understand that I would need to add an IF block in the last For loop of this code block. I am finding out that ASP.NET doesn't like using an IF statement in a For loop. Any thoughts or suggestions on what I am trying to do would be helpful.
 
I am finding out that ASP.NET doesn't like using an IF statement in a For loop.
That is a language syntax issue not an ASP.NET issue. Anyway, what do you mean by that? You can certainly put an IF within a FOR loop.
 
Well, I misspoke on that statement. I have been able to figure out that portion of the syntax in the last few minutes. Here it is and it was easy once I did some google searches to find examples on how to handle empty array items.

Code:
 '//Add Items to ListBox1
        For i = 0 To countyListBox.Items.Count
            '// Check is there any elements in ArrayList
            If ArrayItems(i) = "" Then
                ' ArrayItems(i) has no value
            Else
                ' In this case, ArrayItems(i) has a value
                ListBox1.Items.Add(ArrayItems(i))
            End If
        Next i

So, now on to constructing a URL with parameters created from the array values.
 
I have an array of values from selected from a ListBox that I would like to pass to another page in a session variable using response.redirect. The 2nd page stores the session variables into another array list to display in a list box control. I am getting this error when using the DataBind() command:

Object reference not set to an instance of an object.

How do I fix this problem? I would like to verify that the data is in the array and also iterate through it to write a query.

Code from page1.aspx
Code:
Partial Class ListBox
    Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        '//ADD ITEMS INTO ARRAY 
        '//FIRST FIND HOW MANY ITEMS IN LISTBOX AND REDIM THE ARRAY FOR SIZE 
        Dim ArrayItems(countyListBox.Items.Count) As String

        For i = 0 To countyListBox.Items.Count - 1
            If countyListBox.Items(i).Selected Then
                ArrayItems(i) = countyListBox.Items(i).Value
            End If
        Next

        Session("countyList") = ArrayItems
        Response.Redirect("Post.aspx")

    End Sub
End Class

Code from page2.aspx

Code:
Partial Class Post
    Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim ArrayItems() As String
        ArrayItems = Session("countyList")

        lstCounties.DataSource = ArrayItems

        'Bind DataSource to lstCounties ListBox control
        lstCounties.DataBind()

    End Sub
End Class
 
What line does the error occur on? Did you trace through the code?
 
Yes, lstCounties.DataSource is set with the ArrayItems array. I can see the size of the array and the values of the items. The error occurs on the lstCounties.DataBind() call giving me the "Object reference not set to an instance of an object."

I'm wondering since a ListBox has Text and Value properties and if it's not being set correctly. If this helps, I used this sample code as a test and it does use a slightly different array.

Dim ArrayItems(countyListBox.Items.Count) As String
vs.
Dim ArrayItems As New ArrayList



Default.aspx page
Code:
Partial Class _Default
    Inherits System.Web.UI.Page

    Dim ArrayItems As New ArrayList

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnWelcome.Click

        ArrayItems.Add("Windows Forms")
        ArrayItems.Add("Web Forms")
        ArrayItems.Add("WCF")

        Session("Techs") = ArrayItems
        Response.Redirect("Welcome.aspx")

    End Sub
End Class

Welcome.aspx page
Code:
Partial Class Welcome
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim ArrayItems As New ArrayList
        ArrayItems = Session("Techs")
        lstTech.DataSource = ArrayItems
        DataBind()

    End Sub
End Class
 
I don't believe you can directly bind an array to a control. However since an ArrayList Impliments a number of Inerfaces it should be able to be directly bound to a control
 
arrays implement IEnumerable; therefore you can bind to any enumerable control (grid, list, form, repeater, dropdown, etc).

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
What if I want to use a two-dimensional array to pass the listbox values, one for the value of the list box and the other for the text of the list box. That should be doable. i wanted to display the text on the next page and use the value to generate the where clause in a query. i would think it's two for loops, one inside the other. Assign the value in one for loop and the text in the 2nd for loop.

I am having one annoying problem, the button I have placed on my aspx form only works in FF and Chrome. IE gives me a this:

Code:
Error: btnSubmit_Click is undefined error.

I have taken out my master page and used form tags and the submit button works in IE. I can only assume it's something in the master page that is preventing the submit button to fire. The funny thing is, FF sees the error as well but fires the button anyways.

Button (nothing special here)
Code:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="btnSubmit_Click" />

Button event code (Straightforward stuff)
Code:
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

        '//ADD ITEMS INTO ARRAY 
        '//FIRST FIND HOW MANY ITEMS IN LISTBOX AND REDIM THE ARRAY FOR SIZE 
        Dim ArrayItems(countyListBox.Items.Count) As String

        For i = 0 To countyListBox.Items.Count - 1
            If countyListBox.Items(i).Selected Then
                ArrayItems(i) = countyListBox.Items(i).Value
            End If
        Next

        '// Wrap ArrayItems in a Session
        Session("countyList") = ArrayItems
        '// Send ArrayItems to state name to CountryQuery.aspx
        Response.Redirect("countyQuery.aspx?state=" & lblState.Text)

    End Sub

Is there anything that stands out as a problem?
 
OnClientClick runs on the client (javascript). btnSubmit_Click is asp.net code defined on the server. if you want to wire the server side click event to the server side hanlder btnSubmit_Click it would look like this
Code:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
or
Code:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" Click="btnSubmit_Click" />
one of the two anyway.
What if I want to use a two-dimensional array to pass the listbox values
you will be better off defining a POCO (plain old compiled object) or using a ListItem to map your value and text to the control. a 2D array is a very low level construct that won't provide much value in this context.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
jmeckley,

If I try "Click" or "OnClick", it is still a no go in IE. FF and Chrome have no problems with those events as well as "OnClientClick". I'm not sure if you missed it but I use a master page to make sure that all of my pages have the same look and feel. When I take out the master page, the submit button works. I've been looking at the master page and it's included css files but I don't see anything that would prevent the button from firing. What I do see are some CSS validation issues. Maybe this is causing a problem. I'm looking into it and will take those out.
 
css controls the styling of the page, it has no impact on the functionality of the page. master pages (nested controls in general) alter the client ID of server controls. if you are using javascript to get a control by id that could be breaking.

another thing to check is all your tags (both html and server) are properly closed

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I'm checking the tags now and nothing stands out as a problem. This is why I think it's the master page since the button fires when the master page is taken out. If I use the master page, this is what the header looks like:

Code:
<%@ Page Language="VB" MasterPageFile="~/HUD.master" AutoEventWireup="false" CodeFile="Query.aspx.vb" Inherits="Query" title="TDAT Version 2.0 Query Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<!--  PAGE CODE -->

</asp:Content>

This is what it looks like without the master page.
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Query.aspx.vb" Inherits="Query" title="TDAT Version 2.0 Query Page" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head runat="server">
    <title>Select Multiple Values in List Box</title>
</head>
<body>
    <form id="form1" runat="server">

<!-- PAGE CODE -->

    </form>
</body>
</html>

Do I need additional tags in the master page version?
 
then chances are the problem is in the master page.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I found a small Search form in the master page. I knew it was there but more research revealed that the small embedded form was preventing the submit button from working. All is good now.

Now on to a multi-dimensional array to pass the value and text from each item selected the ListBox control in a session variable. I passing the value now but it should be easy to pass a two-d array. I'd like to use some of the array (values) to create a SQL statement and the text part to use as a label above the GridView control.
 
So, I know I have the pieces to create a 2-d array but I am not populating it correctly. How do I make sure that I assign the correct values to the "i" and "j" positions?

Code:
 Dim ArrayItems(countyListBox.Items.Count, countyListBox.Items.Count) As String

        For i = 0 To countyListBox.Items.Count - 1
            For j = 0 To countyListBox.Items.Count - 1
                If countyListBox.Items(i).Selected Then
                    ArrayItems(i, j) = countyListBox.Items(i).Value
                    ArrayItems(i, j) = countyListBox.Items(j).Text
                End If
            Next
        Next
 
why use a multidimensional array, it's such a low level construct?

either use ListItem objects or define your own POCOs (plain old compiled object). bind the control to the collection and set the appropriate properties for the text and value of the drop down.

this will be much easier to understand and maintain moving forward.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top