ASP.Net Internal Page Search

Introduction

This article describes how to search your internal site pages in ASP.Net.

Description

Here I use an example to search a Help file in a site.

To create this application you need a third-party DLL file to be referenced by the application:

  • SearchDotNetC.dll

You can download it from the source code attached to this page.

Design

Create a page Search.aspx. Add a TextBox, a button and a GridView.

Now design your screen as in the following screen.

Internal-Page-Search-in-ASP-Net1.jpg

Or you can copy the following source code:

  1. <form id="form1" runat="server">      
  2.    <div>  
  3.         <table>  
  4.             <tr>  
  5.                 <td>  
  6.                     <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>  
  7.                 </td>  
  8.                 <td>  
  9.                     <asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" />  
  10.                 </td>  
  11.             </tr>  
  12.             <tr>  
  13.                 <td colspan="2">  
  14.                     <asp:Panel ID="pnlSearchResults" runat="server" Visible="False" Width="99%">  
  15.                         <table class="SearchStatus" width="100%" align="center">  
  16.                             <tr>  
  17.                                 <td>  
  18.                                     Searched the site for <asp:Label ID="lblSearchWords" runat="server" Font-Bold="True"></asp:Label>.  
  19.                                     <br />  
  20.                                     <asp:Label ID="lblFilesFound" runat="server" Font-Bold="True">Label</asp:Label> Files  
  21.                                     found  
  22.                                 </td>  
  23.                             </tr>  
  24.                             <tr>  
  25.                                 <td>  
  26.                                     <asp:GridView ID="gvSearch" runat="server" AutoGenerateColumns="false" ShowHeader="False"  
  27.                                         GridLines="None">  
  28.                                         <Columns>  
  29.                                             <asp:TemplateField>  
  30.                                                 <ItemTemplate>  
  31.                                                     <%# DisplayTitle(((System.Data.DataRowView)Container.DataItem)["Title"].ToString(), ((System.Data.DataRowView)Container.DataItem)["Path"].ToString())%>  
  32.                                                 </ItemTemplate>  
  33.                                             </asp:TemplateField>  
  34.                                         </Columns>  
  35.                                     </asp:GridView>  
  36.                                 </td>  
  37.                             </tr>  
  38.                             <tr>  
  39.                                 <td height="18" style="padding-top: 10px;">  
  40.                                     Searched <asp:Label ID="lblTotalFiles" runat="server" Font-Bold="True"></asp:Label> documents  
  41.                                     in total.  
  42.                                </td>  
  43.                             </tr>  
  44.                         </table>  
  45.                     </asp:Panel>  
  46.                 </td>  
  47.             </tr>  
  48.         </table>  
  49.     </div>  
  50. </form> 

Now go to the code view.

Next add a reference of the following  DLL to your website:

  • SearchDotNetC.dll

