Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
chriskhan2000
Aged Yak Warrior
544 Posts |
Posted - 2005-06-17 : 14:24:10
|
I have a listbox where users can select multiple items from that listbox and then it loops and store those items into an array. After it executes, it does a databind, and I want those items to stay selected. Somehow when I try to handle those items from the ArrayList, it gives me an error saying "Object reference not set to an instance of an object". I've run out of ideas....can someone see if they can see anything wrong with a section of my code.Dim newAry As ArrayList = CType(Session("territoryHolder"), ArrayList)'loops and highlight listbox base on arraylist.Dim X As IntegerDim strDataHolder As String For X = 0 To newAry.Count - 1 strDataHolder = newAry(X) Me.dlTerritory.SelectedItem.Value = strDataHolder Next |
|
jhermiz
3564 Posts |
Posted - 2005-06-17 : 15:10:23
|
Hmm can you kind of explain a bit more on what you need, im sort of confused what you are looking for?Are you just looking to rehighlight the list box items ? If so you cannot do it in that method there, you must set the SelectedItem of each index to true / false, not the value of the string.Jon Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]Imperfection living for perfection -- [url]http://jhermiz.blogspot.com/[/url] |
 |
|
chriskhan2000
Aged Yak Warrior
544 Posts |
Posted - 2005-06-20 : 12:19:16
|
Sorry. Let me try to explain it a little better.I have a listbox where it allows the user to select multiple items and use those items as parameters with my SP. After the user selects the item and executes the application, it will go through a loop and store the selected item into an arraylist and then stores into session so that it can be recall later. Dim count, D As Int32 Dim item As String count = Me.dlTerritory.Items.Count Dim ary As New ArrayList For D = 0 To count - 1 If Me.dlTerritory.Items(D).Selected = True Then item = Me.dlTerritory.Items(D).Value If strFilter = "status in (" & statusFilter & ")" Then strFilter += " and territory = '" & item & "'" Else strFilter += " or territory = '" & item & "'" End If ary.Add(item) End If Next Session.Add("territoryHolder", ary) Now it goes into a databind to pull all the items back into the listbox, in which I want the items from the listbox to be selected as what was stored in my session. Dim X As Integer Dim strDataHolder As String For X = 0 To newAry.Count - 1 strDataHolder = newAry(X) Me.dlTerritory.SelectedItem.Value = strDataHolder Next So example: Items selected before from listbox are (Apple and Pears)ListBox-------AppleBananaPeachPearsI want (Apple and Pears) to stay selected.At this point it does not highlight the items. Which is very annoying for the user in that they have to reselect those items from the listbox each time they change date or different parameters for the search.I know my last code example has a fluke in there that I can't seem to see. Base on what you recommend, do I just set it to my arraylist and if true then reselect else not?Kind of like this?Dim X As IntegerDim strDataHolder As StringFor X = 0 To newAry.Count - 1 If newAry.Item(X).Value = True Then Me.dlTerritory.SelectedIndex = X End IfNext |
 |
|
jhermiz
3564 Posts |
Posted - 2005-06-20 : 23:00:49
|
Hi chris,Just so I am on the same page...you select some items from a list box and store them into an array.Later on you need to reuse this array so you store it as a session variable...Fine and dandy...Later on the user is presented with the same listbox and you want whatever is stored in your session array to be selected in this listbox?Is my assumption correct? If so this should not be a problem, and I should be able to help you tomorrow morning (its 11pm plus no vs.net, so I hate giving out buggy code).Let me know...Jon Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]Imperfection living for perfection -- [url]http://jhermiz.blogspot.com/[/url] |
 |
|
jhermiz
3564 Posts |
Posted - 2005-06-21 : 08:29:44
|
Here we go, I put some comments so you understand it.Public Class WebForm1 Inherits System.Web.UI.Page#Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Protected WithEvents ListBox1 As System.Web.UI.WebControls.ListBox Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents Button2 As System.Web.UI.WebControls.Button 'NOTE: The following placeholder declaration is required by the Web Form Designer. 'Do not delete or move it. Private designerPlaceholderDeclaration As System.Object Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub#End Region 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 IsPostBack Then Me.ListBox1.Items.Add("apples") Me.ListBox1.Items.Add("oranges") Me.ListBox1.Items.Add("bananas") End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a As ArrayList Dim i As Integer 'allocate memory dynamically a = New ArrayList For i = 0 To Me.ListBox1.Items.Count - 1 'was it selected? If Me.ListBox1.Items(i).Selected = True Then 'sure was, so add it... a.Add(Me.ListBox1.Items(i).Text) End If Next 'now check what was selected via the array list WhatWasClicked(a) End Sub Private Sub WhatWasClicked(ByVal a As ArrayList) Dim i As Integer Dim j As Integer '2 loops one for the array, 'second one for the listbox 'loop for the array For i = 0 To a.Count - 1 'loop for the listbox For j = 0 To Me.ListBox1.Items.Count - 1 'the array has the value selected If a(i) = Me.ListBox1.Items(j).Text Then Me.ListBox1.Items(j).Selected = True 'break out no need to reloop Exit For End If Next Next End Sub End Class Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]Imperfection living for perfection -- [url]http://jhermiz.blogspot.com/[/url] |
 |
|
chriskhan2000
Aged Yak Warrior
544 Posts |
Posted - 2005-06-21 : 11:36:39
|
Jon,Your assumption is correct. The user picks from the listbox and then it stores it into an array which stores it into a session to be use later on because the parameter must stay in position(selected). So I need to store it into an array, save it into session, and then handle and highlight what was stored in session back into the listbox.Thanks for the detail example. I will look into it and let you know if I have any questions later on.Thanks,Chris |
 |
|
jhermiz
3564 Posts |
Posted - 2005-06-21 : 11:45:11
|
Ok that code should do exactly what you want.Cheers,Jon Keeping the web experience alive -- [url]http://www.web-impulse.com[/url]Imperfection living for perfection -- [url]http://jhermiz.blogspot.com/[/url] |
 |
|
|
|
|
|
|