nmachin
asked on
ASP.Net Dynamic BreadCrumb
I created a asp.net page that return subcategories based on the category id. I need to create a path on top of the page to look something like this: home >> cat3 >> SubCat4 >> SubSubCat2 >> product.
'cat3' needs to be a link with the Cat_ID ... <a href="mypage.aspx?CatID=3" >cat3</a>
'SubCat4' needs to be a link with the Cat_ID ... <a href="mypage.aspx?CatID=7" >SubCat4</ a>
etc...
This is dynamic so there is no set number to the category hierarchy.
Here's my table structure with some example data
Tbl_Category
Cat_ID Name Parent_ID
1 Cat1 0
2 Cat2 0
3 Cat3 0
4 SubCat1 2
5 SubCat2 2
6 SubCat3 2
7 SubCat4 3
8 SubSubCat1 7
9 SubSubCat2 7
Cat1, Cat2, & Cat3 are the parents with the ID=0
SubCat 1,2 & 3 belong to Cat2
SubCat 4 belongs to Cat3
SubSubCat1 & 2 belong to SubCat4
I've read through several articles dealing with breadcrumbs but the problem that I run into is my App does searches and jumps in several layers into the hierarchy. I need to show a path so the user can get back to that location in the future without doing searches. This path needs to be displayed in the datagrid along with the search results. The result set looks something similiar to Google or Yahoo.
example ...
the user does a search and gets to product in SubSubCat1 and SubCat2. I need the app to show this in the datagrid:
Search results
Product (blah blah)
home >> cat3 >> SubCat4 >> SubSubCat1 >> product
Product (blah blah)
home >> cat2 >> SubCat2 >> product
Also I need to keep track of the categories as the user goes through the layers.
Are there any examples of something like a Stored Procedure that shows how to do this?
Thanks
'cat3' needs to be a link with the Cat_ID ... <a href="mypage.aspx?CatID=3"
'SubCat4' needs to be a link with the Cat_ID ... <a href="mypage.aspx?CatID=7"
etc...
This is dynamic so there is no set number to the category hierarchy.
Here's my table structure with some example data
Tbl_Category
Cat_ID Name Parent_ID
1 Cat1 0
2 Cat2 0
3 Cat3 0
4 SubCat1 2
5 SubCat2 2
6 SubCat3 2
7 SubCat4 3
8 SubSubCat1 7
9 SubSubCat2 7
Cat1, Cat2, & Cat3 are the parents with the ID=0
SubCat 1,2 & 3 belong to Cat2
SubCat 4 belongs to Cat3
SubSubCat1 & 2 belong to SubCat4
I've read through several articles dealing with breadcrumbs but the problem that I run into is my App does searches and jumps in several layers into the hierarchy. I need to show a path so the user can get back to that location in the future without doing searches. This path needs to be displayed in the datagrid along with the search results. The result set looks something similiar to Google or Yahoo.
example ...
the user does a search and gets to product in SubSubCat1 and SubCat2. I need the app to show this in the datagrid:
Search results
Product (blah blah)
home >> cat3 >> SubCat4 >> SubSubCat1 >> product
Product (blah blah)
home >> cat2 >> SubCat2 >> product
Also I need to keep track of the categories as the user goes through the layers.
Are there any examples of something like a Stored Procedure that shows how to do this?
Thanks
ASKER
I came across a UDF and was able to modify it to build a string with links to all the sub categories through a loop.
This is the results I get when I run the UDF...
<a href="mypage.aspx?CatID=3" >Cat3</a> >> <a href="mypage.aspx?CatID=7" >SubCat4</ a> >> <a href="mypage.aspx?CatID=9" >SubSubCat 2</a>
CREATE FUNCTION dbo.GetParentCatPath(@Pare ntId int)
RETURNS VARCHAR(2000) AS
BEGIN
DECLARE @blnExit AS BIT,
@vchResult AS VARCHAR(2000),
@CatId AS INT,
@CatName AS VARCHAR(100),
@tmpCatName as varchar(100)
SET @blnExit=0
SET @vchResult=''
WHILE @blnExit=0
BEGIN
SELECT @ParentId=Tbl_Category.Par ent_ID,
@CatId=Tbl_Category.cat_ID ,
@CatName=Tbl_Category.Cate gory
FROM Tbl_Category
WHERE Tbl_Category.cat_ID=@Paren tId
IF @CatName IS NULL
SET @blnExit=1
ELSE
BEGIN
SET @tmpCatName = '<a href="mypage.aspx?CatID=' + Convert(varchar(6),@CatID) + '">' + RTRIM(LTRIM(@CatName)) + '</a>'
IF @vchResult<>''
SET @vchResult= @tmpCatName + ' '+ '>>' + ' ' + @vchResult
ELSE
SET @vchResult= @tmpCatName
IF @ParentId=0
SET @blnExit=1
END
END
RETURN @vchResult
END
How can something like this be implemented into a dataset which will be displayed on in a DataGrid.
This is the results I get when I run the UDF...
<a href="mypage.aspx?CatID=3"
CREATE FUNCTION dbo.GetParentCatPath(@Pare
RETURNS VARCHAR(2000) AS
BEGIN
DECLARE @blnExit AS BIT,
@vchResult AS VARCHAR(2000),
@CatId AS INT,
@CatName AS VARCHAR(100),
@tmpCatName as varchar(100)
SET @blnExit=0
SET @vchResult=''
WHILE @blnExit=0
BEGIN
SELECT @ParentId=Tbl_Category.Par
@CatId=Tbl_Category.cat_ID
@CatName=Tbl_Category.Cate
FROM Tbl_Category
WHERE Tbl_Category.cat_ID=@Paren
IF @CatName IS NULL
SET @blnExit=1
ELSE
BEGIN
SET @tmpCatName = '<a href="mypage.aspx?CatID=' + Convert(varchar(6),@CatID)
IF @vchResult<>''
SET @vchResult= @tmpCatName + ' '+ '>>' + ' ' + @vchResult
ELSE
SET @vchResult= @tmpCatName
IF @ParentId=0
SET @blnExit=1
END
END
RETURN @vchResult
END
How can something like this be implemented into a dataset which will be displayed on in a DataGrid.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER