<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Using the SqlDataSource.FilterExpression Property</H2></title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="C#" runat="server">
void setBindings ( object src, GridViewRowEventArgs e ) {
if ( e.Row.RowType == DataControlRowType.DataRow ) {
DataRowView rowView = ( DataRowView ) e.Row.DataItem;
Label type = ( Label ) e.Row.FindControl ( "type" );
if ( rowView [ "url" ].ToString ( ).IndexOf ( "demos" ) > -1 )
type.Text = "Demo";
else if ( rowView [ "url" ].ToString ( ).IndexOf ( "syntax" ) > -1 ||
rowView [ "url" ].ToString ( ).IndexOf ( "sys" ) > -1 ||
rowView [ "url" ].ToString ( ).IndexOf ( "refs" ) > -1)
type.Text = "Reference";
else type.Text = "Tutorial";
}
}
void setPager ( Object src, GridViewRowEventArgs e ) {
if ( e.Row.RowType == DataControlRowType.Pager ) {
TableCellCollection myRow = e.Row.Cells;
myRow.AddAt ( 0, new TableCell ( ) );
myRow [ 0 ].Text = "Page " + ( searchGrid.PageIndex + 1 ) + " of " + searchGrid.PageCount;
myRow [ 0 ].HorizontalAlign = HorizontalAlign.Left;
}
}
</script>
</head>
<body>
<div class="header"><h2>Using the SqlDataSource.<b>FilterExpression</b> Property</h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<form runat="server">
<div align="center">
<p>Search for <asp:textbox id="searchBox" runat="server" />
<asp:button text="Search" runat="server" /></p>
<asp:sqldatasource id="articles" runat="server"
connectionstring="<%$ ConnectionStrings:aspnet %>"
selectcommand="SELECT title, url, added, updated FROM aspx_articles ORDER BY title"
filterexpression="title LIKE '%{0}%' or url LIKE '%{0}%'">
<filterparameters>
<asp:controlparameter controlid="searchBox" propertyname="Text" />
</filterparameters>
</asp:sqldatasource>
<asp:gridview id="searchGrid" runat="server"
datasourceid="articles"
width="80%" cellpadding=5 font-size="10pt"
autogeneratecolumns=false
allowsorting allowpaging pagesize=16
onRowCreated="setPager"
onRowDataBound="setBindings">
<headerstyle font-bold backcolor="lightsteelblue" />
<rowstyle verticalalign="top" />
<pagersettings position="top"
mode="nextprevious"
nextpagetext="Next"
previouspagetext="Prev" />
<pagerstyle font-bold
backcolor="cadetblue" forecolor="beige"
horizontalalign="right" />
<columns>
<asp:hyperlinkfield headertext="Title"
datatextfield="title"
datanavigateurlfields="url"
datanavigateurlformatstring="~/{0}"
target="_blank" />
<asp:templatefield headertext="Content Type">
<itemtemplate>
<asp:label id="type" runat="server" />
</itemtemplate>
</asp:templatefield>
<asp:boundfield headertext="Last Updated"
datafield="updated"
dataformatstring="{0:d}" />
</columns>
<emptydatatemplate>
<div style="font:bold 13pt arial; color:navy; text-align:center">
<p>Sorry, there was no match found for <i><%= searchBox.Text %></i>. </div>
</emptydatatemplate>
</asp:gridview>
</div>
</form>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>