Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    305
    Rep Power
    0

    Simple search problem


    Hi everyone,

    How to build a search function (using asp.net 3.5 with C#) to search the texts/keywords on page1.aspx, page2.aspx, page3.aspx, ..., etc.

    For example,

    page1.aspx contains "cars" and page2.aspx contains "car" and page3.aspx contains "computers".

    Now I search "car" and the search reasults shows: page1.aspx and page2.aspx becuase both page1.aspx and page2.aspx contain "car".

    Now I search "computer" and the search results shows: page3.aspx because page3.aspx contains "computers".

    Thanks,

    May
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    37
    Rep Power
    3
    PLENTY of ways. Maybe go with a streamreader

    http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    London
    Posts
    40
    Rep Power
    15
    Hello,

    This question is a bit confusing.

    Are you trying to search the text (markup) on each physical .aspx page?

    Where does your content come from?

    Does this content data not reside in a database?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    305
    Rep Power
    0
    Originally Posted by Darknite
    Hello,

    This question is a bit confusing.

    Are you trying to search the text (markup) on each physical .aspx page?

    Where does your content come from?

    Does this content data not reside in a database?
    Hi,

    I got the search engine working. And this search engine search the content of all *.aspx files. The problem I am having now is my code is unable to return the first n characters/words of the *.aspx file when the search engine finds the *.aspx file.

    Thanks,

    May
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    London
    Posts
    40
    Rep Power
    15
    Hello May,

    Ok, without seeing any code, its like asking to perform brain surgery in the dark, without any tools
    If you could post your code, then it does indeed make it easier for all
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2008
    Posts
    36
    Rep Power
    7
    Search engines first index the pages in database and then using proper query they find the results. You can also index the contents in database first and then can use full text search feature to get the results.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    305
    Rep Power
    0
    Originally Posted by Darknite
    Hello May,



    If you could post your code, then it does indeed make it easier for all
    No problem, I will post my code tonight.

    Thanks,

    May
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    305
    Rep Power
    0
    Originally Posted by cecilia_2004
    No problem, I will post my code tonight.

    Thanks,

    May
    Below is my code:

    Site_Search_Results.aspx

    Code:
    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Site_Search_Results.aspx.cs" Inherits="Site_Search_Results" %>
    
    
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    
    </asp:Content>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
    
    <br />
    
    <asp:Panel ID="panel_Found" runat="server">
    &nbsp;&nbsp;&nbsp;<b>Search results for :</b> <span style="color:Red;"><%= Request.Params.Get("Search_Keyword").Trim() %></span>
    <br /><br />
    </asp:Panel>
    
    <asp:Panel ID="panel_Not_Found" runat="server">
    
    <table>
    <tr>
    <td>
    &nbsp;&nbsp;&nbsp;
    </td>
    <td>
    Your search - <%= Request.Params.Get("Search_Keyword").Trim() %> - did not match any documents.
    <br /><br />
    Suggestions:
    <br /><br />
    &nbsp;&nbsp;&nbsp; Make sure all words are spelled correctly.<br />
    &nbsp;&nbsp;&nbsp; Try different keywords.<br />
    &nbsp;&nbsp;&nbsp; Try more general keywords.
    <br /><br />
    If you search for chemicals and property information please visit page <a href="chemical_catalog.aspx?page=chemical_catalog">Chemical Catalog</a>.
    
    <br />
    </td>
    </tr>
    </table>
    
    </asp:Panel>
    
    
    
    
    
    <asp:DataGrid id="dgrdPages" CssClass="Grid" HorizontalAlign="Center" Width="98%" CellSpacing="0"
    								BorderWidth="0" ShowHeader="False" AllowPaging="True" PageSize="5" PagerStyle-Mode="NumericPages"
    								AutoGenerateColumns="False" AllowSorting="True" HeaderStyle-CssClass="GridHeader"
    								PagerStyle-CssClass="GridPager" FooterStyle-CssClass="GridFooter" SelectedItemStyle-CssClass="GridSelectedItem"
    								AlternatingItemStyle-CssClass="GridAlternatingItem" ItemStyle-CssClass="GridItem" CellPadding="0"
    								Runat="Server"
                                    OnPageIndexChanged="Grid_Change">
                                    
    								<SelectedItemStyle CssClass="GridSelectedItem"></SelectedItemStyle>
    								<AlternatingItemStyle CssClass="GridAlternatingItem"></AlternatingItemStyle>
    								<ItemStyle CssClass="GridItem"></ItemStyle>
    								<HeaderStyle CssClass="GridHeader"></HeaderStyle>
    								<FooterStyle CssClass="GridFooter"></FooterStyle>
    								<Columns>
    									<asp:TemplateColumn>
    										<ItemTemplate>
                                                    <table>
                                                      <tr>
                                                        <td><%# Eval("Title") %></td>
                                                      </tr>
                                                      <tr>
                                                        <td><%# Eval("Description") %></td>
                                                      </tr>                                                  
                                                      <tr>
                                                      <td>
                                                         <span style="color:Green;"><%# Eval("Url") %></span>
                                                      </td>
                                                      </tr>
                                                    </table>
                                            </ItemTemplate>
                                         </asp:TemplateColumn>
    
                                   </Columns>
    
                               </asp:DataGrid>
    </asp:Content>
    Site_Search_Results.aspx.cs

    The following line in the code below does not work. What I try to do is to get the first 50 characters from the *.aspx file once the keywords found in this *.aspx file.

    dr["Description"] = file.Substring(0, 50); //<== This line does not work.

    // OR dr["Description"] = source.Substring(0, 50); //<== This line does not work.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text.RegularExpressions;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Data.OleDb;
    using System.Net.Mail;
    
    public partial class Site_Search_Results : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
                dgrdPages.DataSource = CreateDataSource();
                dgrdPages.DataBind();
            }
        }
    
    
        private ICollection CreateDataSource()
        {
                 
    
            string search = Request.Params.Get("Search_Keyword").Trim();
    
            string[] files = Directory.GetFiles(Server.MapPath("./"), "*.aspx", SearchOption.AllDirectories);
    
            string dir = Server.MapPath("./");
    
            string[] files1;
            int numFiles;
            files1 = Directory.GetFiles(dir);
            numFiles = files.Length;
    
            DataTable dtg = new DataTable();
            DataColumn dc = null;
    
            dc = new DataColumn("Title", typeof(string));
            dtg.Columns.Add(dc);
    
            dc = new DataColumn("Description", typeof(string));
            dtg.Columns.Add(dc);
    
            dc = new DataColumn("Url", typeof(string));
            dtg.Columns.Add(dc);
    
    
            // begin - search static files
            for (int i = 0; i < numFiles; i++)
            {
                string file = files[i].ToString();
                Search_Static_Files(file, search, dtg, dc);
            }
            // end - search static files
    
    
            DataView dv = new DataView(dtg);
    
            if (dv.Count <= 0)
            {
                panel_Found.Visible = false;
                panel_Not_Found.Visible = true;
                dgrdPages.Visible = false;
    
            }
            else
            {
                panel_Found.Visible = true;
                panel_Not_Found.Visible = false;
                dgrdPages.Visible = true;
            }
    
            return dv;
    
        }
    
    
        public void Grid_Change(Object sender, DataGridPageChangedEventArgs e)
        {
    
            // Set CurrentPageIndex to the page the user clicked.
            dgrdPages.CurrentPageIndex = e.NewPageIndex;
    
            // Rebind the data to refresh the DataGrid control. 
            dgrdPages.DataSource = CreateDataSource();
            dgrdPages.DataBind();
        }
    
        private void Search_Static_Files(String file, String searchText, DataTable dtg, DataColumn dc)
        {
    
            //Declare reader as a new StreamReader with file as the file to use
    
            System.IO.StreamReader reader = new System.IO.StreamReader(file);
    
            //Declare text as the reader reading to the end
    
            String text = reader.ReadToEnd();
    
            int FoundText;
    
            int intSearchLoopCounter;
            string[] sarySearchWord;
            
            char[] del = { ' ' };
            
            sarySearchWord = searchText.Trim().Split(del);        
    
            for (intSearchLoopCounter = 0; intSearchLoopCounter < sarySearchWord.Length; intSearchLoopCounter++)
            {
                FoundText = text.IndexOf(sarySearchWord[intSearchLoopCounter], StringComparison.CurrentCultureIgnoreCase);
    
                if (FoundText > -1)
                {
    
    
                    WebClient x = new WebClient();
                    string source = x.DownloadString(file);
                    string title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
                
                    string s = Server.HtmlEncode(file);
                
                    Uri uri = new Uri(file);
                    string filename = Path.GetFileName(uri.LocalPath);
    
                    string full_url = HttpContext.Current.Request.Url.AbsoluteUri;
                    string url_without_filename = Path.GetDirectoryName(full_url);
    
                    DataRow dr = dtg.NewRow();
                    dr["Title"] = "<a href='" + filename.Trim() + "' style='Color:#1e4d7b'>" + title.Trim() + "</a>"; //url_without_filename.Trim() + "\\" + filename.Trim();
    
                   dr["Description"] = file.Substring(0, 50); //<== This line does not work.
     // OR dr["Description"] = source.Substring(0, 50); //<== This line does not work.
                                    
                    dr["Url"] = url_without_filename.Trim() + "\\" + filename.Trim() + "<br /><br />";
                    dtg.Rows.Add(dr);
                                                 
                    break;
    
                }
    
                else
                {
    
                    //Show that it was not a match
                }
    
            }
    
    
        }
    
    }
    Last edited by cecilia_2004; April 11th, 2012 at 06:13 PM.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    London
    Posts
    40
    Rep Power
    15
    If I've read your code correct,

    the variable "file" is just the url correct?


    If that is the case then this line will not work:

    c# Code:
     
    dr["Description"] = file.Substring(0, 50);


    however this line should work assuming there are actually 50 characters or more:

    c# Code:
     
    dr["Description"] = source.Substring(0, 50);


    have you debugged this, have you stepped through to that point and had a look at the variable "source" at that point?

    What errors messages do you get?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    305
    Rep Power
    0
    Originally Posted by Darknite
    If I've read your code correct,

    the variable "file" is just the url correct?


    If that is the case then this line will not work:

    c# Code:
     
    dr["Description"] = file.Substring(0, 50);


    however this line should work assuming there are actually 50 characters or more:

    c# Code:
     
    dr["Description"] = source.Substring(0, 50);


    have you debugged this, have you stepped through to that point and had a look at the variable "source" at that point?

    What errors messages do you get?
    > the variable "file" is just the url correct?

    Correct.

    >dr["Description"] = source.Substring(0, 50);

    I looked at the variable "source" which contains

    "<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="about.aspx.cs" Inherit..."

    and the code display 

    Thanks,

    May
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    305
    Rep Power
    0
    Originally Posted by cecilia_2004
    Below is my code:

    Site_Search_Results.aspx

    Code:
    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Site_Search_Results.aspx.cs" Inherits="Site_Search_Results" %>
    
    
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    
    </asp:Content>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
    
    <br />
    
    <asp:Panel ID="panel_Found" runat="server">
    &nbsp;&nbsp;&nbsp;<b>Search results for :</b> <span style="color:Red;"><%= Request.Params.Get("Search_Keyword").Trim() %></span>
    <br /><br />
    </asp:Panel>
    
    <asp:Panel ID="panel_Not_Found" runat="server">
    
    <table>
    <tr>
    <td>
    &nbsp;&nbsp;&nbsp;
    </td>
    <td>
    Your search - <%= Request.Params.Get("Search_Keyword").Trim() %> - did not match any documents.
    <br /><br />
    Suggestions:
    <br /><br />
    &nbsp;&nbsp;&nbsp; Make sure all words are spelled correctly.<br />
    &nbsp;&nbsp;&nbsp; Try different keywords.<br />
    &nbsp;&nbsp;&nbsp; Try more general keywords.
    <br /><br />
    If you search for chemicals and property information please visit page <a href="chemical_catalog.aspx?page=chemical_catalog">Chemical Catalog</a>.
    
    <br />
    </td>
    </tr>
    </table>
    
    </asp:Panel>
    
    
    
    
    
    <asp:DataGrid id="dgrdPages" CssClass="Grid" HorizontalAlign="Center" Width="98%" CellSpacing="0"
    								BorderWidth="0" ShowHeader="False" AllowPaging="True" PageSize="5" PagerStyle-Mode="NumericPages"
    								AutoGenerateColumns="False" AllowSorting="True" HeaderStyle-CssClass="GridHeader"
    								PagerStyle-CssClass="GridPager" FooterStyle-CssClass="GridFooter" SelectedItemStyle-CssClass="GridSelectedItem"
    								AlternatingItemStyle-CssClass="GridAlternatingItem" ItemStyle-CssClass="GridItem" CellPadding="0"
    								Runat="Server"
                                    OnPageIndexChanged="Grid_Change">
                                    
    								<SelectedItemStyle CssClass="GridSelectedItem"></SelectedItemStyle>
    								<AlternatingItemStyle CssClass="GridAlternatingItem"></AlternatingItemStyle>
    								<ItemStyle CssClass="GridItem"></ItemStyle>
    								<HeaderStyle CssClass="GridHeader"></HeaderStyle>
    								<FooterStyle CssClass="GridFooter"></FooterStyle>
    								<Columns>
    									<asp:TemplateColumn>
    										<ItemTemplate>
                                                    <table>
                                                      <tr>
                                                        <td><%# Eval("Title") %></td>
                                                      </tr>
                                                      <tr>
                                                        <td><%# Eval("Description") %></td>
                                                      </tr>                                                  
                                                      <tr>
                                                      <td>
                                                         <span style="color:Green;"><%# Eval("Url") %></span>
                                                      </td>
                                                      </tr>
                                                    </table>
                                            </ItemTemplate>
                                         </asp:TemplateColumn>
    
                                   </Columns>
    
                               </asp:DataGrid>
    </asp:Content>
    Site_Search_Results.aspx.cs

    The following line in the code below does not work. What I try to do is to get the first 50 characters from the *.aspx file once the keywords found in this *.aspx file.

    dr["Description"] = file.Substring(0, 50); //<== This line does not work.

    // OR dr["Description"] = source.Substring(0, 50); //<== This line does not work.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text.RegularExpressions;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Data.OleDb;
    using System.Net.Mail;
    
    public partial class Site_Search_Results : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
                dgrdPages.DataSource = CreateDataSource();
                dgrdPages.DataBind();
            }
        }
    
    
        private ICollection CreateDataSource()
        {
                 
    
            string search = Request.Params.Get("Search_Keyword").Trim();
    
            string[] files = Directory.GetFiles(Server.MapPath("./"), "*.aspx", SearchOption.AllDirectories);
    
            string dir = Server.MapPath("./");
    
            string[] files1;
            int numFiles;
            files1 = Directory.GetFiles(dir);
            numFiles = files.Length;
    
            DataTable dtg = new DataTable();
            DataColumn dc = null;
    
            dc = new DataColumn("Title", typeof(string));
            dtg.Columns.Add(dc);
    
            dc = new DataColumn("Description", typeof(string));
            dtg.Columns.Add(dc);
    
            dc = new DataColumn("Url", typeof(string));
            dtg.Columns.Add(dc);
    
    
            // begin - search static files
            for (int i = 0; i < numFiles; i++)
            {
                string file = files[i].ToString();
                Search_Static_Files(file, search, dtg, dc);
            }
            // end - search static files
    
    
            DataView dv = new DataView(dtg);
    
            if (dv.Count <= 0)
            {
                panel_Found.Visible = false;
                panel_Not_Found.Visible = true;
                dgrdPages.Visible = false;
    
            }
            else
            {
                panel_Found.Visible = true;
                panel_Not_Found.Visible = false;
                dgrdPages.Visible = true;
            }
    
            return dv;
    
        }
    
    
        public void Grid_Change(Object sender, DataGridPageChangedEventArgs e)
        {
    
            // Set CurrentPageIndex to the page the user clicked.
            dgrdPages.CurrentPageIndex = e.NewPageIndex;
    
            // Rebind the data to refresh the DataGrid control. 
            dgrdPages.DataSource = CreateDataSource();
            dgrdPages.DataBind();
        }
    
        private void Search_Static_Files(String file, String searchText, DataTable dtg, DataColumn dc)
        {
    
            //Declare reader as a new StreamReader with file as the file to use
    
            System.IO.StreamReader reader = new System.IO.StreamReader(file);
    
            //Declare text as the reader reading to the end
    
            String text = reader.ReadToEnd();
    
            int FoundText;
    
            int intSearchLoopCounter;
            string[] sarySearchWord;
            
            char[] del = { ' ' };
            
            sarySearchWord = searchText.Trim().Split(del);        
    
            for (intSearchLoopCounter = 0; intSearchLoopCounter < sarySearchWord.Length; intSearchLoopCounter++)
            {
                FoundText = text.IndexOf(sarySearchWord[intSearchLoopCounter], StringComparison.CurrentCultureIgnoreCase);
    
                if (FoundText > -1)
                {
    
    
                    WebClient x = new WebClient();
                    string source = x.DownloadString(file);
                    string title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
                
                    string s = Server.HtmlEncode(file);
                
                    Uri uri = new Uri(file);
                    string filename = Path.GetFileName(uri.LocalPath);
    
                    string full_url = HttpContext.Current.Request.Url.AbsoluteUri;
                    string url_without_filename = Path.GetDirectoryName(full_url);
    
                    DataRow dr = dtg.NewRow();
                    dr["Title"] = "<a href='" + filename.Trim() + "' style='Color:#1e4d7b'>" + title.Trim() + "</a>"; //url_without_filename.Trim() + "\\" + filename.Trim();
    
                   dr["Description"] = file.Substring(0, 50); //<== This line does not work.
     // OR dr["Description"] = source.Substring(0, 50); //<== This line does not work.
                                    
                    dr["Url"] = url_without_filename.Trim() + "\\" + filename.Trim() + "<br /><br />";
                    dtg.Rows.Add(dr);
                                                 
                    break;
    
                }
    
                else
                {
    
                    //Show that it was not a match
                }
    
            }
    
    
        }
    
    }
    You can run the code by copying the
    Site_Search_Results.aspx
    Site_Search_Results.aspx.cs
    Default.aspx
    Default.aspx.cs

    Please note that you need to create a number of *.aspx files so that the search engine can search them.

    Default.aspx
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="textbox_SearchKeyword" runat="server" /> <asp:Button ID="button_Search" runat="server" Text="Search" OnClick="button_Search_Click" />
        </div>
        </form>
    </body>
    </html>
    Default.aspx.cs
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
    
    
    
        protected void button_Search_Click(object sender, EventArgs e)
        {
            Response.Redirect("Site_Search_Results.aspx?Search_Keyword=" + textbox_SearchKeyword.Text.Trim());
        }
    }
    Last edited by cecilia_2004; April 12th, 2012 at 05:45 AM.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    London
    Posts
    40
    Rep Power
    15
    ok I haven't created a dummy project to try you code out.

    However before I do that, have you tried something like this:

    c# Code:
     
     
     
    dr["Description"] = source.toString().Substring(0, 50);



    Also, what exactly is the error message?
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    London
    Posts
    40
    Rep Power
    15
    Soo...

    I tried understanding your code, but ended up just writing most of it from scratch:

    so instead of using a heavy "datagrid", you can just generate the html yourself (its just a html table anyway).

    try the following code, you will need to finish the rendering part your self, and play around with the extractions parts:

    and it does not return just the first few characters for the descriptions, it does return the full 50 characters.


    Remember THINK FUNCTIONALLY



    c# Code:
     
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
     
    namespace SearchEngine
    {
        public partial class Search_Site_Results : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack 
                    && 
                    Request.QueryString["Search_Keyword"] != null 
                    && 
                    !string.IsNullOrEmpty(Request.QueryString["Search_Keyword"].ToString())
                    )
                { 
                    //split search terms
                    List<string> SearchList = new List<string>();
     
                    SearchList = Request.QueryString["Search_Keyword"].ToString().Split(' ').ToList<string>();
     
                    HTML_RESULT.Text = RenderResults(GetMatchedPages(SearchList, GetStaticFiles()));
                }
            }
     
            private List<string> GetStaticFiles()
            { 
                //Get List of Static Files:
                return Directory.GetFiles(Server.MapPath("./"), "*.aspx", SearchOption.AllDirectories).ToList<string>();;
            }
            private List<StaticPage> GetMatchedPages(List<string> SearchList, List<string> PageList)
            {
                List<StaticPage> MatchedPages = new List<StaticPage>();
     
                WebClient x = new WebClient();
     
                foreach (string aspx in PageList)
                {
                    string source = x.DownloadString(aspx);
                    foreach (string word in SearchList)
                    {
                        if (source.ToLower().Contains(word.ToLower()))
                        { 
                            //this aspx page contains one of the seach term so add it:
                            StaticPage CurrentPage = new StaticPage();
     
                            CurrentPage.PageTitle = "";//extract Page Time
                            CurrentPage.PageDescription = source.ToString().Substring(0, 50);
     
                            Uri Url = new Uri(aspx);
     
                            CurrentPage.PageUrl = Url.AbsoluteUri.ToString();
     
                            if (!MatchedPages.Contains(CurrentPage))
                            {
                                MatchedPages.Add(CurrentPage);
                            }
                        }
                    }               
                }
     
                return MatchedPages;
            }
            private string RenderResults(List<StaticPage> PageLists)
            {
                string html = "";
     
                foreach (StaticPage aspx in PageLists)
                {
                    //Build your html table here using each attributes:
                    html += aspx.PageUrl +"-->"+ aspx.PageDescription +"<br/><br/";
                }
     
                return html;
            }
            public class StaticPage
            {
                public string PageTitle { get; set; }
                public string PageDescription { get; set; }
                public string PageUrl { get; set; }
            }
        }
    }
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    305
    Rep Power
    0
    Originally Posted by Darknite
    ok I haven't created a dummy project to try you code out.

    However before I do that, have you tried something like this:

    c# Code:
     
     
     
    dr["Description"] = source.toString().Substring(0, 50);



    Also, what exactly is the error message?
    There is no error message. The code run without any error.
    In the past, the code return nothing (or empty) when I made the code to return the first 50 characters.

    Now the code return  instead of the first 50 characters.

    I had tried source.toString().Substring(0, 50); and I got the same result.

    Thanks,

    May
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    305
    Rep Power
    0
    Originally Posted by Darknite
    Soo...

    I tried understanding your code, but ended up just writing most of it from scratch:

    so instead of using a heavy "datagrid", you can just generate the html yourself (its just a html table anyway).

    try the following code, you will need to finish the rendering part your self, and play around with the extractions parts:

    and it does not return just the first few characters for the descriptions, it does return the full 50 characters.


    Remember THINK FUNCTIONALLY



    c# Code:
     
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
     
    namespace SearchEngine
    {
        public partial class Search_Site_Results : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack 
                    && 
                    Request.QueryString["Search_Keyword"] != null 
                    && 
                    !string.IsNullOrEmpty(Request.QueryString["Search_Keyword"].ToString())
                    )
                { 
                    //split search terms
                    List<string> SearchList = new List<string>();
     
                    SearchList = Request.QueryString["Search_Keyword"].ToString().Split(' ').ToList<string>();
     
                    HTML_RESULT.Text = RenderResults(GetMatchedPages(SearchList, GetStaticFiles()));
                }
            }
     
            private List<string> GetStaticFiles()
            { 
                //Get List of Static Files:
                return Directory.GetFiles(Server.MapPath("./"), "*.aspx", SearchOption.AllDirectories).ToList<string>();;
            }
            private List<StaticPage> GetMatchedPages(List<string> SearchList, List<string> PageList)
            {
                List<StaticPage> MatchedPages = new List<StaticPage>();
     
                WebClient x = new WebClient();
     
                foreach (string aspx in PageList)
                {
                    string source = x.DownloadString(aspx);
                    foreach (string word in SearchList)
                    {
                        if (source.ToLower().Contains(word.ToLower()))
                        { 
                            //this aspx page contains one of the seach term so add it:
                            StaticPage CurrentPage = new StaticPage();
     
                            CurrentPage.PageTitle = "";//extract Page Time
                            CurrentPage.PageDescription = source.ToString().Substring(0, 50);
     
                            Uri Url = new Uri(aspx);
     
                            CurrentPage.PageUrl = Url.AbsoluteUri.ToString();
     
                            if (!MatchedPages.Contains(CurrentPage))
                            {
                                MatchedPages.Add(CurrentPage);
                            }
                        }
                    }               
                }
     
                return MatchedPages;
            }
            private string RenderResults(List<StaticPage> PageLists)
            {
                string html = "";
     
                foreach (StaticPage aspx in PageLists)
                {
                    //Build your html table here using each attributes:
                    html += aspx.PageUrl +"-->"+ aspx.PageDescription +"<br/><br/";
                }
     
                return html;
            }
            public class StaticPage
            {
                public string PageTitle { get; set; }
                public string PageDescription { get; set; }
                public string PageUrl { get; set; }
            }
        }
    }
    Ok.

    Thanks for the code!

    May
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo