Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, March 25, 2013

Highlighting Text within a GridView When Performing Search Results from SharePoint



Highlighting Text within a GridView When Performing Search Results from SharePoint
Ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="HKGridViewUserControl.ascx.cs" Inherits="HKGridView.HKGridView.HKGridViewUserControl" %>
<style type="text/css">
.HighlightKeyWord
{
    text-decoration: none;
    color:white;
    background:green;
    font-weight:bold;
}
</style>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table width="100%">
<tr><td colspan="3"><asp:Label ID="lblMessage" runat="server"></asp:Label></td></tr>
<tr>
<td width="100px">Enter Keyword</td>
<td width="100px"><asp:TextBox ID="txtSearchKeyWord" runat="server"></asp:TextBox></td>
<td><asp:Button ID="btnSearch" runat="server" Text="Search"
        onclick="btnSearch_Click" /></td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="dgvKeywordSearch" runat="server" Width="100%"
    AutoGenerateColumns="False" CellPadding="4" EnableModelValidation="True"
    ForeColor="#333333" GridLines="Both"
        onrowdatabound="dgvKeywordSearch_RowDataBound">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="SharePoint List Owner">
<ItemTemplate>
<asp:Label ID="lblListOwner" runat="server" Text='<%# HighlightSearchKeyWord(Eval("ListOwners").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="List Name">
<ItemTemplate>
<asp:Label ID="lblListName" runat="server" Text='<%# HighlightSearchKeyWord(Eval("Title").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
Ascx.Cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;
using System.Text.RegularExpressions;

namespace HKGridView.HKGridView
{
    public partial class HKGridViewUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public string HighlightSearchKeyWord(string strSearchKeyword)
        {
            string Search_Str = txtSearchKeyWord.Text.ToLower();
            Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
            return RegExp.Replace(strSearchKeyword, new MatchEvaluator(ReplaceSearchKeyWords));
        }

        public string ReplaceSearchKeyWords(Match mKeyWord)
        {
            return "<span class='HighlightKeyWord'>" + mKeyWord.Value + "</span>";
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy><Where><Or><Contains><FieldRef Name='ListOwners' /><Value Type='Lookup'>" + txtSearchKeyWord.Text.ToLower() + "</Value></Contains><Contains><FieldRef Name='Title' /><Value Type='Text'>" + txtSearchKeyWord.Text.ToLower() + "</Value></Contains></Or></Where>";
                    SPListItemCollection myColl = SPContext.Current.Web.Lists["Lists Log"].GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        dgvKeywordSearch.DataSource = myColl.GetDataTable();
                        dgvKeywordSearch.DataBind();
                    }
                });
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.ToString();
            }
        }

        protected void dgvKeywordSearch_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (txtSearchKeyWord.Text != "")
                {
                    HighlightSearchKeyWord(txtSearchKeyWord.Text);
                }
            }
        }
    }
}

No comments:

Post a Comment