yeasir007

Tuesday, December 04, 2012

Search in gridview in asp.net


Search Item in  Gridview in asp.net : 

download source code : download
demo:




Search in asp.net gridView is nothing but apply filterExpression and add filter Parameter with control id in gridview datasource. Suppose my gridview datasource id is "odsBonusDetails" and a search text box is "txtSearch". Let suppose we want to search grid by employee name which data field id is "strEmployeeName" and that's why i used filter expression "strEmployeeNamelike '%{0}%'" .
     

<asp:ObjectDataSource ID="odsBonusDetails" runat="server" OldValuesParameterFormatString="original_{0}"
  SelectMethod="GetEmployeesBonusDetails" TypeName="SearchingGrid.DAL.Gateway.BonusDetails_DLL"
  FilterExpression="strEmployeeNamelike '%{0}%'">
  <FilterParameters>
  <asp:ControlParameter Name="strEmployeeName" ControlID="txtSearch" PropertyName="Text" />
  </FilterParameters>
  </asp:ObjectDataSource>

If you want to add more data key for searching that should be like that.
<asp:ObjectDataSource ID="odsBonusDetails" runat="server" OldValuesParameterFormatString="original_{0}"
  SelectMethod="GetEmployeesBonusDetails" TypeName="SearchingGrid.DAL.Gateway.BonusDetails_DLL"
  FilterExpression="strEmployeeName like '%{0}%' or strUnitName like '%{0}%'">
  <FilterParameters>
  <asp:ControlParameter Name="strEmployeeName" ControlID="txtSearch" PropertyName="Text" />
  <asp:ControlParameter Name="strUnitName" ControlID="txtSearch" PropertyName="Text" />
  </FilterParameters>
 </asp:ObjectDataSource>

For random searching due to key up i have added following code bellow in page load. And I have also added some other code for highlighting searched text. 
txtSearch.Attributes.Add("onkeyup", "setTimeout('__doPostBack(\'" + txtSearch.ClientID.Replace("_", "$") + "\',\'\')', 0);");

It's very easy to implement. Just download source code and try to implement.Thank you.

No comments:

Post a Comment

Thanks for your comments