Finding a tab's parent by level
This is a simple but useful user defined function I have implemented in order to recursively find a tab's parent on a specific level. Syntax is as follows:
select dbo.udfGetParentByLevel (level, tabid)
where level is the level you need (0,1,2 etc.) and tabid is the id of the tab for which you need to find the parent.
Nothing much, but can save you in certain scenarios.
CREATE FUNCTION udfGetParentTabByLevel
(
-- Add the parameters for the function here
@parentlevel int, @initialtabid int
)
RETURNS int
AS
BEGIN
declare @level int
declare @tabid int
declare @parentid int
select @level = level, @tabid=tabid, @parentid=parentid from tabs where tabid=@initialtabid
while @level > @parentlevel
begin
select @level = level, @tabid=tabid, @parentid=parentid from tabs where tabid=@parentid
end
-- Return the result of the function
RETURN @tabid
END
GO
(
-- Add the parameters for the function here
@parentlevel int, @initialtabid int
)
RETURNS int
AS
BEGIN
declare @level int
declare @tabid int
declare @parentid int
select @level = level, @tabid=tabid, @parentid=parentid from tabs where tabid=@initialtabid
while @level > @parentlevel
begin
select @level = level, @tabid=tabid, @parentid=parentid from tabs where tabid=@parentid
end
-- Return the result of the function
RETURN @tabid
END
GO
4 comments:
I've written a similar function in VB.NET that does the same thing based on active tab. I use it to display the top ancestor name on all of its' child pages, and to add a class to the navigation menu to keep the top ancestors' button "lit up"
http://www.joesak.com/2007/09/13/three-scripts-i-wrote-for-dotnetnuke-skins/
Thank you for letting us know, these are simple but very useful scripts, I'll be sure to include them in my skins too!
Here is another method of finding the TabID of every ancestor of an active Tab. It uses recursion to create an Array of TabIDs and Javascript, JQuery and CSS to style the top-level navigation element.
http://webtipstoremember.blogspot.com/2010/03/how-to-get-tabid-of-every-ancestor-of.html
Goodd reading this post
Post a Comment