<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.Configuration" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void btnSearch_Click(object sender, EventArgs e)
{
DataTable dtblProducts = (DataTable)Cache["ProductsToFilter"];
if (dtblProducts == null)
{
dtblProducts = GetProductsFromDB();
Cache["ProductsToFilter"] = dtblProducts;
}
string filter = String.Format("Title LIKE '{0}*'", txtTitle.Text);
DataRow[] rows = dtblProducts.Select(filter, "Title");
grdProducts.DataSource = rows;
grdProducts.DataBind();
}
private DataTable GetProductsFromDB()
{
string connectionString = WebConfigurationManager.ConnectionStrings["Products"].ConnectionString;
SqlDataAdapter dad = new SqlDataAdapter("SELECT Title, Director FROM Products", connectionString);
DataTable dtblProducts = new DataTable();
dad.Fill(dtblProducts);
return dtblProducts;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Show DataTable Select</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox
id="txtTitle"
Tooltip="Search"
Runat="server" />
<asp:Button
id="btnSearch"
Text="Search"
Runat="server" OnClick="btnSearch_Click" />
<hr />
<asp:GridView
id="grdProducts"
AutoGenerateColumns="false"
Runat="server">
<Columns>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<%# ((DataRow)Container.DataItem)["Title"] %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Director">
<ItemTemplate>
<%# ((DataRow)Container.DataItem)["Director"] %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
File: Web.config
<configuration>
<connectionStrings>
<add name="Products"
connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=|DataDirectory|MyDatabase.mdf;Integrated Security=True;User Instance=True" />
</connectionStrings>
</configuration>
|