Posts tagged extension methods
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);
}
Strip all HTML tags from string
Nov 9th
This is something we need very often. Either for security reasons or just because of some business rules. I will share a small code snippet with you as an extension method to string type so you can take advantages of using Regular Expressions to remove HTML tags from any user input.
public static string StripHtml(this string value)
{
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
value = Regex.Replace(value, @"<(.|\n)*?>", string.Empty, RegexOptions.Multiline | RegexOptions.IgnoreCase);
return value;
}
And this is how you need to use it:
string var = txUserInput.Text; var = var.StringHtml();
How to uppercase the first letter of a word or sentence
Nov 1st
When I used to use PHP to develop my web applications, I really loved its basic but useful method named ucfirst. This function is explained using a simple explanation on PHP documentation:
ucfirst — Make a string’s first character uppercase
I believe within all those useful and complex libraries taking place in .NET Framework, it still misses these kind of basic and useful methods. However, the good side is that we can extend it using our custom extension methods.
Because I needed to implement such a feature as PHP offers using ucfirst function, I wrote the following simple extension to achieve the same functionality.
/// <summary>
/// Capitalizes the first letter of the word.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ToUpperFirst(this string value)
{
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
if (value.Length > 1)
{
return
string.Format("{0}{1}",
value.Substring(0, 1).ToUpper(),
value.Substring(1).ToLower()
);
}
else
{
return value.ToUpper();
}
}
And this is how to use it:
string val = "something"; val = val.ToUpperFirst(); // result = Something