Monday, May 11, 2009

.NET annoyance 4 - Lack of conditional tags

There are no conditional tags in .NET web components.

So instead of a nice jstl tag in java like:

Java/JSP/JSTL/EL Solution #1:
<c:if test="${row.name == ''}"%gt;BLANK%lt;/c:if>

In .NET I would have to create a code-behind wrapper method like:

Solution #2:
protected String testEmpty(String stringToCheck, String trueReturn, String falseReturn)
{
if("".Equals(stringToCheck)) return(trueReturn);
else return(falseReturn);
}

Then use this in the form view:
<%# testEmpty(DataBinder.Eval(Container.DataItem, "name"),
"BLANK", DataBinder.Eval(Container.DataItem, "name")) %>

Or Solution #3:
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView dataRow = e.Row.DataItem as DataRowView;
Object dataValue = dataRow["name"];
if("".Equals(dataValue)) e.Row.Cells[2].Text = "BLANK";
}
}

In solution 2 I am essentially creating my own conditional tag. Why shouldn't there be one like this by default? In solution 3 I am creating codebehind that will break if I reorder the fields in the formview.

No comments:

Post a Comment