Avoid Multiple Form Tags To Add Google Search To ASP.Net Page

by Naveen 13. August 2010 09:12

Last week I was updating the site to include some new features. I realized that I was missing a very important component on the blog and that was google search box. I had this search on my other sites for quite some time. I remember that I had to deal with the fact that Google Custom Search script is implemented as a POST FORM with the action set to a URL that will be used to display results.

First you have to deal with the fact that ASP.Net framework does not allow you to have multiple FORM tags on the page. But if you want have more than one then only one can have runat='server' attribute on it. And then you have to make sure that your FORM tags are not nested. Well, depending on your layout of the site and implementation, you may or may not be able to control location of this google search form. Here is the solution that I implemented few years ago and deployed on this site as well.

Use of iform

Yes, took the Google Custom Search script and put it in a static HTML page. And then put an IFRAME on the page where I wanted to display the search box. This is where you will have to be little careful. You will have to make sure that style of the HTML page matches your main site. Next make sure that height and width of iframe is big enough to accomodate the search box otherwise you will end up with horizontal and vertical scroll bars around the frame and that will not be a pretty sign for site users. You want to give the illusion to users that this search box is sitting on the page itself. Now perform search and it works. But there is a problem. Search result target URL is being opened in the frame where search box is. Well that is a little problem.

Fix target of FORM

This is where you will have to make a change in the script that Google Custom Search generated for you. In FORM tag, set target to _top. Now when this form is submitted, the action is sent to page contained this iframe. The following snippet is what I have in my HTML page.

<div class="cse-branding-bottom" style="background-color:#FFFFFF;color:#000000">
  <div class="cse-branding-form">
    <form action="http://localhost/ByteBlocksWeb/SearchResults.aspx" 
      id="cse-search-box" 
      target="_top">
      <div>
        <input type="hidden" name="cx" value="partner-pub-xxxxxxxxxxxxx:wcbg5i-ahdn" />
        <input type="hidden" name="cof" value="FORID:11" />
        <input type="hidden" name="ie" value="ISO-8859-1" />
        <input type="text" name="q" size="60" />
        <input type="submit" name="sa" value="Search" />
      </div>
    </form>
  </div>
  <div class="cse-branding-logo">
    <img src="http://www.google.com/images/poweredby_transparent/poweredby_FFFFFF.gif" alt="Google" />
  </div>
  <div class="cse-branding-text">
    Custom Search
  </div>
</div>

Now perform a search and you will notice that your results show up in main page and not in iframe anymore. Give it a try right here in this site by using search at top of the page and see how it works.

Give your advice to big bosses and make money

Views: 501

Tags:

ASP.Net | Google

Convert IP Address To Location and Address to Geo Location Using C# VB.Net

by Viper 1. June 2009 19:10

Recently I implemented BackYardTweets. Big part of the site is geo targeting. What that means is that to figure out location of the user visiting the site and getting longitude and latitude of that location. The implementation uses the following work flow:

  • When user visits the site, only information that is available is IP adress assigned to your network connection by user's internet service provider. The implementation translates that IP address to location including coordinates (longitude and latitude).
  • After user has logged in, application allows user to change location. User can pick country, region and city. From these three (some time only 2), application determines geolocation.

Now I will explain how these two steps are performed.

Convert IP Address To Geo Location

Application uses IP to geo location service provided by IP Location tools. A simple HTTP request to this site's target URL sends xml response that containss all the data that you will need to get location of site's visitor. Following is sample response.

<?xml version="1.0" encoding="UTF-8"?>
<Response>
 <Ip>74.125.45.100</Ip>
 <Status>OK</Status>
 <CountryCode>US</CountryCode>
 <CountryName>United States</CountryName>
 <RegionCode>06</RegionCode>
 <RegionName>California</RegionName>
 <City>Mountain View</City>
 <ZipPostalCode>94043</ZipPostalCode>
 <Latitude>37.4192</Latitude>
 <Longitude>-122.057</Longitude>
