Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 13th, 2006, 02:25 PM
Authorized User
 
Join Date: Aug 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default how do i get a handle on a listbox in page_load

I have a nested DropDownListBox inside of a DataList - see below.  I have autopostback enabled on the ddlb and on PageLoad I want to Response.Redirect("someOtherPage.aspx?cid=" & lbClasses.SelectedValue().ToString()).  Ever ything is working except the PageLoad keeps erring with "Object reference not set to an instance of an object" on the Response.Redirect line.  I assume its because I'm not getting a handle on the listbox in order to harvest the selectedValue.  Can anyone muddle through this message and code and advise, Please.


Code:
<asp:DataList ID="dlListName" runat="server" DataSourceID="sdsCategories" RepeatColumns="2" RepeatDirection="Horizontal">
    <ItemTemplate>
        <div class="categoryName"><asp:Label ID="itemDescriptionLabel" runat="server" Text='<%# Bind("classCategory") %>'></asp:Label><br />
            <asp:ListBox ID="lbClasses" runat="server" Rows="6" Width="228px" AutoPostBack="true"></asp:ListBox>Â </div>
    </ItemTemplate>
    <ItemStyle CssClass="itemStyle" />
    <AlternatingItemStyle CssClass="alternatingItemStyle" /></asp:DataList>
<asp:SqlDataSource ID="sdsCategories" runat="server" ConnectionString="<%$ ConnectionStrings:LcsWebData %>"
    SelectCommand="SELECT DISTINCT classCategory FROM usrtbl_Classes WHERE (classCategory <> N'NA') ORDER BY classCategory">
</asp:SqlDataSource>
Code:
    Protected Sub dlListName_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlListName.ItemDataBound

        Dim row As DataRowView

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            row = DirectCast(e.Item.DataItem, DataRowView)
            Dim conn As SqlConnection = Nothing
            Dim cmd As SqlCommand
            Dim ds As New DataSet()
            Dim parmCategoryName As New SqlParameter("@CategoryName", SqlDbType.NVarChar)

            conn = New SqlConnection(ConfigurationManager.ConnectionStrings("LcsWebData").ConnectionString)
            conn.Open()
            cmd = New SqlCommand()
            cmd.Connection = conn
            cmd.CommandText = "dbo.usp_getClassNamesPerClassCategory"
            cmd.CommandType = CommandType.StoredProcedure
            parmCategoryName.Direction = ParameterDirection.Input
            parmCategoryName.Value = row("classCategory").Trim()
            cmd.Parameters.Add(parmCategoryName)

            Dim ad As New SqlDataAdapter(cmd)

            ad.Fill(ds)

            lbClasses = DirectCast(e.Item.FindControl("lbClasses"), ListBox)
            lbClasses.DataSource = ds
            lbClasses.DataTextField = "className"
            lbClasses.DataValueField = "classId"
            lbClasses.DataBind()
            conn.Close()
        End If
    End Sub


Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack() Then
            Response.Redirect("class.aspx?cid=" & lbClasses.SelectedValue().ToString())
        End If
    End Sub
Code:
Public lbClasses As ListBox
 
Old June 14th, 2006, 01:12 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

You can't refence it that way since it is in a template column. You need to get a refernce to the object in the ItemDatabound event of the datalist

 
Old June 19th, 2006, 08:13 AM
Authorized User
 
Join Date: Aug 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I appreciate your response, however I can't find anything practical in it's content.

 
Quote:
quote:You need to get a refernce to the object in the ItemDatabound event of the datalist


I haven't a clue as to what you mean by this statement, can you be more specific?
 
Old June 19th, 2006, 09:20 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Datalist ItemDataBoundEvent:
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
           Dim lb as New ListBox
           lb = e.Item.FindControl("lbClasses")
           ''Do what you need to do here with the listbox.
        End If







Similar Threads
Thread Thread Starter Forum Replies Last Post
Page_Load question wayneamurphy BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 4 November 6th, 2006 03:54 AM
page_load() hassel vijayapteET ASP.NET 1.0 and 1.1 Basics 2 October 6th, 2005 01:30 PM
setting mult. selections in listbox at page_load jtyson General .NET 1 April 14th, 2004 07:39 AM
force Page_Load miguel.ossa ASP.NET 1.0 and 1.1 Basics 7 January 23rd, 2004 06:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.