|
xxx
Searching Example with Functional User
Controls by Charles Carroll
This example illustrates User Controls
created for functional reasons.
filename=/students/charlescarroll/searcheruc_functional.aspx
<%@ trace="true" debug="true"%>
<%@ Register TagPrefix="charles" tagname="listcity" src="listcity.ascx"%>
<%@ Register TagPrefix="charles" tagname="liststate" src="liststate.ascx"%>
<%@ Register TagPrefix="charles" tagname="listzip" src="listzip.ascx"%>
<%@ Register TagPrefix="charles" tagname="grid" src="datagrid.ascx"%>
<script language="VB" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
searchagain.visible=false
END SUB
Sub Search_click(s as object, e as eventargs)
trace.write("city",cy.choice)
trace.write("state",st.choice)
trace.write("zip",zp.choice)
trace.write("cityinc",cyinclude.checked)
trace.write("stateinc",stinclude.checked)
trace.write("zipinc",zpinclude.checked)
Dim strConn as string="/experiments/data/biblio.mdb"
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.choice & "' "
prefix=" AND "
END IF
IF stinclude.checked THEN
strSQL=strSQL & prefix & " state='" & st.choice & "' "
prefix=" AND "
END IF
IF zpinclude.checked THEN
strSQL=strSQL & prefix & " zip='" & zp.choice & "' "
END IF
trace.write("strSQL",strSQL)
searchresults.SQL=strSQL
searchresults.connection=strConn
searchresults.databind()
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>
<charles:listcity id="cy" 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:liststate id="st" 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:listzip id="zp" 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" runat="server"/>
</form>
</body></html>
Here is city user control.
filename=/students/charlescarroll/listcity.ascx
<%@ Import Namespace="System.Data.oledb" %>
<script language="vb" runat="server">
public choice as string
Sub Page_Load(S As Object, E As EventArgs)
Dim strConn as string="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("/experiments/data/biblio.mdb") & ";"
If ispostback=false THEN
Dim Conn as New OLEDBConnection(strConn)
Dim Cmd as New OLEDBCommand("select distinct city from publishers",Conn)
Conn.Open()
thelist.DataSource = Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
thelist.datatextfield="city"
thelist.datavaluefield="city"
thelist.DataBind()
conn.close()
ELSE
choice=thelist.selecteditem.text
END IF
End Sub
</script>
<ASP:DropDownList id="thelist" runat="server"/>
Here is state user control.
filename=/students/charlescarroll/liststate.ascx
<%@ Import Namespace="System.Data.oledb" %>
<script language="vb" runat="server">
public choice as string
Sub Page_Load(S As Object, E As EventArgs)
Dim strConn as string="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("/experiments/data/biblio.mdb") & ";"
If ispostback=false THEN
Dim Conn as New OLEDBConnection(strConn)
Dim Cmd as New OLEDBCommand("select distinct state from publishers",Conn)
Conn.Open()
thelist.DataSource = Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
thelist.datatextfield="state"
thelist.datavaluefield="state"
thelist.DataBind()
conn.close()
ELSE
choice=thelist.selecteditem.text
END IF
End Sub
</script>
<ASP:DropDownList id="thelist" runat="server"/>
Here is zip user control.
filename=/students/charlescarroll/listzip.ascx
<%@ Import Namespace="System.Data.oledb" %>
<script language="vb" runat="server">
public choice as string
Sub Page_Load(S As Object, E As EventArgs)
Dim strConn as string="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & server.mappath("/experiments/data/biblio.mdb") & ";"
If ispostback=false THEN
Dim Conn as New OLEDBConnection(strConn)
Dim Cmd as New OLEDBCommand("select distinct zip from publishers",Conn)
Conn.Open()
thelist.DataSource = Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
thelist.datatextfield="zip"
thelist.datavaluefield="zip"
thelist.DataBind()
conn.close()
ELSE
choice=thelist.selecteditem.text
END IF
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.
|  |
 |  |  |
|
|
|
|