Search from a Textbox-Display with a GridView

This sample shows several things, however, the main item shown is the procedure laid out in the title of the sample. In this sample, DS2 uses a ‘Like’ SQL Statement, in order for the user to type in a minimum numer of characters, for the search (here, like ‘bo’, ‘ch’, or ‘ant’).

Also, since there are two SQLDataSourceControls, here, we show how to assign the connectionstring to a variable, and programmatically change/assign the connectionstring in code. Along with that, we also show how to assign the Selectcommands for each, in code. Mainly, though, in this case, since the connectionstrings are lengthy, it helps to do this, in order to save screen width.

<script language="VB" Runat="server">
Dim sConn as String
Sub Page_Load(Source as Object, E as EventArgs)
sConn="server=YourServer;Database=YourDb;pwd=YourPWD;uid=YourUID"

DS1.SelectCommand = "SELECT  ProductID,  ProductName,  CategoryID,  " & _
"UnitsInStock,  UnitPrice From  dbo.NWProducts"
DS1.ConnectionString=sConn

DS2.SelectCommand = "Select [ProductID], [ProductName], [QuantityPerUnit], " & _
"[UnitPrice] from NWProducts Where ProductName LIKE ''%'' + @ProductName + ''%''"
DS2.ConnectionString=sConn
End Sub

Sub doSearch(Source as Object, E as EventArgs)
MyGridView.DataSourceID = "DS2"
MyGridView.DataBind()
End Sub

Sub doShowAll(Source as Object, E as EventArgs)
MyGridView.DataSourceID = "DS1"
MyGridView.DataBind()
End Sub
</script>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 5.0">
<title>Search from a Textbox - Display with a GridView</title>
</head>
<body>
<form id="form1" Runat="server">
Search by Product Name:
<asp:TextBox id="txtProduct" Runat="server" />
<asp:Button id="button1" Text="Search" onclick="doSearch" Runat="server" />
<asp:Button id="button2" Text="Show All" onclick="doShowAll" Runat="server" />
<asp:GridView Runat="server"
Id="MyGridView"
GridLines="None"
cellpadding="0"
cellspacing="1"
Headerstyle-BackColor="#7988B7"
Headerstyle-Forecolor="#FFFFFF"
Headerstyle-Font-Names="Arial"
Headerstyle-Font-Size="10"
BackColor="#E0E0F6"
Font-Names="Arial"
Font-Size="8"
AlternatingRowStyle-BackColor="#EFEFEF"
AlternatingRowStyle-Font-Names="Arial"
AlternatingRowStyle-Font-Size="8"
BorderColor="Black"
AllowPaging = "True"
PageSize = "10"
PagerSettings-Mode = "Numeric"
PagerStyle-HorizontalAlign="Center">
</asp:GridView>

<asp:SQLDataSource ID="DS1"
Runat="Server">
</asp:SQLDataSource>
<asp:SQLDataSource ID="DS2"
Runat="Server">
<SelectParameters>
<asp:ControlParameter
ControlID="txtProduct"
Name="ProductName"
PropertyName="Text"
Type="String"></asp:ControlParameter>
</SelectParameters>
</asp:SQLDataSource>
</form>
</body>
</html>

Related Posts:

  • No Related Posts
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!