E:\web\learnaspcom\htdocs\freebook\learn\ubtoc.xml LearnAsp.com - ASP ASP.net Free Lessons
Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll
<Asp.net blog>
<personal site>
xxx

Utility Belt: Inheritance Example
by Charles M. Carroll

To modify or extend UtilityBelt.vb the best way is to inherit the LearnAsp.UtilityBelt object. This has several advantages. One advantage is that since you never modified the UtilityBelt.vb you got off the website you don't need to sync your changes with ours. Plus if you like UtilityBelt but dislikue the way we implemented some of it you can replace any of our methods with your own.

Here is the .aspx page callig the inherited Utility Belt    filename=/experiments/utilitybelt/vernext/charlescarroll/utilitybeltdemo_toc_learn.aspx

<Test Script Below>


<%@trace="false" debug="false" %>
<%@ Import Namespace="Chaz"%>
<%@ Assembly src="utilitybeltchaz.vb" %>
<script language="VB" runat="server">
    dim ub1 as new chaz.utilitybelt()
    dim strConnect as string
Sub Page_Load(S As Object, E As EventArgs)
    TRY
        trace.tracemode=TraceMode.SortByTime
        ub1.plcException=plcErr
        
        dim ht1 as new hashtable
        ht1.add("group","section") ' assumes a title and default img attribute
        ht1.add("weblocation","url")
        ht1.add("webname","name")
        ht1.add("groupimage","sectionIMG")
        plcTOC.controls.add(new literalcontrol(ub1.TOC("/freebook/learn/ubtoc.xml",ht1)))
    CATCH exc as exception
            ub1.ExceptionReportWithDetail("SUB Page_Load",exc,"no more details from me")
    END TRY
End Sub
</script>
<html><head>
<title></title>
</head>
<body bgcolor="#FFFFFF">
<asp:placeholder id="plcErr" runat="server"/>
<asp:placeholder id="plcTOC" runat="server"/>
<form runat="server">
</form>
</body></html>

Here is .vb code that contains the inherited UtilityBelt:

filename=/experiments/utilitybelt/vernext/charlescarroll/utilitybeltchaz.vb

option strict off
imports system
imports system.collections
imports system.data
imports system.xml
imports system.text
Imports system.xml.xmlnodetype
NameSpace Chaz
    Public Class UtilityBelt
        Inherits LearnAsp.UtilityBeltdll
        Private Server as  System.Web.HttpServerUtility=System.Web.HttpContext.Current.Server
        Private Trace as  System.Web.TraceContext=System.Web.HttpContext.Current.Trace
        
    Public sub New()
            MyBase.New()
        End sub
    