</Response>

Following code snippet shows the implementation that was done in the application to get geo location based on IP address of site visitor.


public class UserGeoLocator
{
 private const string ApiUrl = "http://ipinfodb.com/ip_query.php?ip={0}";
 public SiteUser GetUserLocation(string ipAddress)
 {
 if (string.IsNullOrEmpty(ipAddress))
 {
  return null;
 }
 string reqUrl = string.Format(ApiUrl, ipAddress);
 HttpWebRequest httpReq = HttpWebRequest.Create(reqUrl) as HttpWebRequest;
 try
 {
  string result = string.Empty;
  HttpWebResponse response = httpReq.GetResponse() as HttpWebResponse;
  using (var reader = new StreamReader(response.GetResponseStream()))
  {
   result = reader.ReadToEnd();
  }
  return ProcessResponse(result);
 }
 catch (Exception ex)
 {
  throw;
 }
}

private SiteUser ProcessResponse(string strResp)
{
 StringReader sr = new StringReader(strResp);
 XElement respElement = XElement.Load(sr);
 string callStatus = (string)respElement.Element("Status");
 if (string.Compare(callStatus, "OK", true) != 0)
 {
  return null;
 }
 SiteUser user = new SiteUser() {IP = (string) respElement.Element("Ip"), 
 City = (string) respElement.Element("City"),
 Country =  (string)respElement.Element("CountryName"),
 CountryCode = (string)respElement.Element("CountryCode"),
 RegionCode =  (string)respElement.Element("RegionCode"),
 RegionName = (string)respElement.Element("RegionName"),
 PostalCode =  (string)respElement.Element("ZipPostalCode"),
 Latitude = (decimal)respElement.Element("Latitude"),
 Longitude = (decimal)respElement.Element("Longitude")};
 return user;
 }
}

Geo Location From Address

Application allows user to select Country, Region and City. This needs to be translated to geo location. Application uses Google's Map API to accomplish the task. For this you will need to apply for key from Google. Following code snippet is from the application. It takes address of user as input and translates it to latitude and latitude. Following code snippet is from class implemented in the application.


public static bool GetGeoLocationFromGoogle(string nearLocation, 
                      out double longitude, out double latitude)
{
 bool ret = false;
 longitude = latitude = 0d;
 string apiKey = ConfigHelper.GooleMapKey;
 string reqUrl = string.Format("http://maps.google.com/maps/geo?q={0}&output={1}&key={2}",
			  HttpUtility.UrlEncode(nearLocation), "csv", apiKey);
 WebClient httpClient = new WebClient();
 try
 {
  string response = httpClient.DownloadString(reqUrl);
  string[] values = response.Split(",".ToCharArray());
  if (values.Length == 4)
  {
   if (values[0] == "200")
   {
    latitude = double.Parse(values[2]);
    longitude = double.Parse(values[3]);
    ret = true;
   }
  }
}
catch (Exception ex)
{
 Trace.WriteLine(ex.Message);
}
return ret;
}

See how it works

Use see all this in action at following link


Give your advice to big bosses and make money

Views: 14539

Tags: ,

.Net | Google | Twitter

Google Adsense - How many ad units can be put on a page

by Viper 10. February 2009 05:33

Every now and then I get question from somebody, How may google ad units I can place on my page?. All this information is actually available on google's site. I will just put that data in a nice table form so it is easy for you to refer to it. I have extracted this data from google's site. I will suggest that for more accurate and upto date information you do visit the link that I have supplied at the bottom of the post.

Unit Type#
Link Units3
Standard Ad Units3
Search Boxes2
Video Units1

Can I place more than one link unit on a page?
Give your advice to big bosses and make money

Views: 1200

Tags: , ,

SEO | AdSense | Google

Powered by BlogEngine.NET 1.5.1.7
Theme by Naveen Kohli

By Categories