вторник, 15 марта 2011 г.

Fixed: AsyncFileUpload always return “The file attached is invalid”

If you’re using Microsoft ASP.NET AjaxToolkit’s AsyncFileUpload placed inside a webform nested in a master page, chances are you’ll be faced with a baffling error message “The file attached is invalid”. No amount of manual troubleshooting and breakpoint-ing help because there’s no complete error messages (or even any that makes sense).
The reason: The client side script got confused because it couldn’t find the predicted ClientID.
The solution: Add ClientIDMode=”AutoID” to your markup!

суббота, 5 марта 2011 г.

ASP.NET 4.0,SEO and meta tags

I am thinking to create a new series of posts regarding ASP.NET and SEO (Search Engine Optimisation). I am going to start with this post , talking about some new features that make our asp.net apps more SEO friendly. At the end of the day, there is no point having a great application and somehow “scare” the search engines away. This is going to be a short post so let’s quickly have a look at meta keywords and ASP.NET 4.0.
Meta keywords and description are important elements of a page and make it search engine friendly. ASP.Net 4.0 added 2 new properties on the Page object to let us define the Meta Keywords and Description.
Create a simple asp.net application using Visual Studio 2010. In the Default.aspx.cs code behind file type
Page.MetaKeywords = “asp.net,vb,c#,css,html,”;
Page.MetaDescription = “This is my blog that focuses on ASP.NET.”;
Alternatively we can add those two meta tags in the Page directive
<%@ Page Language=”C#” MetaKeywords=”asp.net,vb,c#,css,html” MetaDescription=”This is my blog that focuses on ASP.NET.” AutoEventWireup=”true”  CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
Run your application. Go to View->Source and see the meta tags
Hope it helps!!!

пятница, 4 марта 2011 г.

Getting the first day in a week with C#

I recently found myself having a DateTime value and needing to calculate the date of the first day in the week that my known date was in using C#. While this is one of those things that are pretty simple to do once you know the solution it did take me a couple of hours to get it right. It turns out that something as simple as calculating a the first day of a week is trickier than one might expect, at least if we want a solution that works for all cultures.
My first instinct was to calculate the first day of a week by subtracting dayInWeek.DayOfWeek - firstDayInWeek days from dayInWeek where dayInWeek is a DateTime specifying a date in the week. That worked fine as long as I did the calculations in an environment where the culture was English, where the first day of the week is Sunday which is enumerated as 0 in DayOfWeek (against international standards which says the first day of the week is monday). As I shifted to using a Swedish culture it broke down and I had to find a better solution.
Heres what I finally came up with:
01.using System;
02.using System.Globalization;
04.public static class FirstDayOfWeekUtility
07./// Returns the first day of the week that the specified
08./// date is in using the current culture.
10.public static DateTime GetFirstDayOfWeek(DateTime dayInWeek)
12.CultureInfo defaultCultureInfo = CultureInfo.CurrentCulture;
13.return GetFirstDateOfWeek(dayInWeek, defaultCultureInfo);
17./// Returns the first day of the week that the specified date
18./// is in.
20.public static DateTime GetFirstDayOfWeek(DateTime dayInWeek, CultureInfo cultureInfo)
22.DayOfWeek firstDay = cultureInfo.DateTimeFormat.FirstDayOfWeek;
23.DateTime firstDayInWeek = dayInWeek.Date;
24.while (firstDayInWeek.DayOfWeek != firstDay)
25.firstDayInWeek = firstDayInWeek.AddDays(-1);
27.return firstDayInWeek;