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

Searching Example with Cached User Controls
by Charles Carroll

User Controls can be cached. Here is an attempt to do so by just fragment caching a User Control.

   filename=/students/charlescarroll/searcheruc_cached_fixed.aspx

<Test Script Below>


<%@ trace="true" debug="true"%>
<%@ Register TagPrefix="charles" tagname="list" src="dblist_cached.ascx"%> 
<%@ Register TagPrefix="charles" tagname="grid" src="datagrid.ascx"%> 
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
    ' cant be done with caching hacked into UC
    ' searchagain.visible=false
    trace.tracemode=TraceMode.SortByTime
END SUB

Sub Search_click(s as object, e as eventargs)
    trace.write("city", request("cy:thelist")) 
    trace.write("state", request("st:thelist") ) 
    trace.write("zip", request("zp:thelist") ) 
    
    
    DIM strSQL as string
    DIM prefix as string
    
    searchcriteria.visible=false
    search.visible=false
    searchagain.visible=true
    searchresults.visible=true
    
    strSQL="select * from publishers where "
    
    If cyinclude.checked=false AND stinclude.checked=false AND zpinclude.checked=false THEN
        message.text="You did not choose any search categories so we cannot Search"
        searchresults.visible=false
        exit sub
    ELSE
        message.text=""
    END IF
    
    IF cyinclude.checked THEN
        strSQL=strSQL & " city='" & request("cy:thelist") & "' "
        prefix=" AND "
    END IF

    IF stinclude.checked THEN
        strSQL=strSQL & prefix & " state='" & request("st:thelist") & "' "
        prefix=" AND "
    END IF

    IF zpinclude.checked THEN
        strSQL=strSQL & prefix & " zip='" & request("st:thelist") & "' "
    END IF
    
    trace.write("strSQL",strSQL) 

    ' cant be done with caching hacked into UC
    searchresults.SQL=strSQL
    searchresults.databind()
    
END SUB

SUB Searchagain_click(s as object, e as eventargs)
    searchcriteria.visible=true
    
    searchagain.visible=false
    search.visible=true
    
    ' cant be done with caching hacked into UC
    searchresults.visible=false
    page.RegisterHiddenField("SQL","")
END SUB
</script>
<html><head>
<title>Searcher</title>
</head>
<body bgcolor="#FFFFFF">
<form runat="server">
<asp:literal id="message" runat="server"/>
<asp:Table id="searchcriteria" runat="server" GridLines="both" BorderWidth="1px">
<asp:TableRow>
<asp:TableCell>City</asp:TableCell>
<asp:TableCell>
    <charles:list id="cy"
        connection="/experiments/data/biblio.mdb"
        SQL="select distinct city from Publishers" 
        textfield="city" 
        valuefield="city" 
    runat="server"/>
</asp:TableCell>
<asp:TableCell>
    <ASP:checkbox id="cyinclude" text="include in Search?" runat="server"/>
</asp:TableCell>
</asp:TableRow>

<asp:TableRow>
<asp:TableCell>State</asp:TableCell>
<asp:TableCell>
    <charles:list id="st"
        connection="/experiments/data/biblio.mdb"
        SQL="select distinct state from Publishers" 
        textfield="state" 
        valuefield="state"     
    runat="server"/>
</asp:TableCell>
<asp:TableCell>
    <ASP:checkbox id="stinclude" text="include in Search?" runat="server"/>
</asp:TableCell>
</asp:TableRow>

<asp:TableRow>
<asp:TableCell>Zip</asp:TableCell>
<asp:TableCell>
    <charles:list id="zp"
        connection="/experiments/data/biblio.mdb"
        SQL="select distinct zip from Publishers" 
        textfield="zip" 
        valuefield="zip"     
    runat="server"/>
</asp:TableCell>
<asp:TableCell>
    <ASP:checkbox id="zpinclude" text="include in Search?" runat="server"/>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:button id="search" text="search for data" onclick="search_click" runat="server"/><br>
<asp:button id="searchagain" text="search for data again" onclick="searchagain_click" runat="server"/>

<charles:grid id="searchresults" 
    visible="false" connection="/experiments/data/biblio.mdb" runat="server"/> 

</form>

</body></html>
Here is listbox user control.

filename=/students/charlescarroll/dblist_cached.ascx


<%@ Import Namespace="System.Data.oledb" %>
<%@ OutputCache Duration="600" VaryByParam="none" VaryByControl="connection,SQL,valueField,textField"%>
<script language="vb" runat="server">

Public connection AS string
Public SQL AS string
public valuefield as string 
public textfield as string
public choice as string

Sub Page_Load(S As Object, E As EventArgs)
        trace.write("dblist_cached.ascx SUB Page_Load","start")
    If ispostback=false THEN
        IF connection.startswith("/") THEN
            connection="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath(connection) & ";"
        END IF
        Dim Conn as New OLEDBConnection(Connection) 
        Dim Cmd as New OLEDBCommand(SQL,Conn) 
        Conn.Open() 
        thelist.DataSource = Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection) 
        thelist.datatextfield=textfield
        thelist.datavaluefield=valuefield
        thelist.DataBind() 
        conn.close()
    ELSE
        choice=thelist.selecteditem.text
    END IF
    trace.write("dblist_cached.ascx SUB Page_Load","end")
End Sub
</script>
<ASP:DropDownList id="thelist" runat="server"/>
Here is grid user control.

filename=/students/charlescarroll/datagrid.ascx


<%@ control classname="dbgrid"%>
<%@ Import Namespace="System.Data.oledb" %> 
<script language="VB" runat="server"> 

public SQL as string

public connection as string

public connectionAppSetting as string

public visible as boolean 

Sub DataBind()
    mydatagrid.visible=visible
    If visible=false THEN
        EXIT SUB
    END IF
    TRY
    IF LEN(connectionappsetting)>0 THEN
        connection=ConfigurationSettings.AppSettings(connectionAppSetting)
    END IF
    IF connection.startswith("/") THEN
        connection="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath(connection) & ";"
    END IF
        Dim Conn as New OLEDBConnection(Connection) 
    Dim Cmd as New OLEDBCommand(SQL,Conn) 
    Conn.Open() 
    myDataGrid.DataSource = Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection) 
    myDataGrid.DataBind() 
    errormsgsql.text=""
    errormsg.text=""
    CATCH oledberr as oledbException
            errormsgsql.text="<br>OLEDB Msg=" & oledberr.Message & "<br>"
    CATCH exc As Exception
        If instr(exc.tostring(),"could not auto-generate any columns")>0 THEN
            errormsg.text="Zero Records matched that request"
        ELSE    
            errormsg.text="<br>Exception #" & exc.ToString() & "<br>"
        END IF
    FINALLY
        If errormsg.text & errormsgsql.text="" THEN
            mydatagrid.visible=true
        ELSE
            mydatagrid.visible=false
        END IF
    END TRY
END SUB
</script> 
<asp:label id="errormsgsql" font-size="14" forecolor="red" runat="server"/>
<asp:label id="errormsg" font-size="14" forecolor="red" runat="server"/>

<ASP:DataGrid id="MyDataGrid" runat="server" 
BackColor="white" 
BorderColor="black" 
ShowFooter="false" 
CellPadding=3 
CellSpacing="0" 
Font-Name="Verdana" 
Font-Size="8pt" 
Headerstyle-BackColor="lightblue" 
Headerstyle-Font-Size="10pt" 
Headerstyle-Font-Style="bold" 
MaintainState="false" 
/> 
</font>

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.