locked
Create Search Box and query against the DB for output RRS feed

  • Question

  • User1621236832 posted

    I would simply like for end user to type-in a state on textbox (textbox1) control and after they click on "Search" button, it will query the DB and display the results on the grid. How to best acheive this task in simple ways?

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3.Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            Please Enter a State&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Search" />
            <br />
            <br />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                DataKeyNames="StateID" DataSourceID="StateSearchSqlDataSource">
                <Columns>
                    <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
                    <asp:BoundField DataField="StateID" HeaderText="StateID" ReadOnly="True" 
                        SortExpression="StateID" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="StateSearchSqlDataSource" runat="server" 
                ConnectionString="<%$ ConnectionStrings:WebDevConnectionString2 %>" 
                SelectCommand="SELECT [State], [StateID] FROM [States]"></asp:SqlDataSource>
        <br /> 
            <br /> 
    
    
        </div>
        </form>
    </body>
    </html>
    
    Tuesday, May 17, 2011 6:08 PM

Answers

  • User-1499637000 posted

    Hi,

    The simple way is to do with the selectparemeters and control parameter, here is an example how to do:

    <%@ Page Language="C#" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Text box Search example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <table border="1px solid">
                <tr>
                    <td>
                        Enter product Name:
                    </td>
                    <td>
                        <asp:TextBox ID="txtProductName" Text="" runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:Button ID="btnSearch" runat="server" Text="Search" />
                    </td>
                </tr>
                <tr>
                    <td colspan="3">
                        <asp:GridView ID="grvProducts" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                            DataKeyNames="ProductID" DataSourceID="SqlDataSource1" ShowFooter="True">
                            <Columns>
                                <asp:BoundField DataField="ProductID" HeaderText="ProductID" SortExpression="ProductID" />
                                <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                                <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
                                <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
                            </Columns>
                            <PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast"
                                PageButtonCount="3" Position="Bottom" />
                            <PagerStyle CssClass="pagination" HorizontalAlign="Center" VerticalAlign="Middle" />
                        </asp:GridView>
                    </td>
                </tr>
            </table>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                SelectCommand="SELECT [ProductID], [ProductName], [UnitsInStock], [UnitPrice] FROM [Alphabetical list of products] WHERE ([ProductName] LIKE '%' + @ProductName + '%')">
                <SelectParameters>
                    <asp:ControlParameter ControlID="txtProductName" DefaultValue="%" Name="ProductName"
                        PropertyName="Text" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
        </div>
        </form>
    </body>
    </html>
    

    Hope the above example helps.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 18, 2011 12:02 AM