Thursday, July 10, 2014

Search Gridview Rows Based On Header Text Box Values Using OntextChanged And Like Command


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="grid.aspx.cs" Inherits="grid" %>

<!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>
        <asp:GridView ID="grd" AutoGenerateColumns="False" runat="server" BackColor="#DEBA84"
            BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">
            <Columns>
                <asp:TemplateField HeaderText="id">
                    <HeaderTemplate>
                        <asp:TextBox ID="txtid" runat="server" OnTextChanged="ontxtid" AutoPostBack="true"></asp:TextBox>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblid" runat="server" Text='<%#bind("id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="fname">
                    <HeaderTemplate>
                        <asp:TextBox ID="txtfname" runat="server" OnTextChanged="ontxtfname" AutoPostBack="true"></asp:TextBox>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblfname" runat="server" Text='<%#bind("fname") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="lname">
                    <HeaderTemplate>
                        <asp:TextBox ID="txtlname" runat="server" OnTextChanged="ontxtlname" AutoPostBack="true"></asp:TextBox>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblnamel" runat="server" Text='<%#bind("lname") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Description">
                    <HeaderTemplate>
                        <asp:TextBox ID="txtdes" runat="server"></asp:TextBox>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <%# Eval("description").ToString().Replace("&","</br>")%>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

 --------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class grid : System.Web.UI.Page
{
    string xxx = ConfigurationManager.ConnectionStrings["aaa"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection(xxx);
            SqlCommand cmd = new SqlCommand("select * from Des");
            cmd.Connection = con;
            SqlDataAdapter dap = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            dap.Fill(ds);
            grd.DataSource = ds;
            grd.DataBind();
            //return ds;
           
        }
    }
    protected void ontxtid(object sender, EventArgs e)
    {

        if (grd.HeaderRow != null)
        {
            TextBox txti = (TextBox)grd.HeaderRow.FindControl("txtid");
            SqlConnection con = new SqlConnection(xxx);
            SqlCommand cmd = new SqlCommand("select * from des where id like '" + txti.Text + "%'");
            cmd.Connection = con;
            SqlDataAdapter dap = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            dap.Fill(ds);
            grd.DataSource = ds;
            grd.DataBind();

        }
    }
    protected void ontxtfname(object sender, EventArgs e)
    {

        if (grd.HeaderRow != null)
        {
            TextBox txtf = (TextBox)grd.HeaderRow.FindControl("txtfname");
            //TextBox txti = (TextBox)grd.HeaderRow.FindControl("txtid");
            SqlConnection con = new SqlConnection(xxx);
            SqlCommand cmd = new SqlCommand("select * from des where fname like '" + txtf.Text + "%'");
            cmd.Connection = con;
            SqlDataAdapter dap = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            dap.Fill(ds);
            grd.DataSource = ds;
            grd.DataBind();
        }
    }
    protected void ontxtlname(object sender, EventArgs e)
    {

        if (grd.HeaderRow != null)
        {
            TextBox txtl = (TextBox)grd.HeaderRow.FindControl("txtlname");
            //TextBox txti = (TextBox)grd.HeaderRow.FindControl("txtid");
            SqlConnection con = new SqlConnection(xxx);
            SqlCommand cmd = new SqlCommand("select * from des where lname like '" + txtl.Text + "%'");
            cmd.Connection = con;
            SqlDataAdapter dap = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            dap.Fill(ds);
            grd.DataSource = ds;
            grd.DataBind();
        }
    }

 
}
 ----------------------------------------------------------------------------------
<connectionStrings>
              <add name="aaa" connectionString="Data Source=KCLINK-45-PC\SQLEXPRESS;Initial Catalog=naresh;Integrated Security=True" providerName="System.Data.SqlClient"/>
       </connectionStrings>
       <system.web>
<connectionStrings>

----------------------------------------------------------------------------
CREATE TABLE [dbo].[des](
      [id] [int] IDENTITY(1,1) NOT NULL,
      [fname] [varchar](50) NULL,
      [lname] [varchar](50) NULL,
      [description] [varchar](200) NULL,
 CONSTRAINT [PK_des] PRIMARY KEY CLUSTERED
(
      [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO