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

Version3 User Controls For Amazon.com books
by Charles Carroll

This page demonstrates a very useful in the real-world User Control to encapsulate a hyperlink and book cover to buy something from Amazon.com and to use one's promotional code to make a little money on the deal.

Simple page:

   filename=/experiments/usercontrols/testbooks_v3.aspx

<Test Script Below>


<%@ Trace="true" debug="true" %>
<%@ Register TagPrefix="bookshow" tagname="cover" src="books_v3.ascx"%>
<html><head>
<title>CodeShow Test</title>
</head>
<body>

<bookshow:cover ISBN="0201485672" 
    promocode="learnasp" AmazonDevToken="D1967KF255R5KZ"
    ShowDetails="ProductName,ListPrice,OurPrice,UsedPrice,Author,AvgCustomerRating" 
    height=105 width=80 runat="server"/><br>
<bookshow:cover ISBN="0517887290" 
    promocode="learnasp" AmazonDevToken="D1967KF255R5KZ"
    ShowDetails="ProductName,ListPrice,OurPrice,UsedPrice,Author,AvgCustomerRating" 
    height=105 width=80 runat="server"/><br>
<bookshow:cover ISBN="0385484992" 
    promocode="learnasp" AmazonDevToken="D1967KF255R5KZ"
    ShowDetails="ProductName,ListPrice,OurPrice,UsedPrice,Author,AvgCustomerRating" 
    height=105 width=80 runat="server"/><br>
<bookshow:cover ISBN="0451169530" promocode="easterseals" height=105 width=80 runat="server"/><br>
<bookshow:cover ISBN="0553283685"
    promocode="learnasp" AmazonDevToken="D1967KF255R5KZ"
    ShowDetails="All" 
    height=105 width=80 runat="server"/><br>
<bookshow:cover ISBN="B0000797E5"
    promocode="learnasp" AmazonDevToken="D1967KF255R5KZ"
    ShowDetails="All" 
    height=105 width=80 runat="server"/><br>

</body>
</html>

The User Control:

filename=/experiments/usercontrols/books_v3.ascx


<%@ debug="true" %>
<%@import namespace="system.io"%>
<%@import namespace="system.net"%>
<%@import namespace="system.xml"%>
<%@import namespace="System.Security.Cryptography"%>
<script runat="server" language="vb">

Public isbn AS string           ' rainbow item only
public localGraphic as string   ' rainbow item only

Public height AS integer=80     ' rainbow item, module can set default*
Public width as integer=55      ' rainbow item, module can set default* 
public AmazonDevToken as string ' rainbow item, module can set default* 
public showDetails as string="ProductName,OurPrice,UsedPrice,Author,Actor,Director,MPAARating,TheatricalReleaseDate,AvgCustomerRating,Artist,ReleaseDate,Track" ' rainbow item, module can set default*
public promocode as string="learnasp" '  rainbow item, module can set default*

' ~/Amazon.config should be checked for defaults, then module, then item

Sub Page_Load(Src As Object, E As EventArgs) 
    Trace.TraceMode = TraceMode.SortByTime
    book.NavigateUrl="http://www.amazon.com/exec/obidos/ISBN=" & ISBN & "/" & promocode
    IF len(localgraphic)=0 THEN
        bookcover.ImageURL="http://images.amazon.com/images/P/" & ISBN & ".01.MZZZZZZZ.jpg"
    ELSE
        bookcover.ImageURL=localgraphic
    END IF
    bookcover.Alternatetext="Amazon:" &  isbn
    bookcover.height=Unit.Pixel(height)
    bookcover.width=Unit.pixel(width)
    
    If ShowDetails.Length>0
        dim strAmazonURL as string="http://xml.amazon.com/onca/xml2?"
        strAmazonURL &= "t=" & PromoCode & "&dev-t=" & AmazonDevToken
        strAmazonURL &= "&type=heavy&AsinSearch=" & isbn & "&f=xml"

        DIM xmlTr1 as XmlTextReader
        xmlTr1=XMLReaderCached_v2(strAmazonURL)

        LitBookDetails.Text="<br>"
        ShowDetails="," & ShowDetails & ","
        DO WHILE xmlTr1.Read()
            if xmlTr1.NodeType.ToString()="Element"
                dim strName as string=xmlTr1.Name
                if showDetails.IndexOf("," & strName & ",")>-1 OR showDetails.tolower()=",all,"
                    xmlTr1.Read()
                    'trace.write("isemptyelement;hasvalue;name;typename;length", xmltr1.IsEmptyElement() & ";" & xmltr1.HasValue() & ";" & strName & ";" & typename(xmlTr1.value) & ";" & Xmltr1.value.length())
                    LitBookDetails.Text &= strName & "&nbsp;&nbsp;<b>" & xmlTr1.Value & "</b><br>"
                End if
            end if
        LOOP    
    end if
End Sub

private function XmlReaderCached_v1(strXML as string) as XmlTextReader
        dim xmlTrCached as new XmlTextReader(strXML)
    return(xmlTrCached)
end function


private function XmlReaderCached_v2(strXML as string) as XmlTextReader
        dim xmlTrCached as XmlTextReader
    Dim strChksum As String=MD5checksum(strXML)
    Dim strInCache as string
    If Cache(strChksum) Is Nothing Then
        trace.write("cache miss",strXML)
        strInCache=httpGet(strXML)
        If strInCache="error"
            ' nothing to do
        else
            Cache.Insert(strChksum,strInCache,Nothing,DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration)
        end if
    Else
        trace.write("cache hit",strXML)
        strInCache=Cache(strChksum)
    End If

    dim strReader1 as StringReader= new StringReader(strInCache)
    xmlTrCached=new XmlTextReader(strReader1)
    return(xmlTrCached)
end function

private function httpget(strURL as string) as string
    dim sr1 as streamreader

    dim strTemp as string
    Dim webResponse1 As WebResponse
        Dim webRequest1 As WebRequest

    TRY
        webRequest1 = webRequest1.Create(strURL)
        webResponse1 = webRequest1.GetResponse()
        sr1 = New StreamReader(webResponse1.GetResponseStream())
        strTemp=sr1.ReadToEnd()
        
    CATCH exc1 as exception

        strTemp="error"
    FINALLY
        sr1.Close
    END TRY
    return(strTemp)
end function

Private Function ConvertStr2ByteArray(ByVal strInput As String) As Byte()
       Dim intCounter As Integer
       Dim arrChar As Char()
       arrChar = strInput.ToCharArray()
       Dim arrByte(arrChar.Length - 1) As Byte
       For intCounter = 0 To arrByte.Length - 1
       arrByte(intCounter) = Convert.ToByte(arrChar(intCounter))
       Next
       Return arrByte
End Function

private Function MD5checksum(ByVal strParm1 As String) As String
       Dim arrHashInput As Byte()
       Dim arrHashOutput As Byte()
       Dim objMD5 As new MD5CryptoServiceProvider
       arrHashInput = ConvertStr2ByteArray(strParm1)
       arrHashOutput = objMD5.ComputeHash(arrHashInput)
       return(BitConverter.ToString(arrHashOutput))
End Function

</script>
<asp:hyperlink id="book" runat="server">
<asp:Image ID="bookcover" runat="server" />
</asp:hyperlink>
<asp:Literal id="litBookDetails" runat="server"/>


There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too. a $5 tip buys him lunch at McDonalds, a $20 tip buys his kid Hitoshi a new computer game, a $39 tip buys his daughter Michiko a few nice outfits. See our donor list.