In this code you will see you how you can search records preset in a gridview on button click event.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"Inherits="ProjectDemo_Asp.et.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>Search GridView Record on Button Click By Using C#.Net in Asp.Net</title>
</head>
<body>
<form id="form1" runat="server">
Search By Title :
<asp:TextBox ID="txtsearch" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Search" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"EmptyDataText="There are no data records to display."
Width="500px" BorderStyle="Solid" ShowFooter="True">
<Columns>
<asp:BoundField DataField="author_name" HeaderText="NAME" />
<asp:BoundField DataField="publisher_name" HeaderText="PUB. NAME" />
<asp:BoundField DataField="title" HeaderText="TITLE" />
<asp:BoundField DataField="publication_year" HeaderText="PUB. YEAR"/>
</Columns>
<HeaderStyle BackColor="#66CCFF" />
</asp:GridView>
</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.Data;
using System.Data.SqlClient;
namespace DemoGrid {
public partial class serchfrid: System.Web.UI.Page
{
public string connectionstring = "Data Source=NACH_UAT\\NACH;Initial Catalog=ReportTracking;User ID=sa;Password=admin@123";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable _objdt = new DataTable();
_objdt = GetDataFromDataBase("");
if (_objdt.Rows.Count > 0)
{
GridView1.DataSource = _objdt;
GridView1.DataBind();
}
}
}
private string addcondition(string str, string qs)
{
string qsqs1 = qs.ToLower();
if (qs1.Contains(" where "))
{
qsqs = qs + " and " + str;
}
else
{
qsqs = qs + " where " + str;
}
return qs;
}
public DataTable GetDataFromDataBase(string searchtext)
{
DataTable _objdt = new DataTable();
string querystring = "";
querystring = "select * from tblProductMaster";
if (TextBox1.Text != null && txtcity.Text != null)
{
//// querystring += " where ProductName like '%" + TextBox1.Text + "%';";
//querystring = addcondition(" ProductName between " + (TextBox1.Text) + " or " + txtcity.Text.Trim() + ")", querystring);
querystring = addcondition(" SELECT ProductName, Active,Addedby FROM tblProductMaster WHERE ProductName=" + TextBox1.Text + "UNION ALL SELECT ProductName, Active,Addedby FROM tblProductMaster WHERE ProductName=" + txtcity.Text + " ORDER BY ProductName", querystring);
// querystring = addcondition(" DATEADD(dd, 0, DATEDIFF(dd, 0, CallTime )) between convert (datetime,'" + (TextBox1.Text) + "',103) and convert (datetime,'" + txtcity.Text.Trim() + "',103)", querystring);
}
//if (querystring != "")
//{
// querystring += " where title like '%" + txtsearch.Text + "%';";
//}
SqlConnection _objcon = new SqlConnection(connectionstring);
SqlDataAdapter _objda = new SqlDataAdapter(querystring, _objcon);
_objcon.Open();
_objda.Fill(_objdt);
return _objdt;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable _objdt = new DataTable();
// _objdt = GetDataFromDataBase(txtcity.Text);
if (_objdt.Rows.Count > 0)
{
GridView1.DataSource = _objdt;
GridView1.DataBind();
}
}
}
}