Archive

Archive for the ‘.NET’ Category

ASP.NET: DefaultButton and DefaultFocus

June 13th, 2008 Comments off

Since a while, on some of my company’s internet pages the DefaultButton and DefaultFocus properties did not work, although the code used seemed to be perfect. The problem was that the DefaultFocus javascript was not rendered at all.

After investigating the problem, I walked into two problems, solving which solved my problem as well:

1. DefaultButton requires a control’s UniqueID property, but DefaultFocus requires a control’s ClientID property. Strange, but it really works ASP.NET: DefaultButton and DefaultFocus icon smile !
2. Do not use hyphen characters in your ID properties (I had an auto ID using a GUID), since it will cause your focus to fail (the javascript is not being rendered). If you have or want to use some kind of separation, use the underscore character instead (it will work).

Hope that helps somebody out there too ASP.NET: DefaultButton and DefaultFocus icon wink !

Technorati tags:

Lambda expressions and lambda expression trees

June 10th, 2008 Comments off

Lately I had to dive into consequences for a migration from VS 2005 to VS 2008 and I tumbled over lambda expressions and lambda expression trees. Although lambda expressions themselves are quite easy to understand, I had a hard time trying to understand lambda expression trees.

However, I found a very sharp explanation on lambda expression trees at Charlie Calvert’s blog. It helped me a lot getting the right point of view.

In fact, expression trees are not that difficult to understand. It is the people’s stories which make them hard to understand. One of the mistakes made in relation to expression trees is that they are extendable. Simple: they are not. The only difference is that lambda expressions are compiled at compile time, lambda expression trees not (they are stored tree-wise, as shown below – that explains their name).

Lambda expressions and lambda expression trees LambdaExpressionTree

The reason expression trees have an added value lies in the IQueryable<T> interface. As stated by Charlie, that interface has an Expression property. When using the interface, e.g. with LINQ to SQL, the expression tree is generated for the LINQ query, but when it has to be executed it can be translated into the target language (e.g. T-SQL) from the lambda expression tree.

Technorati tags:

HttpException: File does not exist

May 13th, 2008 24 comments

Since some time I happened to get a nasty exception in the global.asax Application_Error method. Exception details are below:

Type : System.Web.HttpException
Message : File does not exist.
Source : System.Web
ErrorCode : -2147467259
Data : System.Collections.ListDictionaryInternal
TargetSite : Void ProcessRequestInternal(System.Web.HttpContext)
Stack Trace :
at System.Web.StaticFileHandler.ProcessRequestInternal(HttpContext context)
at System.Web.DefaultHttpHandler.BeginProcessRequest(HttpContext context, AsyncCallback callback, Object state)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

After some hard time trying to resolve this bug, I finally figured out what the problem was. The problem related to this exception is an HttpRequest to a non-existing source, not being the main request, but something like an image or iframe source. In my case, it was an iframe source loaded with a non-existing document.

To resolve this exception at your place, add the following to your Watch window: ((HttpApplication)sender).Context.Request.Url and set a breakpoint on the Application_Error method. This way, the exception will clarify itself a lot (it definitely did for me).

Set MasterPage (.master) title from a web form (.aspx)

February 22nd, 2008 Comments off

Lots of people complain about not being able to change the master page’s title using ASP.NET 2.0 and up. A commonly seen tutorial is to use the following piece of code:

((HtmlTitle) Page.Master.FindControl("lblTitle")).Text =
    "Set some kind of title";

In most of the cases you need to change the master page’s title, you do not have to bother about this. Just use the correct page stadium and set the page’s title. The only page event to be able to do this is Page_PreInit (since master page and content page are still separate ‘pages’ in this stadium). The following will work (if you have AutoEventWireup enabled Set MasterPage (.master) title from a web form (.aspx) icon wink ):

protected void Page_PreInit(object sender, EventArgs e)
{
    Title = "Set some kind of title";
}

IsDateTime in .NET for both ticks and string representation

June 9th, 2006 Comments off

Some while ago I developed a Settings system, where the SettingValue type should be checked. The DateTime.TryParse function did not do all the work I wanted it to do, so I thought ‘why not develop my own workaround’ IsDateTime in .NET for both ticks and string representation icon smile ? It is posted below…

/// <summary>
/// The IsDateTime function returns whether the given expression is a DateTime or not.
/// </summary>
/// <param name="expression">Expression to check for being DateTime.</param>
/// <returns>Boolean</returns>
/// <remarks>
/// The DateTime expression can be given as ticks or as normal DateTime representation.
/// </remarks>
public static bool IsDateTime(object expression) {
if(expression == null) return false;Bert Loedeman

string stringExpression = System.Convert.ToString(expression);
if(String.IsNullOrEmpty(stringExpression)) return false;

// DateTime can be specified in ticks. In that case the string should be convertible
// to a long (System.Int64) and fit in a specific range.
long retLong;
if(Int64.TryParse(stringExpression, out retLong)) {
// Ticks: 0 = DateTime.MinValue, 0x2bca2875f4373fff = DateTime.MaxValue
if((retLong < DateTime.MinValue.Ticks) || (retLong > DateTime.MaxValue.Ticks)) return false;
return true;
}

DateTime retDateTime;
return DateTime.TryParse(stringExpression, out retDateTime);
}