Search records in GridView(C sharp ASP .NET)

15 四月, 2009 at 6:42 下午 發表留言

今日係要做search 同埋filter 既function, 找到了一個很不錯的sample, 是有用到ajax 的……很好…. 試過d code, 真的不錯呢~~

但只是做了search function, filter 好像不能完成呢, 而且一加上paging 及sorting function, 就出現了很多問題。(我想這些問題都是因為我不懂的問題而引申出來的….)

http://www.aspdotnetcodes.com/Asp.Net_GridView_Filter.aspx

我想做這個, 但是~~~做不到呢, 沒有sample 我就快死了~~哈哈~~

明天再研究

Quoted from http://csharpdotnetfreak.blogspot.com/2008/12/search-records-in-gridview-and.html:
Search records in GridView and highlight result using C sharp ASP .NET and AJAX

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

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN"
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
<
title>Grid Search example</title>
<
style type="text/css">
.highlight {
text-decoration:none; font-weight:bold;
color:black; background:yellow;}
</style>
</
head>
<
body>
<
form id="form1″ runat="server">
<
asp:ScriptManager ID="ScriptManager1″
   runat="server" />

<div>
Enter first name to search:
</div>
<
div>
<
asp:TextBox ID="txtSearch" runat="server"
   AutoPostBack="True"
   OnTextChanged="txtSearch_TextChanged">
</
asp:TextBox>&nbsp;
</div>
<
div>
<
asp:UpdatePanel ID="UpdatePanel1″
   runat="server">
<
ContentTemplate>
<
asp:GridView ID="grdSearch"
   runat="server"
   AutoGenerateColumns="false">
<
Columns>
<
asp:TemplateField HeaderText="FirstName">
<
ItemTemplate>
<
asp:Label ID="lblFirstName"
   runat="server"
Text='<%# Highlight(Eval(“FirstName").ToString()) %>’>
</
asp:Label>
</
ItemTemplate>
</
asp:TemplateField>
<
asp:TemplateField HeaderText="LastName">
<
ItemTemplate>
<
asp:Label ID="lblLastName" runat="server"
Text='<%#(Eval(“LastName")) %>’></asp:Label>
</
ItemTemplate>
</
asp:TemplateField>
<
asp:TemplateField HeaderText="Location">
<
ItemTemplate>
<
asp:Label ID="lblLocation" runat="server"
Text='<%#(Eval(“Location")) %>’></asp:Label>
</
ItemTemplate>
</
asp:TemplateField>
</
Columns>
</
asp:GridView>
</
ContentTemplate>
<
Triggers>
<
asp:AsyncPostBackTrigger ControlID="txtSearch"
   EventName="TextChanged" />
</
Triggers>
</
asp:UpdatePanel>

</div>

</form>
</
body>
</
html>

And it code behind

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
string strConnection =
ConfigurationManager.AppSettings["ConnectionString"];
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
  BindGrid();
}
}

private DataTable GetRecords()
{
SqlConnection conn = new SqlConnection(strConnection);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from Employees";
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
DataSet objDs = new DataSet();
dAdapter.Fill(objDs);
return objDs.Tables[0];

}
private void BindGrid()
{
DataTable dt = GetRecords();
if (dt.Rows.Count > 0)
{
  grdSearch.DataSource = dt;
  grdSearch.DataBind();
}
}
private void SearchText()
{
DataTable dt = GetRecords();
DataView dv = new DataView(dt);
string SearchExpression = null;
if (!String.IsNullOrEmpty(txtSearch.Text))
{
  SearchExpression = string.Format("{0} '%{1}%'",
      grdSearch.SortExpression, txtSearch.Text);

}
dv.RowFilter = "FirstName like" + SearchExpression;
grdSearch.DataSource = dv;
grdSearch.DataBind();

}
public string Highlight(string InputTxt)
{
string Search_Str = txtSearch.Text.ToString();
// Setup the regular expression and add the Or operator.
Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(),
             RegexOptions.IgnoreCase);

// Highlight keywords by calling the 
//delegate each time a keyword is found.
return RegExp.Replace(InputTxt,
     new MatchEvaluator(ReplaceKeyWords));

// Set the RegExp to null.
RegExp = null;

}

public string ReplaceKeyWords(Match m)
{

return "<span class=highlight>" + m.Value + "</span>";

}

protected void txtSearch_TextChanged(object sender, EventArgs e)
{
SearchText();
}
}

Entry filed under: AJAX, ASP.NET. Tags: , , .

PS CS4 與ImageReady合併了 load data in gridview via ADO.NET

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Google photo

您的留言將使用 Google 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

Trackback this post  |  Subscribe to the comments via RSS Feed


近期文章


%d 位部落客按了讚: