L
Laura K
I would like to create a nested datalist inside the
selecteditemtemplate of a datalist. My hope is that when a user clicks
on a link a set of child links is displayed. For example if the user
clicks men's clothing he would get a list of sub categories like pants,
shirts, shorts etc
I have put together the following and I get the original datalist fine.
When I try to get the sublist I get the following error: Object
reference not set to an instance of an object.
The offending line is
brandSubList.DataSource = Catalog.GetSubCatsInBrands(BrandID)
from the code behind listed below.
---------------------------------
HTML code looks like this:
<asp:datalist id="brandList" Runat="server">
<ItemTemplate>
<DIV class="navcells"><B>
<asp:hyperlink id="brandLink" Runat="server" NavigateUrl= '<%#
"../default.aspx?brandID=" & databinder.eval(container.dataitem,
"strBrandCode") %>' text= '<%# DataBinder.Eval( Container.DataItem,
"strBrandName" ) %>' >
</asp:hyperlink></B></DIV>
</ItemTemplate>
<SelectedItemTemplate>
<asp
ataList id="brandSubList" Runat="server">
<ItemTemplate><DIV class="navcells"><B>
<asp:LinkButton id="brandsubLink" Runat="server"
CommandName="select"><%# DataBinder.Eval( Container.DataItem,
"strSubCategory" ) %>
</asp:LinkButton></B></DIV>
</ItemTemplate>
</asp
ataList>
</SelectedItemTemplate>
</asp:datalist>
---------------------------------------------------------
My code behind looks like this:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim BrandID As String = Request.QueryString("brandID")
brandList.DataSource = Catalog.GetBrands()
brandList.DataBind()
If Not BrandID Is Nothing Then
Dim BrandSublist As DataList
brandSubList.DataSource =
Catalog.GetSubCatsInBrands(BrandID)
brandSubList.DataBind()
End If
--------------------------------------------------------
Data is pulled from a business objects doc like this
Public Shared Function GetBrands() As SqlDataReader
'Create connetion objedt
Dim Connection As New SqlConnection(connectionString)
'Create and initialize the command Object
Dim command As New SqlCommand("New_GetBrands", Connection)
command.CommandType = CommandType.StoredProcedure
'Open the connection
Connection.Open()
'Return a SqlDatatreader
Return command.ExecuteReader(CommandBehavior.CloseConnection)
End Function
Public Shared Function GetSubCatsInBrands(ByVal brandID As String) As
SqlDataReader
'create the connection string
Dim connection As New SqlConnection(connectionString)
'Create and initialize the command Object
Dim command As New SqlCommand("New_getSubCatsInBrands",
connection)
command.CommandType = CommandType.StoredProcedure
'Add an input parameter and suply a valiue for it
command.Parameters.Add("@brandID", SqlDbType.NVarChar, 5)
command.Parameters("@brandID").Value = brandID
'Open the connection, exectue the command, and clost the
connection
Try
connection.Open()
command.ExecuteNonQuery()
Finally
connection.Close()
End Try
selecteditemtemplate of a datalist. My hope is that when a user clicks
on a link a set of child links is displayed. For example if the user
clicks men's clothing he would get a list of sub categories like pants,
shirts, shorts etc
I have put together the following and I get the original datalist fine.
When I try to get the sublist I get the following error: Object
reference not set to an instance of an object.
The offending line is
brandSubList.DataSource = Catalog.GetSubCatsInBrands(BrandID)
from the code behind listed below.
---------------------------------
HTML code looks like this:
<asp:datalist id="brandList" Runat="server">
<ItemTemplate>
<DIV class="navcells"><B>
<asp:hyperlink id="brandLink" Runat="server" NavigateUrl= '<%#
"../default.aspx?brandID=" & databinder.eval(container.dataitem,
"strBrandCode") %>' text= '<%# DataBinder.Eval( Container.DataItem,
"strBrandName" ) %>' >
</asp:hyperlink></B></DIV>
</ItemTemplate>
<SelectedItemTemplate>
<asp
<ItemTemplate><DIV class="navcells"><B>
<asp:LinkButton id="brandsubLink" Runat="server"
CommandName="select"><%# DataBinder.Eval( Container.DataItem,
"strSubCategory" ) %>
</asp:LinkButton></B></DIV>
</ItemTemplate>
</asp
</SelectedItemTemplate>
</asp:datalist>
---------------------------------------------------------
My code behind looks like this:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim BrandID As String = Request.QueryString("brandID")
brandList.DataSource = Catalog.GetBrands()
brandList.DataBind()
If Not BrandID Is Nothing Then
Dim BrandSublist As DataList
brandSubList.DataSource =
Catalog.GetSubCatsInBrands(BrandID)
brandSubList.DataBind()
End If
--------------------------------------------------------
Data is pulled from a business objects doc like this
Public Shared Function GetBrands() As SqlDataReader
'Create connetion objedt
Dim Connection As New SqlConnection(connectionString)
'Create and initialize the command Object
Dim command As New SqlCommand("New_GetBrands", Connection)
command.CommandType = CommandType.StoredProcedure
'Open the connection
Connection.Open()
'Return a SqlDatatreader
Return command.ExecuteReader(CommandBehavior.CloseConnection)
End Function
Public Shared Function GetSubCatsInBrands(ByVal brandID As String) As
SqlDataReader
'create the connection string
Dim connection As New SqlConnection(connectionString)
'Create and initialize the command Object
Dim command As New SqlCommand("New_getSubCatsInBrands",
connection)
command.CommandType = CommandType.StoredProcedure
'Add an input parameter and suply a valiue for it
command.Parameters.Add("@brandID", SqlDbType.NVarChar, 5)
command.Parameters("@brandID").Value = brandID
'Open the connection, exectue the command, and clost the
connection
Try
connection.Open()
command.ExecuteNonQuery()
Finally
connection.Close()
End Try