And write the following code in the .cs file:

  1. using System;  
  2. using System.Configuration;  
  3. using System.Collections.Generic;  
  4. using System.Data;  
  5. using System.Linq;  
  6. using System.Web.UI;  
  7. using System.Web.UI.WebControls;  
  8. using SearchDotNetC;  
  9. using System.IO;  
  10. public partial class Search : System.Web.UI.Page  
  11. {  
  12.     private Searchs.UserSearch searchSite;  
  13.     private void DisplayContent()  
  14.     {  
  15.         if ((searchSite.PageDataset != null))  
  16.         {  
  17.             pnlSearchResults.Visible = true;  
  18.             lblSearchWords.Text = searchSite.SearchWords;  
  19.             if (ViewState["SortExpression"] == null)  
  20.                 ViewState["SortExpression"] = "MatchCount Desc";  
  21.             BindDataGrid(ViewState["SortExpression"].ToString());  
  22.             lblTotalFiles.Text = searchSite.TotalFilesSearched.ToString();  
  23.             lblFilesFound.Text = searchSite.TotalFilesFound.ToString();  
  24.         }  
  25.     }  
  26.     private void BindDataGrid(string strSortField)  
  27.     {  
  28.         DataView dvwPages = default(DataView);  
  29.         dvwPages = searchSite.PageDataset.Tables["Pages"].DefaultView;  
  30.         dvwPages.Sort = strSortField;  
  31.         gvSearch.DataSource = dvwPages;  
  32.         gvSearch.DataBind();  
  33.     }  
  34.     //Here adding a hyperlink  
  35.     protected string DisplayTitle(string Title, string Path)  
  36.     {  
  37.         return string.Format("<A href='{1}' style='color: #017fbc'  target='FrameMain'>{0}</a>", Title, Path);  
  38.     }  
  39.     private string DisplayPath(string Path)  
  40.     {  
  41.         return string.Format("{0}", Request.ServerVariables["HTTP_HOST"]);  
  42.     }  
  43.     private Searchs.UserSearch SearchSite(string strSearch)  
  44.     {  
  45.         Searchs.UserSearch srchSite = new Searchs.UserSearch();  
  46.         //Read in all the search words into one variable  
  47.         srchSite.SearchWords = strSearch;  
  48.         srchSite.Search(Server.MapPath("~/Help/files"));  
  49.         return srchSite;  
  50.     }  
  51.     protected void Page_Load(object sender, EventArgs e)  
  52.     {  
  53.         if ((searchSite == null))  
  54.             searchSite = (Searchs.UserSearch)Session["Site"];  
  55.     }  
  56.     protected void Button1_Click(object sender, EventArgs e)  
  57.     {  
  58.         string strSearchWords = null;  
  59.         //If there is no words entered by the user to search for then dont carryout the file search routine  
  60.         strSearchWords = txtSearch.Text;  
  61.         pnlSearchResults.Visible = false;  
  62.         if (!strSearchWords.Equals(""))  
  63.         {  
  64.             Searchs.Site.ApplicationPath = string.Format("http://{0}{1}", Request.ServerVariables["HTTP_HOST"], Request.ApplicationPath);  
  65.             searchSite = SearchSite(strSearchWords);  
  66.             Session["Site"] = searchSite;  
  67.             DisplayContent();  
  68.         }  
  69.     }  
  70. }   

Now go to Solution Explorer and add another page Default.aspx.

On this page, I added a Frameset to show search.aspx in one side and the page you want to see in the other side.

For design copy the following code:

  1. <head id="Head1" runat="server">  
  2.     <title></title>  
  3.      <script type="text/javascript">  
  4.     <!--  
  5.              var sTopic = "";  
  6.          if (top.location.href.lastIndexOf("?") > 0)  
  7.              sTopic = top.location.href.substring(top.location.href.lastIndexOf("?") + 1, top.location.href.length);  
  8.          if (sTopic == "") sTopic = "1stpage.aspx";  
  9.          document.write('<frameset cols="300,*">');  
  10.          document.write('<frame src="Search.aspx" name="Frameleft">');  
  11.          document.write('<frame src="' + sTopic + '" name="FrameMain">');  
  12.          document.write('</frameset>');  
  13.     //-->  
  14.     </script>  
  15. </head>  
  16. <body>  
  17.     <form id="form1" runat="server">  
  18.     <div>  
  19.     </div>  
  20.     </form>  
  21. </body>  
  22. </html> 

Now for searching purposes create a folder Help and within that folder create some HTML pages like in the following screen:

Internal-Page-Search-in-ASP-Net2.jpg

The important thing is to open web.config and add the following line within <appSetting>:

  1. <appSettings>  
  2.       <add key="FilesTypesToSearch" value=".htm,.html,.asp,.shtml,.aspx"/>  
  3.       <add key="DynamicFilesTypesToSearch" value=".asp,.shtml,.aspx" =""/>  
  4.       <add key="BarredFolders"value="aspnet_client,_private,_vti_cnf,_vti_log,_vti_pvt,_vti_script,_vti_txt,cgi_bin,_bin,bin,_notes,images,scripts"/>  
  5.       <add key="BarredFiles" value="localstart.asp,iisstart.asp,AssemblyInfo.vb,Global.asax,Global.asax.vb,SiteSearch.aspx" =""/>  
  6.       <add key="EnglishLanguage" value="True" =""/>  
  7.       <add key="Encoding" value="utf-8" =""/>           
  8. </appSettings>   

Set Default.aspx as the Start Up page and build your application. 

Now enter the title of the page you want to search in the TextBox and click on the search Button. If the page is present then it will show the page in the grid.

Internal-Page-Search-in-ASP-Net3.jpg

Now click on the Hyperlink in the grid will show the page in the right frame.

Internal-Page-Search-in-ASP-Net4.jpg

You can search any page within the Help folder by typing its name.


Similar Articles