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>
[prev. Lesson]  DataList to Display multi-column HTML from database
     [next Lesson]  ADO.net: ExecuteScalar for One Value

"Stupid" Data Binding Tricks by Charles Carroll

This page demonstrates how databinding functions can be used to emit arbitrary HTML into a database display without having to place any code in the templates, merely one function call. It illustrates the potential to be used for a variety of purposes: totals, fixing nulls, conditionally displaying data, displaying group bands, etc.

Here is a few databinding samples:

   filename=/experiments/databinding/stupidtricks.aspx

<Test Script Below>


<%@ Import Namespace="System.Data.OLEDB" %> 
<script language="VB" runat="server"> 
Sub Page_Load(Src As Object, E As EventArgs) 
    Dim strConn as string ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("/experiments/data/biblio.mdb") & ";" 
    Dim strSQL as string ="select * from publishers where state='NY' order by city" 
    Dim Conn as New OLEDBConnection(strConn) 
    Dim Cmd as New OLEDBCommand(strSQL,Conn) 
    Conn.Open() 
    thedata.DataSource = Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection) 
    thedata.DataBind() 
End Sub 

Dim intTotal as integer
function dtbndAddone() as string
    intTotal+=1
end function

function dtbndTotalOfData() as string
    return intTotal
end function

function dtbndPrettyCity(p1 as string) as string
    dim strOutput as string
    If p1="Albany" THEN
        strOutput="<b><font color='red'>" & p1 & "</b></font>"
    ELSE
        strOutput=p1
    END IF
    return strOutput
end function

dim intCityCount as integer
function dtbndHeader1(p1 as string) as string
    static strCityPrevious as string
    static strCityCurrent as string
    dim strOutput as string
    strCityCurrent=p1
    If strCityCurrent=strCityPrevious THEN
        strOutput=""
    ELSE
        strOutput="<b>NEW CITY GROUP</b><hr>"
        intCityCount+=1
    END IF
    strCityPrevious=strCityCurrent
    return strOutput
end function

Function dtbndFaxDisplay(p1 as string,p2 as object,p3 as string) as string
    If typeof(p2) IS dbnull THEN
        return ""
    END IF
    If len(p2)>0 THEN
        Return p1 & p2 & p3
    END IF
End Function

Function dtbndStateZipCombo(p1 as string,p2 as string) as string
    Return p1 & "&nbsp;&nbsp;&nbsp;" & p2
End Function

</script> 
<html><head> 
<title>Stupid DataBinding Tricks</title></head> 
<body bgcolor="#FFFFFF"> 
<ASP:repeater id="thedata" runat="server"> 

<itemtemplate> 
    <hr>
    <%# dtbndHeader1(Container.DataItem("City"))%>
    PubID=<b><%# Container.DataItem("PubID")%></b><BR> 
    <%# Container.DataItem("Company Name")%><br> 
    <%# dtbndPrettyCity(Container.DataItem("City"))%>, 
    <%# dtbndStateZipCombo(Container.DataItem("State"),Container.DataItem("Zip"))%><br> 
    Tel=<%# Container.DataItem("Telephone")%><br> 
    <%# dtbndFaxDisplay("Fax=",Container.DataItem("Fax"),"<br>")%>
    <%# dtbndAddOne()%>
</itemtemplate>

<footertemplate>
    <hr>
    <h3>Total Count=<%# dtbndTotalofData()%><br>
    Unique Cities=<%# intCityCount %></h3>
</footertemplate>

</ASP:repeater> 
</body></html> 
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.