Electronic Screw

.NET :: Do[t]he NET

Using GeoIPService of WebServiceX

clock February 14, 2007 17:00 by author
WebserviceX.net provides a wide range of webservices, that can be used freely in development. GeoIPService is one webservice available with them that can be used to display the users country information that he/she was browsing from. 

A small example on how to use the webservice (GeoIPService) in asp.net development. Create a new Project in Visual Studio. Right click on the project in the Solution Explorer and Click Add Web reference. In the Add Web Reference dialog box, enter the following url in the URL: box and click Go button.

http://www.webservicex.net/geoipservice.asmx?wsdl

If the webservice is found, the information will be displayed in the respective boxes. Click on Add Reference button. By default the web service will be added with the following namespace net.webservicex.www

The required webserive files will be added automatically in the App_WebReferences folder created under the root of your application. You will see the geoipservice.discomap and geoipservice.wsdl added under the net >> webservicex >> www folder.

Note: geoipservice.discomap and geoipservice.wsdl are the discovery and webservice definition language files, that provides the necessary methods and classes.

Now , we use the classes and methods provided by the webservice in our page to display the country information like name etc..

Here is a sample listing on how to use in the aspx page (C# code)

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using net.webservicex.www;
using System.Text;

public
partial class _Default : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
      // set the current country name
      StringBuilder _stringToDisplay = new StringBuilder();
      GeoIPService _gipService = new GeoIPService();
      GeoIP _geoIP = _gipService.GetGeoIP(Request.UserHostAddress.ToString());
      _stringToDisplay.Append(_geoIP.CountryName);
      _stringToDisplay.Append(
" [" + _geoIP.CountryCode + "] ");
      
      
lblCountryName.Text = _stringToDisplay.ToString();
   }
}

A sample of this implementation can be seen in my home page http://www.g9th.com in the top right corner.

-Electronic Screw

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Creating a CascadingDropDown using ASP.NET AJAX

clock February 12, 2007 05:32 by author gnineth

I have seen in many forums people asking questions as “how to populate the cascadingdropdown with values from database". Although there was a sample provided for the same in the ASP.NET AJAX ControlToolkit, most of the learners (novice) following the walkthrough given at http://ajax.asp.net/ajaxtoolkit/Walkthrough/CCDWithDB.aspx get struck at the CarsTableAdapters used in the webservice.

So, I have posted an article Creating a CascadingDropDown Using AJAX at aspalliance.com which explains the step-by-step procedure on how to create CascadingDropDown using database values with the help of Microsoft SQL Server 2005 Express Edition.

E Screw

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Sort ListItems in ASP.NET

clock February 11, 2007 12:41 by author gnineth
Sorting of ListItems inside a Dropdownlist or Listbox is not provided by default .net framework. Either you need to sort your items at the database level (using order by) if the lists are binded with a datasource from database, or you need to write your own class which implements the IComparer interface and override the Compare method stub.

I write an example on how you write the class and override the Compare method. The compare method takes as input, 2 objects (x and y) and performs a comparison on the two objects. Passing objects as parameters will help in sorting custom object types as well as datatypes like int string etc., The function returns 0 if the two objects are equal, a number less than zero if x is less than y, and a number greater than zero if x is greater than y.

A class ListItemComparer which implements the IComparer interface and overrides the Compare method.

[code:c#]
// A class which implements the IComparer interface.
public class ListItemComparer : IComparer
{
    public int Compare(object x, object y)
    {
        ListItem li_x = (ListItem)x;
        ListItem li_y = (ListItem)y;
        CaseInsensitiveComparer c = new CaseInsensitiveComparer();
        return c.Compare(li_x.Text, li_y.Text);
    }
}
[/code]


Now, we can have a generalized static method, which takes the Dropdownlist as input and sorts it accordingly.

[code:c#]
public static void SortDropDownList(DropDownList dlList)
{
    ArrayList arl = null;
    if (dlList.Items.Count > 0)
    {
        arl = new ArrayList(dlList.Items.Count);
        foreach (ListItem li in dlList.Items)
            arl.Add(li);
     }
    arl.Sort(new ListItemComparer());
    dlList.Items.Clear();
    for (int i = 0; i < arl.Count; i++)
        dlList.Items.Add(arl[i].ToString());
}
[/code]


Now you can use this method to sort any dropdownlist by passing it to the above method.

E Screw

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Enter key on Textbox Control

clock February 11, 2007 10:03 by author gnineth

To enable the enter key on a textbox, to get the functionality of clicking a button, write the javascript. If you have only one button, and by setting the AutoSubmitBehavior to True, you can achieve this, but when you have multiple buttons, when the user press the Enter key, the first focused button is clicked.

With javascript the enter key code can be trapped. The following code will fire the enter key event, on the body.

<body onkeydown="if(event.keyCode == 13){document.getElementById('btnSubmit').click();}">
...
<asp:Button id="btnSubmit" runat="server" Text="Submit"></asp:Button>


If you want to trap the enter key on a textbox use the following

<head>
   <script language="javascript">
       function doEnterKey()
        {
            if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13))
            {
                document.form1.submit();
            }
        }
   </script>
</head>
<body>
   <form id="form1" runat="server">
      <asp:TextBox id="Text1" runate="server" />
   </form>
</body>
</html>

in Page_Load procedure in code-behind file write the following code

submit1.Attributes.Add("onfocus","javascript:doEnterKey();")
Text1.Attributes.Add("onchange","javascript:doEnterKey();")

E Screw

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Search

Calendar

<<  November 2008  >>
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

Archive

Tags

Categories


Blogroll

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2008

    Sign in