FUNCTION Toc(strParmFilename as string,htXMLMap as hashtable) as string
    DIM strTask as string="SUB releaseStreamReader"
    DIM Xmltr1 as XMLTextReader
    TRY    
        LogTaskStart(strTask)
        DIM strParms as string=";weblocation;webname;group;groupmatch;groupimage;"
        Dim d As DictionaryEntry
        DIM intParmCount as integer
        For Each d IN htXMLMap
                IF strParms.indexOf(";" & d.Key & ";")=-1 
                    LogActivity("error","SUB TOC", "Bad Parameter=" & d.key)
                END IF
                intParmCount+=1
        Next d
        
        IF System.Web.HttpContext.Current.request("caller")="utilitybelt"
            'LogDebugData
            LogInfo(strTask,"caller=utilitybelt, displaying subsection")
        htXMLMap("groupmatch")=System.Web.HttpContext.Current.request("section")
        END IF

        ' This is all groupmatch stuff
        DIM strSectionMatch as string=""
        DIM strSectionCurrent as string=""
        DIM strSectionPrev as string=""
        DIM bolSectionShow as boolean=true
        DIM strSectionURL as string
        DIM bolSectionMode as boolean=false
        DIM intSectionCount as integer
        IF htXMLMap.contains("groupmatch")
            LogInfo(strTask,"contains groupmatch")
            strSectionMatch=htXMLMap("groupmatch")
            bolSectionShow=false
            bolSectionMode=true
            IF htXMLMap.contains("groupmatchsectionurl")
                strSectionURL=htXMLMap("groupmatchsectionurl")
            ELSE
                strSectionURL=""
            END IF
        END IF

        DIM strPadLeft="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
        DIM strURLthispage as string= System.Web.HttpContext.Current.Request.RawURL.tolower()

        strParmFileName=FileMapIfNeeded(strParmFileName,"FUNCTION Toc")

        Xmltr1=new XMLtextReader(strParmFileName)

        DIM strURL as string
        DIM strDes as string

        DIM strURLPrev as string
        DIM strDesPrev as string

        DIM strURLNext as string
        DIM strDesNext as string

        DIM strSection as string

        DIM intTOCcounter as integer
        DIM intTOCcurrent as integer

        DIM sb1 as new stringbuilder

        DIM node11 as system.xml.xmlnodetype
        DIM strNodeName as string
        DIM bolInTable=false
        DIM strSectionEnd as string=""
        DIM strSectionStart as string=""

            DO WHILE Xmltr1.Read()
                LogDebugData("nodetype=",XmlTr1.nodeType)
                LogDebugData("nodename=",XMLtr1.Name)
                IF XmlTr1.nodeType=Element AND Xmltr1.Name=htXMLmap("weblocation")
                    strURLprev=strURL
                    strDesPrev=strDes

                    Xmltr1.Read()
                    strURL=XmlTr1.Value

                    intTOCcounter+=1
                    intTOCcurrent+=1

                    strURLPrev=strURL
                    strDesPrev=strDes

                END IF
                IF XmlTr1.nodeType=Element AND Xmltr1.Name=htXMLmap("group")
                    IF bolInTable
                sb1.append("</table>")
                        strSectionEnd="</td></tr></table>" & vbcrlf
                        bolInTable=false
                    END IF                    
 
             IF Xmltr1.hasattributes AND false=true
                         DIM intAttributeCount as integer
                         DIM strAttName as string
                         DIM strAttValue as string
                intAttributeCount=Xmltr1.attributecount()
                DO WHILE Xmltr1.MoveTonextAttribute()
                    strAttValue=XMLTr1.Value()
                        strAttName=Xmltr1.Name
                        trace.write("strAttName=",strAttValue)
                        trace.write("strAttValue=",strAttValue)
                LOOP
                      END IF
                      
                    Xmltr1.MoveTonextAttribute()
                    strSection=XmlTr1.Value
                    intSectionCount+=1
                    strSectionPrev=strSectionCurrent
                    strSectionCurrent=strSection
                    ' This is all groupmatch stuff
                    IF strSection=strSectionMatch
                        bolSectionShow=true
                    ELSE
                        If strSectionMatch<>""
                            bolSectionShow=false
                        END IF
                    END IF
                    
                    DIM strImg as string
                    TRY
                        Xmltr1.MoveTonextAttribute()
                        strImg=XmlTr1.Value
                        IF strImg=strSection
                            strImg=""
                        ELSE
                            bolInTable=true
                        END IF
                    CATCH
                        strImg=""
                    END TRY
                    
                    IF bolSectionShow = false
                        ' nothing to do
                    ELSE
                        WITH sb1
                             IF strImg=""
                          
                             ELSE
                                 .append(vbcrlf)
                                 .append("<table width='100%'><tr><td width='1%'>")
                                 .append("<img src='")
                                 .append(strImg)
                                 .append("'")
                                 .append(" height='200' width='200'>")
                                 .append("</td>")
                                 .append(vbcrlf)
                                 .append("<td width='99%'>")
                                 strImg=""
                             END IF
                             IF strSectionPrev<>strSectionCurrent and BolInTable
                                 .append(vbcrlf)
                                 .append(vbcrlf)
                                 strSectionEnd=""

                             END IF

                            .append("<a href='")
                            .append(strSectionURL)
                            .append("?caller=utilitybelt&section=")
                            .append(server.urlencode(strSection))
                            .append("'><font size='+2'>")
                            .append(strSection)
                            .append("</font></a><br>")
                            .append(vbcrlf)
                        END WITH
                    END IF

                END IF        
                IF XmlTr1.nodeType=Element AND Xmltr1.Name=htXMLmap("webname")
                    Xmltr1.Read()
                    strDes=XmlTr1.value            
                    LogDebugData(strTask,"URL=" & strURL)
                    LogDebugData(strTask, "Desc=" & strDes)

                    LogDebugData(strTask,"strURLPrev=" & strURLPrev)
                    LogDebugData(strTask,"strDesPrev=" & strDesPrev)    

                    IF strURLthispage<>strURL
                        IF bolSectionShow = false
                            ' nothing to do
                        ELSE
                            WITH sb1
                            .append(strPadLeft)
                            .append("<a href='")
                            .append(strURL)
                            .append("'>")
                            .append(strDes)
                            .append("</a><br>")
                            .append(vbcrlf)
                            END WITH
                        END IF
                    END IF
                END IF            
            LOOP
            DIM strPrefix as string=""
            IF  bolSectionMode AND intSectionCount>1 
                strPrefix  = "<a href='" 
                strPrefix &= System.Web.HttpContext.Current.Request.FilePath.tolower()
                strPrefix &= "'><font size='+2'>Full Table Of Contents</font></a><br><br>"
            END IF
        LogTaskEnd(strTask)
        return (strPrefix & sb1.ToString())
    CATCH ex1 as exception
        LogException(strTask,ex1)
    FINALLY
        ' TODO - anyway to see if stream is not open
        Xmltr1.Close()    
    END TRY
