如何使用asp.net在gridview中突出显示搜索结果
本文关键字:显示 搜索结果 gridview 何使用 asp net | 更新日期: 2023-09-27 17:49:24
我正在使用一个搜索框来排序我的网格视图根据搜索文本。我想突出显示输入到文本框中的网格视图中的匹配文本。这是我的aspx页面-
<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;
}
}
请告诉我怎么做。
有两种方法:
-
修改数据表中相关列的文本。(找到每一列的文本,并在文本周围添加一个css类的span。)下面是一个示例:
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做同样的事情,让浏览器做所有的工作。(参考此链接获取示例)