Merhaba arkadaşlar bu makalemizde GridView nesnesinde ajax kullanarak kayit arayacagiz. Bulunan kaydi highlight olarak gosterimini sagliyacagiz. Sayfamiza 1 adet GridView , Javascript ve UpdatePanel ekleyecegiz. GridView nesnesinin AutoGenerateColumns="False" ozelligini false yapacagiz.
Sekil 1
Sekil 2
Sekil 3
Sekil 4
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace aspnet_gridview_search_with_ajax
{
public partial class WebForm1 : System.Web.UI.Page
{
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
MySqlConnection con = new MySqlConnection("Server=localhost;Database=dbemployee;Uid=root;Pwd='2344';AllowUserVariables=True;UseCompression=True;");
con.Open();
string cmd = "Select * From staff";
MySqlDataAdapter dAdapter = new MySqlDataAdapter(cmd, con);
DataSet ds = new DataSet();
dAdapter.Fill(ds);
dt = ds.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}
private void Search(string strSearch)
{
lblAlert.Visible = false;
MySqlConnection con = new MySqlConnection("Server=localhost;Database=dbemployee;Uid=root;Pwd='2344';AllowUserVariables=True;UseCompression=True;");
con.Open();
string cmd = "Select * From staff";
MySqlDataAdapter dAdapter = new MySqlDataAdapter(cmd, con);
DataSet ds = new DataSet();
dAdapter.Fill(ds);
dt = ds.Tables[0];
DataView dv = new DataView(dt);
string searchWord = null;
if (!String.IsNullOrEmpty(strSearch))
{
searchWord = string.Format("{0} '%{1}%'",
GridView1.SortExpression, strSearch);
}
dv.RowFilter = "FirstName like" + searchWord;
if (dv.Count > 0)
{
GridView1.DataSource = dv;
GridView1.DataBind();
Button backbutton = (Button)GridView1.FooterRow.FindControl("btnBack");
backbutton.Visible = true;
}
else
{
lblAlert.Visible = true;
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
System.Threading.Thread.Sleep(2000);
if (e.CommandName == "Search")
{
TextBox aranan = (TextBox)GridView1.FooterRow.FindControl("txtSearch");
Search(aranan.Text);
}
else if (e.CommandName == "Back")
{
BindData();
}
}
public string Highlight(string InputTxt)
{
GridViewRow gvr = GridView1.FooterRow;
if (gvr != null)
{
TextBox txtFind = (TextBox)GridView1.FooterRow.FindControl("txtSearch");
if (txtFind.Text != null)
{
string strSearch = txtFind.Text;
Regex willAppear = new Regex(strSearch.Replace(" ", "|").Trim(),
RegexOptions.IgnoreCase);
return willAppear.Replace(InputTxt, new MatchEvaluator(Colorize));
}
else
return InputTxt;
}
else
{
return InputTxt;
}
}
public string Colorize(Match m)
{
return "<span class='highlight'>" + m.Value + "</span>";
}
}
}
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="aspnet_gridview_search_with_ajax.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.highlight
{
text-decoration: none;
font-weight: bold;
color: black;
background: yellow;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<asp:Label ID="lblAlert" runat="server" Visible="false" Text="Could not find the records you are looking for. Please try again! Aradiginiz kayit bulunamadi. Lutfen tekrar deneyiniz!"></asp:Label>
<asp:GridView ID="GridView1" runat="server" Width="823px" HorizontalAlign="Center"
AutoGenerateColumns="False"
DataKeyNames="Id" OnRowCommand="GridView1_RowCommand"
ShowFooter="True" Height="323px" CellPadding="4" ForeColor="#333333" GridLines="None" >
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1"
ForeColor="White" Font-Bold="True" />
<PagerStyle BackColor="#2461BF"
ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1"
Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="FirstName" runat="server" Text='<%# Highlight(Eval("FirstName").ToString()) %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtSearch" runat="server"
></asp:TextBox>
<asp:Button ID="btnSearch"
CommandName="Search"
runat="server" Text="Search"
/>
<asp:Button ID="btnBack"
CommandName="Back"
runat="server" Text="Back" Visible="false"
Width="60px" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Contact" HeaderText="Contact" />
<asp:BoundField DataField="Mail" HeaderText="Mail" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
<ProgressTemplate>
<img src="resim/loading.gif" alt="Searching...Araniyor... Please Wait!.. Lutfen Bekleyin!.." />
</ProgressTemplate>
</asp:UpdateProgress>
</div>
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede gorusmek uzere. Bahadir SAHIN
0 comments:
Yorum Gönder