END FUNCTION


function XMLToDataTable(strFilename as string) as dataTable
    DIM strTask as string="function XMLToDataTable"
    DIM Xmltr1 as XMLTextReader 
    DIM dt1 as datatable
    TRY
        logTaskStart(strTask)
        Xmltr1=new XMLtextReader(FileMapIfNeeded(strFileName,"function XMLToDataTable"))
        DIM bolTrackThis as boolean=false
        DIM intXMLkey as integer
        DIM strElementCurrent as string
        DIM intAttributeCount as integer=0
        dt1 = New DataTable()
        dt1.Columns.Add(new DataColumn("Key", GetType(String)))
        dt1.Columns.Add(new DataColumn("Element", GetType(String)))
        dt1.Columns.Add(new DataColumn("NodeType", GetType(String)))
        dt1.Columns.Add(new DataColumn("Value", GetType(String)))
        dt1.Columns.Add(new DataColumn("Name", GetType(String)))
        dt1.Columns.Add(new DataColumn("LocalName", GetType(String)))
        dt1.Columns.Add(new DataColumn("NameSpaceURI", GetType(String)))
        Xmltr1.WhiteSpaceHandling=WhiteSpacehandling.None
        Xmltr1.Read()
        DO 
            bolTrackThis=true
            IF XMLTr1.NodeType=Element
                'bolTrackThis=false
                'Xmltr1.Read()
                strElementCurrent=XMLTr1.Name()
            END IF
            IF XMLTr1.NodeType=EndElement
                'bolTrackThis=false
                strElementCurrent=""
            END IF            
            IF bolTrackThis
                intXMLkey+=1
            END IF
            intAttributeCount=0
            IF bolTrackThis=True 
                DIM dtblRowTemp as datarow
                dtblrowTemp = dt1.NewRow()
                dtblrowTemp(0) = intXMLkey
                dtblrowTemp(1) = strElementCurrent
                dtblrowTemp(2) = XMLTr1.NodeType.ToString()
                dtblrowTemp(3) = server.htmlencode(XMLTr1.Value())
                dtblrowTemp(4) = Xmltr1.Name
                dtblrowTemp(5) = Xmltr1.LocalName()
                dtblrowTemp(6) = Xmltr1.NamespaceURI()
                dt1.Rows.Add(dtblrowTemp)
            END IF
            IF Xmltr1.hasattributes
                intAttributeCount=Xmltr1.attributecount()
                DO WHILE Xmltr1.MoveTonextAttribute()
                    intXMLkey+=1
                    DIM dtblRowTemp as datarow
                    dtblrowTemp = dt1.NewRow()
                    dtblrowTemp(0) = intXMLkey
                    dtblrowTemp(1) = strElementCurrent
                    dtblrowTemp(2) = XMLTr1.NodeType.ToString()
                    dtblrowTemp(3) = server.htmlencode(XMLTr1.Value())
                    dtblrowTemp(4) = Xmltr1.Name
                    dtblrowTemp(5) = Xmltr1.LocalName()
                    dtblrowTemp(6) = Xmltr1.NamespaceURI()
                    dt1.Rows.Add(dtblrowTemp)
                LOOP
            END IF
            LOOP WHILE Xmltr1.Read()
            LogTaskEnd(strTask)
    CATCH ex1 as exception
        LogException(strTask,ex1)
    FINALLY
        ' TODO - stream?
        Xmltr1.Close()
    END TRY
    return(dt1)
END FUNCTION


End Class 
End NameSpace

Chaz Wish List
Tall Tip $5
Grande Tip $20
Venti Tip $39
Tip Jar Thanks
2004 Thanks
2005 Thanks
HUGE Tip -love site