Это пример того, как искать в сетке и заполнять результаты по этим критериям поиска, используя sqldatasource .....
привязка к сетке .....
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="False" AllowPaging="True"
AllowSorting="true" DataSourceID="dsGridview" Width="540px" PageSize="10">
<Columns>
<asp:BoundField DataField="id" HeaderText="ID" SortExpression="id" />
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<ItemStyle Width="120px" HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lblFirstname" Text='<%# HighlightText(Eval("FirstName")) %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<ItemStyle Width="120px" HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lblLastname" Text='<%# HighlightText(Eval("LastName")) %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Department" HeaderText="Department"
SortExpression="Department" ItemStyle-Width="130px" />
<asp:BoundField DataField="Location" HeaderText="Location"
SortExpression="Location" ItemStyle-Width="130px" />
</Columns>
</asp:GridView>
и источник данных sql выглядит следующим образом ...
<asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT * FROM People"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
FilterExpression="firstname like '%{0}%' or lastname like '%{1}%'">
<FilterParameters>
<asp:ControlParameter Name="firstname" ControlID="txtSearch" PropertyName="Text" />
<asp:ControlParameter Name="lastname" ControlID="txtSearch" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
добавление текстового поля для поиска ..
<asp:TextBox ID="txtSearch" runat="server" />
<asp:ImageButton ID="btnSearch" ImageUrl="images/searchbutton.png" runat="server" />
<asp:ImageButton ID="btnClear" ImageUrl="images/clearbutton.png" runat="server" />
и это код для привязки и ввода текста в текстовое поле
using System.Text.RegularExpressions;
Partial;
class GridviewwithHighlightedSearch : System.Web.UI.Page {
// Create a String to store our search results
private string SearchString = "";
string HighlightText(string InputTxt) {
// This function is called whenever text is displayed in the FirstName and LastName
// fields from our database. If we're not searching then just return the original
// input, this speeds things up a bit
if ((SearchString == "")) {
return InputTxt;
}
else {
// Otherwise create a new regular expression and evaluate the FirstName and
// LastName fields against our search string.
Regex ResultStr;
ResultStr = new Regex(SearchString.Replace(" ", "|"), RegexOptions.IgnoreCase);
return ResultStr.Replace(InputTxt, new MatchEvaluator(new System.EventHandler(this.ReplaceWords)));
}
}
public string ReplaceWords(Match m) {
// This match evaluator returns the found string and adds it a CSS class I defined
// as 'highlight'
return ("<span class=highlight>"
+ (m.ToString + "</span>"));
}
protected void btnClear_Click(object sender, System.Web.UI.ImageClickEventArgs e) {
// Simple clean up text to return the Gridview to it's default state
txtSearch.Text = "";
SearchString = "";
Gridview1.DataBind();
}
protected void btnSearch_Click(object sender, System.Web.UI.ImageClickEventArgs e) {
// Set the value of the SearchString so it gets
SearchString = txtSearch.Text;
}
}
и это стиль css для подсветки ..
и это изображение для приведенного выше вида сетки
<style type="text/css">
.highlight {text-decoration: none;color:black;background:yellow;}
</style>
iнадеюсь, это поможет вам ...