I would recommend you extracting this functionality into some utility class that you could reuse from both PageA and PageB:
public class Country
{
public string Name { get; set; }
public string Iso3166TwoLetterCode { get; set; }
public static Country GetCountry(string userHost)
{
IPAddress ipAddress;
if (IPAddress.TryParse(userHost, out ipAddress))
{
return new Country
{
Name = ipAddress.Country(),
Iso3166TwoLetterCode = ipAddress.Iso3166TwoLetterCode()
};
}
return null;
}
}
And then in your page:
protected void Page_Load(object sender, EventArgs e)
{
//Code to fetch IP address of user begins
string userHost = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (String.IsNullOrEmpty(userHost) ||
String.Compare(userHost, "unknown", true) == 0)
{
userHost = Request.Params["REMOTE_ADDR"];
}
Label1.Text = userHost;
var country = Country.GetCountry(userHost);
if (country != null)
{
Label2.Text = country.Name;
Label3.Text = country.Iso3166TwoLetterCode;
}
}
Now you could reuse the Country
class from another page. Depending on your requirements you could even further customize it by passing additional parameters to the function and the return type.