List Box Selected Item

Converting VB app to ASP

Page 1 of 1

7 Replies - 10648 Views - Last Post: 07 January 2010 - 12:03 PM Rate Topic: -----

#1 ZRonZ  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 206
  • Joined: 09-January 09

List Box Selected Item

Posted 06 January 2010 - 08:08 AM

I have a working app in VB that I want to make as a web page for the company network that includes:

		If lbxSearchResults.Items.Count = 0 Then
			m_FileName = lbxFiles.SelectedItem
		Else
			m_FileName = lbxSearchResults.SelectedItem
		End If



I'm having trouble converting with the above segment of VB code to ASP. In ASP.NET it's giving me "Value of type 'System.Web.UI.WebControls.ListItem' cannot be converted to 'String'.

If I change it to

		If lbxSearchResults.Items.Count = 0 Then
			m_FileName = lbxFiles.SelectedItem.text
		Else
			m_FileName = lbxSearchResults.SelectedItem.text
		End If



I get a "NullReferenceException was unhandled by user code Object reference not set to an instance of an object"

Can anyone guide me to figure out the ASP.Net equivalent to the VB code?

Is This A Good Question/Topic? 0
  • +

Replies To: List Box Selected Item

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: List Box Selected Item

Posted 06 January 2010 - 09:08 AM

that should work fine.

This is an example from the MSDN:
http://msdn.microsof...y/z4d7ktzs.aspx

Sub SubmitBtn_Click(sender As Object, e As EventArgs)
		If ListBox1.SelectedIndex > - 1 Then
			Label1.Text = "You chose: " & ListBox1.SelectedItem.Text
		End If
End Sub


Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: List Box Selected Item

Posted 06 January 2010 - 09:10 AM

Are you certain that you have an item selected in either or both ListBoxes?

In order to access the SelectedItem property, an item must be selected in that ListBox. Otherwise you will get the NullReferenceException.
Was This Post Helpful? 0
  • +
  • -

#4 ZRonZ  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 206
  • Joined: 09-January 09

Re: List Box Selected Item

Posted 06 January 2010 - 09:35 AM

Here's the code as it stands now. I threw in the two msgboxes to see what what happening with item selection (duh, should have done that long ago, sorry). While an item has been clicked and is highlighted, both msgboxes come back with a selectedindex of -1. Is there some code that is required to recognize the selected/highlighted item?

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		lbxSearchResults.Items.Clear()
		lbxMSDSFiles.Items.Clear()
		GetDirectoryContents()
	End Sub

	Protected Sub GetDirectoryContents()
		Dim dDir As New DirectoryInfo(sDIR)
		Dim fFileSystemInfo As FileSystemInfo

		For Each fFileSystemInfo In dDir.GetFileSystemInfos
			lbxMSDSFiles.Items.Add(fFileSystemInfo.Name)
		Next
	End Sub

	Protected Sub btnSelectMSDS_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelectMSDS.Click
		MsgBox(lbxMSDSFiles.SelectedIndex)
		MsgBox(lbxSearchResults.SelectedIndex)
		If lbxSearchResults.Items.Count = 0 Then
			MsgBox(lbxMSDSFiles.SelectedItem.Text)
			m_FileName = lbxMSDSFiles.SelectedItem.Text
		Else
			m_FileName = lbxSearchResults.SelectedItem.Text
		End If

		Dim myFile As String = sDIR & m_FileName

		Dim fi = New FileInfo(myFile)

		Response.Clear()
		Response.ContentType = "application/pdf"
		Response.AddHeader("Content-Disposition", "inline; filename=" & fi.Name)
		Response.AddHeader("Content-Length", fi.Length.ToString())
		Response.WriteFile(fi.FullName)
		Response.End()

	End Sub



Thanks
Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: List Box Selected Item

Posted 06 January 2010 - 01:16 PM

The problem is in your Page_Load event. It is important to keep in mind that each time your page does a Postback, the code in the Page_Load event will run. And since you are clearing the selected item from each ListBox, nothing is selected when button click event is executed. The button event will run after the Page_Load event has executed.

To prevent it from resetting your values, check if the page is posting back, if it is then don't run the code in the Page_Load event.

Example:
	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			lbxSearchResults.Items.Clear()
			lbxMSDSFiles.Items.Clear()
			GetDirectoryContents()
		End If
	End Sub


Was This Post Helpful? 1
  • +
  • -

#6 ZRonZ  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 206
  • Joined: 09-January 09

Re: List Box Selected Item

Posted 06 January 2010 - 01:36 PM

View PostJayman, on 6 Jan, 2010 - 12:16 PM, said:

The problem is in your Page_Load event. It is important to keep in mind that each time your page does a Postback, the code in the Page_Load event will run. And since you are clearing the selected item from each ListBox, nothing is selected when button click event is executed. The button event will run after the Page_Load event has executed.

To prevent it from resetting your values, check if the page is posting back, if it is then don't run the code in the Page_Load event.


Thanks Jayman. I redid the page_load event code and now it's working - sort of. The original reason I was clearing the data from the listbox was that it was adding the data again each time I pulled up the .pdf file and came back to the default page. That interesting feature has now returned. I'll play with that and see what I can figure out.

One "last" question: Is there any way to force the .pdf to open in a new window?
Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: List Box Selected Item

Posted 07 January 2010 - 08:19 AM

You are going to need to use Javascript window.open to create a new window. Just pass the file name of the PDF via a session variable to the new page and then in the page load event put the code to write the file to the Response object.
Was This Post Helpful? 0
  • +
  • -

#8 ZRonZ  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 206
  • Joined: 09-January 09

Re: List Box Selected Item

Posted 07 January 2010 - 12:03 PM

View PostJayman, on 7 Jan, 2010 - 07:19 AM, said:

You are going to need to use Javascript window.open to create a new window. Just pass the file name of the PDF via a session variable to the new page and then in the page load event put the code to write the file to the Response object.


Thanks again Jayman.

I'll have to check into using Javascript when I get some time. I've never tried it, and I don't know anything about it. I did find a way to write a workaround. It's a pretty absurd way of doing it but the user won't know the difference and, in the end, I guess that's the important thing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1