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

Typical Searching Example (dropdown lists, grids)
by Charles Carroll

Here is typical code that would be used in ASP.net to create a search page. Note that unlike Classic ASP, the use of server controls eliminates the need for HTTP request model programming. Even though the resultant code created is HTML 3.2, the page itself codes using a rich object programming model server-side that then generates the "HTTP/HTML plumbing" required".

   filename=/experiments/searcher/searcher_v1.aspx

<Test Script Below>


<%@ trace="true"%>
<%@ Import Namespace="System.Data.OLEDB" %>
<script language="VB" runat="server">
Dim strConn as string="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("/experiments/data/biblio.mdb") & ";" 
Sub Page_Load(Src As Object, E As EventArgs)
    searchagain.visible=false
    If ispostback = false THEN
        Dim strSQL as string
        Dim Conn as New OLEDBConnection(strConn)

        strSQL="select distinct city from publishers"
        Dim Cmd as New OLEDBCommand(strSQL,Conn)
        Conn.Open()
        cy.DataSource = Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
        cy.DataBind()
        Conn.Close()

        strSQL="select distinct state from publishers"
        Dim Cmd2 as New OLEDBCommand(strSQL,Conn)
        Conn.Open()
        st.DataSource = Cmd2.ExecuteReader(system.data.CommandBehavior.CloseConnection)
        st.DataBind()    
        Conn.Close()
        
        strSQL="select distinct zip from publishers"
        Dim Cmd3 as New OLEDBCommand(strSQL,Conn)
        Conn.Open()
        zp.DataSource = Cmd3.ExecuteReader(system.data.CommandBehavior.CloseConnection)
        zp.DataBind()    
        Conn.Close()    
    END IF
End Sub

Sub Search_click(s as object, e as eventargs)
    trace.write("city",cy.selecteditem.text) 
    trace.write("state",st.selecteditem.text) 
    trace.write("zip",zp.selecteditem.text) 
    trace.write("cityinc",cyinclude.checked) 
    trace.write("stateinc",stinclude.checked) 
    trace.write("zipinc",zpinclude.checked) 
    
    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='" & cy.selecteditem.text & "' "
        prefix=" AND "
    END IF

    IF stinclude.checked THEN
        strSQL=strSQL & prefix & " state='" & st.selecteditem.text & "' "
        prefix=" AND "
    END IF

    IF zpinclude.checked THEN
        strSQL=strSQL & prefix & " zip='" & zp.selecteditem.text & "' "
    END IF
    
    trace.write("strSQL",strSQL) 

    Dim Conn as New OLEDBConnection(strConn)
    Dim Cmd as New OLEDBCommand(strSQL,Conn)
    Conn.Open()
    searchresults.DataSource = Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
    searchresults.DataBind()    
    Conn.Close()        
    
END SUB

SUB Searchagain_click(s as object, e as eventargs)
    searchcriteria.visible=true
    searchresults.visible=false
    searchagain.visible=false
    search.visible=true
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>
    <ASP:DropDownList id="cy" datatextfield="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>
    <ASP:DropDownList id="st" datatextfield="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>
    <ASP:DropDownList id="zp" datatextfield="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"/>

<ASP:DataGrid id="searchresults" runat="server" 
Width="100%" 
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" 
/> 

</form>

</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.