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