31 August 2005

How to convert from hexadecimal to int?


public string IntToHex(int number)
{
return String.Format("{0:x}", number);
}

public int HexToInt(string hexString)
{
return int.Parse(hexString,
System.Globalization.NumberStyles.HexNumber, null);
}

09 August 2005

Invalidade ASP.Net Cache to reload dependent user controls

If you have two user controls (ascx) in the asp.net cache and need to update a user control if the other changes, you can use the following method:

On the dependent ascx, that needs to refresh:
PartialCachingControl pcc= Parent as PartialCachingControl;
StaticPartialCachingControl spcc= Parent as StaticPartialCachingControl;

if (pcc !=null) pcc.Dependency=new CacheDependency(null, new string[]{"HighLightChange"});

This will create a cache dependency on the HighLightChange string.

On the ascx that needs to invalidate the cache:
HttpRuntime.Cache.Insert("HighLightChange","true");

The change on the HighLightChange will invalidate the cache dependency of the user control that must do a data refresh.

02 August 2005

How to get the ApplicationPath in ASP.Net?

If you have a virtual directory:
string path = HttpContext.Current.Request.ApplicationPath;

If your site is on the root or a virtual directory:

string path = HttpContext.Current.Request.ApplicationPath;
if (path == "/")
path = "http://" + Request.Url.Host;
else
path += "/";