Thursday, April 21, 2011

Knock Me Out

Knock Me Out: "Ideas, thoughts, and discussion about Knockout (KnockoutJS.com) and related technologies (jQuery and jQuery Templates)"

Friday, April 08, 2011

Why I Still L.O.V.E. ASP.NET WebForms - John Katsiotis

This guy uses the Model-View-Presenter (MVP) pattern with ASP.NET WebForms. A Microsoft MSDN link is here; a Martin Fowler article here.

Why I Still L.O.V.E. ASP.NET WebForms - John Katsiotis: "We will use the MVP pattern and the open-source project WebFormsMVP to accomplish that! Can’t wait? Download the sample!"

Thursday, April 07, 2011

Don't mock HttpContext

Don't mock HttpContext: "It's so easy to take a direct dependency on HttpContext and not even realize it. If you're in the code behind in Web Forms or in a controller action in MVC, it's just right there, tempting you to use it to access session variables, application security, etc.

But don't."

Monday, April 04, 2011

dapper-dot-net - Simple SQL object mapper for SQL Server - Google Project Hosting

Wow. Impressive! Great for quick and dirty stuff, no? It uses dynamic types extensively.

dapper-dot-net - Simple SQL object mapper for SQL Server - Google Project Hosting: "Dapper - a simple object mapper for .Net
Dapper is a single file you can drop in to your project that will extend your IDbConnection interface."

Friday, April 01, 2011

Setting the Text of a ListControl in ASP.NET

If you try the following, it should set the visible text of the dropdown control in question, right?
    listControl.Text = "4 Year College";
Well, unfortunately, no! The "Text" property is named that way for consistency across all ASP.NET controls and refers to the form value that is passed to the server when the page is submitted. So unfortunately for me, the "Text" property silently rejected my attempt to set it and the page did not work.

You may be asking yourself why I am trying to set the visible text and not the value of the control. The reason is that the application I'm working on receives the displayed text from a web service and not the value that is passed in the form variables. Why not create a dictionary, look up the the text string in the dictionary, and pass that to the control?

The answer? I already have a dictionary. It's called the SELECT control itself. The generated HTML for my control is, say, the following:

<select name="ddlAvailableData" id="ddlAvailableData">
<option value="1">Harvard, MA: 2007-03 to 2007-03</option>
<option value="2">Cleveland Public, OH: 2003-01 to 2003-01</option>
</select>
view raw gistfile1.html hosted with ❤ by GitHub

This control is essentially a dictionary of key-value pairs: the key is the value attribute of the option tag, and the value is the visible text that is displayed for the given item in the dropdown list. So in the spirit of Don't Repeat Yourself (DRY), I wrote a extension function to allow me to set not just the value but also the text. I give you ListControlExtensions.cs:

using System;
using System.Linq;
using System.Web.UI.WebControls;
namespace System.Web.UI.Controls
{
public static class ListControlExtensions
{
/// <summary>
/// Set the list control text to a specified value. First search on text; next search on value.
/// </summary>
/// <param name="listControl"></param>
/// <param name="s">Value or Text to set.</param>
/// <param name="leaveAloneIfNotFound">If true, then the control isn't changed if the desired item is not found.</param>
/// <returns>The ListControl.</returns>
public static ListControl SetSelectedTextOrValue(this ListControl listControl, string s, bool leaveAloneIfNotFound = false)
{
int newIndex = -1;
// First search by text, then by value.
ListItem listItem = listControl.Items.Cast<ListItem>()
.Where((item1, i) =>
{
newIndex = i;
return item1.Text.Equals(s, StringComparison.CurrentCultureIgnoreCase);
})
.FirstOrDefault() ??
listControl.Items.Cast<ListItem>()
.Where((item1, i) =>
{
newIndex = i;
return item1.Value.Equals(s, StringComparison.CurrentCultureIgnoreCase);
})
.FirstOrDefault();
if (listItem != null)
{
listControl.SelectedIndex = newIndex;
}
else if (!leaveAloneIfNotFound)
{
// Nothing satisfies the criteria.
listControl.SelectedIndex = -1;
}
return listControl;
}
}
}

Here's a typical usage. In this particular scenario, my data should be scrubbed, but if not I log a warning:

string certificationType = GetCertificationType(ordinal);
ddlCertification.SetSelectedTextOrValue(certificationType);
if (ddlCertification.SelectedItem == null)
{
Log.WarnFormat(
"An unsupported certification type '{0}' encountered for Education #{1} for Reference ID {2}",
certificationType, ordinal, BcRequest.ReferenceID);
}
view raw gistfile1.cs hosted with ❤ by GitHub