Monday 13 August 2012

ASP.NET Simple SearchBox

Introduction:
    Going to explain Simple Search Box using SqlDataSource And LIKE Query..

Explaination:
We Have TextBox1 and Button1 And GridView1

HTML Markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search-Mahad-aspspirits.aspx.cs" Inherits="Search_Mahad_aspspirits" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        Search
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            Text="Search!" />
    
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
            CellPadding="3" DataSourceID="SqlDataSource1" GridLines="Horizontal">
            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
            <Columns>
                <asp:BoundField DataField="CampusCode" HeaderText="CampusCode" 
                    SortExpression="CampusCode" />
                <asp:BoundField DataField="StudentName" HeaderText="StudentName" 
                    SortExpression="StudentName" />
                <asp:BoundField DataField="Gender" HeaderText="Gender" 
                    SortExpression="Gender" />
                <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
            </Columns>
            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            <AlternatingRowStyle BackColor="#F7F7F7" />
        </asp:GridView>
    
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:gomahadConnectionString %>" 
            SelectCommand="SELECT CampusCode, StudentName, Gender, Age FROM registration WHERE StudentName LIKE '%' + @StName + '%'">
            <SelectParameters>
                <asp:ControlParameter ControlID="TextBox1" Name="StName" PropertyName="Text" />
            </SelectParameters>
        </asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>

First of all Place a GridView on your Desired place, then click on GridView Top Right small arrow and Select DataSource, Choose New DataSource.. Then After Setup your SqlDataSource On "Configure SELECT Statement"


As you can See you have "Parameter Source" By which you can also Get Value From "Control, QueryString, Session, Form, Cookie"

on Button Click you can write Code of DataBind.

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlDataSource1.DataBind();
        GridView1.DataBind();
    }
You can see with a single line of C# Code we Can Achieve this Goal by SqlDataSource.
Hope this will help you!
Good Luck`

10 comments:

Mahad Bin Mukhtar said...

Thankyou.. for your Feedback..

Unknown said...

I really Enjoy the reading on this post, please continue it.


SEO Sydney | SEO Melbourne | SEO

Unknown said...

hmmm u luk good

Unknown said...

Thanks for your sharing.. really helpfull.. :)
This is the first time for me develop with asp.net and my client asked me to insert the search box in his asp.net website.


Unknown said...

Simple & to the points, works great! Thanks! :)

Unknown said...

could u pls tell me how to add multiple search in gridview using sqldatasorce.

Unknown said...
This comment has been removed by the author.
Unknown said...

This is a comments

Unknown said...

VVVVVVVVVVV

Unknown said...

How we can do the same thing with external database MS Sql server????

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More