Archive for December, 2008
HttpPostedFile.FileName browser dependent behaviour
Dec 14th
Even though these kind of variables must have some standards (may be it already has), there are differences in practise. Be careful with the FileName property of HttpPostedFile class when you want to work on a file being uploaded by visitors of your page because the FileName value that you will get on Internet Explorer and Firefox are different.
If your visitor uses Internet Explorer, you will get the local path to the file. E.g.: C:\somefile.txt. However, if your visitor uses Firefox, the FileName value that you will get will be only the file’s name. E.g.: somefile.txt.
To avoid problems, you may have the following check.
string filename = postedFile.FileName;
if (filename.IndexOf('\\') > -1)
{
filename = filename.Substring(filename.LastIndexOf('\\') + 1);
}
else if (filename.IndexOf('/') > -1)
{
filename = filename.Substring(filename.LastIndexOf('/') + 1);
}
Extension methods don’t compile? No panic!
Dec 10th
I came across to a really strange problem today. Some extension methods that I have copied from another project just did not want to be compiled at all.
‘string’ does not contain a definition for ‘MethodName’.
I have checked everything, including clearing the temporary ASP.NET files within framework folder (C:\Windows\Microsoft.NET\Framework\…), restarting Visual Studio and even restarting the OS. The target framework of the projects were all set to 3.5 as well. But unfortunately, nothing helped. Finally, when I compared what kind of configuration I could be missing from web.config, I have found out the missing (and the killer) part.
If you have a similar problem, try adding the following configuration section in your project’s web.config file.
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<providerOption name="CompilerVersion" value="v3.5"/>
<providerOption name="WarnAsError" value="false"/>
</compiler>
</compilers>
</system.codedom>
Some useful UpdatePanel extensions
Dec 5th
I want to share some pretty basic and useful UpdatePanel control extensions with you. Method names are self explaining so I will not go deep in details.
public static void AlertOnLoad(this UpdatePanel updatePanel, string key, string message)
{
message = message.Replace("'", "\'").Replace("\r\n", "").Replace("\n", "");
message = "alert('" + message + "');";
AjaxControlToolkit.ToolkitScriptManager.RegisterStartupScript(updatePanel, updatePanel.GetType(), key, message, true);
}
public static void RedirectOnLoad(this UpdatePanel updatePanel, string key, string url)
{
url = "top.location.href = '" + url + "';";
AjaxControlToolkit.ToolkitScriptManager.RegisterStartupScript(updatePanel, updatePanel.GetType(), key, url, true);
}
public static void OpenWindowOnLoad(this UpdatePanel updatePanel, string key, string url)
{
url = "window.open('" + url + "');";
AjaxControlToolkit.ToolkitScriptManager.RegisterStartupScript(updatePanel, updatePanel.GetType(), key, url, true);
}