|
|
|
อยากทำไฮไลท์ คำที่ต้องการค้นหาค่ะ ใครเคยทำหรือมีข้อแนะนำ ขอปรึกษาหน่อยค่ะเพิ่งหัดเขียนค่ะขอบคุณล่วงหน้าเลยน่ะค่ะ |
|
|
|
|
|
|
|
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace web_help_Kms
{
public partial class subject_ct : System.Web.UI.Page
{
string strKeyWord;
protected void Page_Load(object sender, EventArgs e)
{
strKeyWord = txtKeyWord.Text;
}
void BindData()
{
SqlConnection objConn = new SqlConnection();
SqlCommand objCmd = new SqlCommand();
SqlDataAdapter dtAdapter = new SqlDataAdapter();
DataSet ds = new DataSet();
String strConnString, strSQL;
strConnString = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;
strSQL = "SELECT * FROM tb_content_topic WHERE (ct_name like '%" + strKeyWord + "%') ";
objConn.ConnectionString = strConnString;
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds);
//*** BindData to GridView ***//
myGridView.DataSource = ds;
myGridView.DataBind();
dtAdapter = null;
objConn.Close();
objConn = null;
}
public void myGridView_RowDataBound(Object s, GridViewRowEventArgs e)
{
// NO
Label lblNum = e.Row.FindControl("lblNum") as Label;
if (lblNum != null)
{
lblNum.Text = Convert.ToString((e.Row.RowIndex + 1) + (myGridView.PageIndex * myGridView.PageSize));
}
//*** ct_name ***//
Label lblct_name = (Label)(e.Row.FindControl("lblct_name"));
if (lblct_name != null)
{
lblct_name.Text = (string)DataBinder.Eval(e.Row.DataItem, "ct_name ");
}
//*** detail ***//
Label lblct_post_detail = (Label)(e.Row.FindControl("lblct_post_detail"));
if (lblct_post_detail != null)
{
lblct_post_detail.Text = (string)DataBinder.Eval(e.Row.DataItem, "ct_post_detail");
}
//*** date ***//
Label lbldate = (Label)(e.Row.FindControl("lbldate"));
if (lbldate != null)
{
DateTime tt = (DateTime)DataBinder.Eval(e.Row.DataItem, "ct_post_date");
lbldate.Text = tt.ToString("dd/MM/yyyy");//(string)DataBinder.Eval(e.Row.DataItem, "ct_post_date");
}
}
public void btnSearch_Click(Object sender, EventArgs e)
{
BindData();
}
public void ShowPageCommand(Object s, GridViewPageEventArgs e)
{
myGridView.PageIndex = e.NewPageIndex;
BindData();
}
}
}
Tag : .NET, Ms SQL Server 2008, Ajax, Web (ASP.NET), C#
|
|
|
|
|
|
Date :
2011-06-20 09:25:00 |
By :
TuckySIS |
View :
1157 |
Reply :
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คืออยากจะทำแบบ IE ค่ะ เมื่อค้นหาคำแล้ว คำที่ค้นหาพื้นหลังคำนั้นจะเป็นสีเหลือง หรือสีเทาค่ะ แต่หนูไม่รู้ว่าทำอย่างไรค่ะ
|
|
|
|
|
Date :
2011-06-20 11:59:05 |
By :
TuckySIS |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองทำได้แล้วค่ะ แต่ต้องเอาไปพัฒนาต่อให้ค้นหาข้อมูลได้จาก แหล่งอื่นอีก อันนี้ที่เราทำได้ ลองนำไปศึกษษแล้วแลกเปลี่ยนกันได้น่ะค่ะ
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridViewSearchHeighlight.aspx.cs" Inherits="web_help_Kms.GridViewSearchHeighlight" %>
<!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>
<title>Tucky_TestKM
</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" />
<asp:UpdatePanel ID="UpdatePanel1"
runat="server">
<ContentTemplate>
<div>
<asp:GridView ID="grdSearch" runat="server"
BackColor="white" BorderColor="Aqua"
AutoGenerateColumns="false" ShowFooter="true"
OnRowCommand="grdSearch_RowCommand" AllowPaging="True" PageSize="5">
<FooterStyle BackColor="AliceBlue"
ForeColor="AntiqueWhite" />
<PagerStyle BackColor="#99CCCC"
ForeColor="#003399" HorizontalAlign="Left" />
<HeaderStyle BackColor="#003399"
Font-Bold="True" ForeColor="#CCCCFF" />
<Columns>
<asp:TemplateField HeaderText="หัวข้อองค์ความรู้">
<ItemTemplate>
<asp:Label ID="lblct_name" runat="server"
Text='<%# Highlight(Eval("ct_name").ToString()) %>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtSearch" runat="server"
Width="70px"></asp:TextBox>
<asp:Button ID="btnSearch"
CommandName="Search"
runat="server" Text="Search"
Width="60px" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ct_post_detail"
HeaderText="รายละเอียดองค์ความรู้" />
</Columns>
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1"
runat="server">
<ProgressTemplate>
<br />
<img src="Pic/ajax.gif"
alt="Searchig" />
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<br />
</form>
</body>
</html>
|
|
|
|
|
Date :
2011-06-21 13:42:37 |
By :
TuckySIS |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
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.Text.RegularExpressions;
using System.Data.SqlClient;
namespace web_help_Kms
{
public partial class GridViewSearchHeighlight : System.Web.UI.Page
{
static DataTable dt;
string strConnection =
ConfigurationManager.AppSettings["ConnectionString"];
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt = GetRecords();
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 tb_content_topic";
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
DataSet objDs = new DataSet();
dAdapter.Fill(objDs);
return objDs.Tables[0];
}
private void BindGrid()
{
if (dt.Rows.Count > 0)
{
grdSearch.DataSource = dt;
grdSearch.DataBind();
}
}
private void SearchText(string strSearchText)
{
//DataTable dt = GetRecords();
DataView dv = new DataView(dt);
string SearchExpression = null;
if (!String.IsNullOrEmpty(strSearchText))
{
SearchExpression =
string.Format("{0} '%{1}%'",
grdSearch.SortExpression, strSearchText);
}
dv.RowFilter = "ct_post_detail like" + SearchExpression;
grdSearch.DataSource = dv;
grdSearch.DataBind();
}
protected void grdSearch_RowCommand
(object sender, GridViewCommandEventArgs e)
{
System.Threading.Thread.Sleep(500);
if (e.CommandName == "Search")
{
lblMessage.Text = "";
TextBox txtGrid =
(TextBox)grdSearch.FooterRow.FindControl("txtSearch");
if (txtGrid.Text != "")
{
SearchText(txtGrid.Text);
}
else
lblMessage.Text = "Please enter search keyword";
}
}
public string Highlight(string InputTxt)
{
GridViewRow gvr = grdSearch.FooterRow;
if (gvr != null)
{
TextBox txtExample =
(TextBox)grdSearch.FooterRow.FindControl("txtSearch");
if (txtExample.Text != null)
{
string strSearch = txtExample.Text;
//string Search_Str = txtGrid.Text.ToString();
// Setup the regular expression
//and add the Or operator.
Regex RegExp =
new Regex(strSearch.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;
}
else
return InputTxt;
}
else
{
return InputTxt;
}
}
public string ReplaceKeyWords(Match m)
{
return "<span class=highlight>" + m.Value + "</span>";
}
}
}
|
|
|
|
|
Date :
2011-06-21 13:43:22 |
By :
TuckySIS |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|