如何使用asp.net在gridview中突出显示搜索结果? [英] How to highlight search results in gridview using asp.net?
问题描述
我正在使用搜索框根据搜索文本对gridview进行排序.我想在输入到文本框中的gridview中突出显示匹配的文本. 这是我的aspx页面-
I am using a search box to sort my gridview according to the search text. I want to highlight the matching text in the gridview entered into the textbox. This is my aspx page-
<table>
<tr>
<td>
<asp:TextBox ID="TextBox1" runat="server" Width="167px">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Submit" Width="116px" />
</td>
</tr>
<tr>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</tr>
</table>
后面的代码
public void bind()
{
dt = g1.return_dt("select * from tbl1 where id is not null "
+ Session["Name"] + " order by compname ");
if (dt.Rows.Count > 0)
{
adsource = new PagedDataSource();
adsource.DataSource = dt.DefaultView;
adsource.PageSize = 10;
adsource.AllowPaging = true;
adsource.CurrentPageIndex = pos;
btnfirst.Enabled = !adsource.IsFirstPage;
btnprevious.Enabled = !adsource.IsFirstPage;
btnlast.Enabled = !adsource.IsLastPage;
btnnext.Enabled = !adsource.IsLastPage;
GridView1.DataSource = adsource;
GridView1.DataBind();
}
else
{
GridView1.DataSource = null;
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
Session["Name"] = string.Format("and compname like '%{0}%' or productcategory like '%{0}%' or country like '%{0}%'");
}
else
{
Session["Name"] = null;
}
}
请指导我该怎么做.
推荐答案
您可以通过两种方式进行操作:
You can do it in two ways:
-
修改数据表相关列中的文本. (在每列中查找文本,并在该文本周围添加一个带有css类的跨度.)这是一个示例:
Modify the text in the relevant columns of you data table. (Find the text in each column and add an span with css class surrounding that text.) Here is a sample:
foreach(DataRow dr in dt.Rows)
{
foreach(DataColumn dc in dt.Columns)
{
string s = Convert.ToString(dr[dc.ColumnName]);
s = s.Replace("Your Search Text","<span style='color:RED'>Your Search Text</span>");
dr[dc.ColumnName] = s;
}
}
foreach(DataRow dr in dt.Rows)
{
foreach(DataColumn dc in dt.Columns)
{
string s = Convert.ToString(dr[dc.ColumnName]);
s = s.Replace("Your Search Text","<span style='color:RED'>Your Search Text</span>");
dr[dc.ColumnName] = s;
}
}
您可以使用javascript进行相同操作,并让浏览器完成所有艰苦的工作. (有关示例,请参见此链接.)
You can use javascript for the same and let the browser do all the hardwork. (Refer this link for a sample.)
希望这会有所帮助.
这篇关于如何使用asp.net在gridview中突出显示搜索结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!