...

View Full Version : How to get ListBox value with PreviousPage



jim_denney
09-04-2010, 09:46 PM
Does anyone know how to get the selected value from a listbox using PreviousPage using VB? I have tried every option I can find, but nothing works. My code is:

-- First Page --
[CODE]
<%@ Page Title="" Language="VB" MasterPageFile="~/CBAMaster.master" AutoEventWireup="false" CodeFile="Identify_mbr.aspx.vb" Inherits="Identify_mbr" Debug="true"%>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CBA_EducationConString %>"
SelectCommand="SELECT [NAME], [ID] FROM [Members] ORDER BY [NAME]">
</asp:SqlDataSource>

<div style="width: 300px; top: 100; position:absolute ">
<asp:ListBox ID="ListBox1"
runat="server"
DataSourceID="SqlDataSource1"
DataTextField="NAME"
DataValueField="ID"
Width="278px">
</asp:ListBox>
</div>

<br />
Select your agency name and click the sumbit button to proceed.<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="Submit"
Height="26px"
Width="80px"
PostBackUrl="GetMbrInfo.aspx" />
</div>
</asp:Content>
[CODE]

-- Second Page PageLoad --
[CODE]
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
txtCity.Text = CType(PreviousPage.FindControl("ListBox1"), ListBox).SelectedValue
End Sub
[CODE]

All this gives is a "Object reference not set to an instance of an object." error. Any help will be appreciated.

jim_denney
09-05-2010, 03:26 AM
It turns out I had to reference the ContentPlaceHolder before I could get to the value of the ListBox. MS sure has implemented a funky way to do this, the old "Request.Form" was a lot easier to deal with. In this application we have to go through several screens to gather data for several tables before we can finally sign a registrant up for a course and collect payment, so I have to post to different URLs a few more times. The fun never stops.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim MemCode As String
Dim cph As ContentPlaceHolder = PreviousPage.Master.FindControl("ContentPlaceHolder1")
If cph IsNot Nothing Then
Dim LstMbr As ListBox = cph.FindControl("ListBox1")
If LstMbr.SelectedValue IsNot Nothing Then
MemCode = LstMbr.SelectedValue
Response.Write(MemCode)
End If
End If
End Sub



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum