c# - How to highlight search results in gridview using asp.net? -


i using search box sort gridview according search text. want highlight matching text in gridview entered textbox. 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> 

code behind

public void bind() {      dt = g1.return_dt("select  * tbl1 id  not null  "             + session["name"] + "  order  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  '%{0}%' or productcategory '%{0}%' or country '%{0}%'");     }     else     {          session["name"] = null;     } } 

please guide me how can this.

you can in 2 ways:

  1. modify text in relevant columns of data table. (find text in each column , add span css class surrounding text.) here 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; } }

  2. you can use javascript same , let browser hardwork. (refer this link sample.)

hope helps.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -