Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2003
    Location
    Illinois
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to get ListBox value with PreviousPage

    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.

  • #2
    New Coder
    Join Date
    Jan 2003
    Location
    Illinois
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile I solved it, if anyone else needs this:

    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •