ASP.NET / C#

Create a Search Box

I am going to be looking at different ways to add “Search” functionality to web sites. I have heard Google offers a search box you can add to your site, and I’ll probably use that one, just because I love Google. But today I learned of “PicoSearch”. They have a free one you can add to HTML pages (Free with ads). Or you can purchase it for $249/yr to remove the ads. (That’s $20.75/month; so decide if you’re really marketing on your site or if it’s just there reference).

If you are using code-behind here is some C# code:
(This is to perform tasks from a small box on the home page and return the results on a different page

protected void btnSearch_Click(object sender, EventArgs e)
{
   if (txtCustomerSearching.Text != null && txtCustomerSearching.Text != "")
      {  String varCriteria = "" ;
          varCriteria = txtCustomerSearching.Text;

         Response.Redirect(SiteSearch.aspx?ssCriteria=" + varCriteria);
     }

}

Then on the second page (the page that will return the results) use this code-behind:

private void Page_Load(object sender, System.EventArgs e)
  {  try
{
String ssCfrm1 = "";

ssCfrm1 = Request["ssCriteria"].ToString();

if (ssCfrm1 == "")
   {
     Response.Write("Search criteria not valid, try a different word: " + ssCfrm1);
   }
else
{
getHits(ssCfrm1);

}

}
catch (Exception ex)
    { throw ex; }

}

private void getHits(string ssCfrm1)
   {
       String ssCfrm = Request["ssCriteria"].ToString();
       lblCriteria.Text = ssCfrm1;
       Content content = new Content(appEnv.GetConnection());
// open connection to CMS database
dt = content.GetContentForSiteSearchForSource(ssCfrm1,"HRT");
LiteralControl lit;
LiteralControl lit2;
TableCell cell;
int prv = -1;
int cur;
int i = 1;

for each (DataRow dr in dt.Rows)
  { cur = Convert.ToInt32(dr["ContentID"]);
if (cur != prv)
    {
try
    { TableRow row = new TableRow();
       table1.Rows.Add(row);
       content = new Content(appEnv.GetConnection());
       dtC = content.GetContentForID(cur);.
       string varCid = "<a href='content.aspx?cid=" + dtC.Rows[0]["ContentID"].ToString() + ">"
           + dtC.Rows[0]["Headline"].ToString() + "</a>";
lit2 =new LiteralControl(varCid.ToString());
cell =new TableCell();

cell.HorizontalAlign =HorizontalAlign.Left;
cell.Font.Size = new FontUnit(FontSize.XSmall);
cell.Controls.Add(lit2);
row.Cells.Add(cell);
TableRow row2 = new TableRow();
table1.Rows.Add(row2);
string str = dtC.Rows[0]["Tagline"].ToString() + "<br />";
string segment = str.Substring(0,str.Length);
lit = new LiteralControl(str);
cell = new TableCell();
cell.HorizontalAlign = HorizontalAlign.Left;
cell.VerticalAlign =VerticalAlign.Top;
cell.Font.Size = new FontUnit(FontSize.XSmall);
cell.Controls.Add(lit);

row2.Cells.Add(cell);
  i = i + 1;
str ="";
segment="";
}
catch { }
lblTally.Text = (i - 1).ToString();

}

}

if (i == 1)
   { lblTally.Text ="0"; }
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s