Jack @ ASP.NET

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries for December, 2009

Convert RGB Color To Hex With JavaScript

   1: function colorToHex(color) {

   2:     if (color.substr(0, 1) === '#') {

   3:         return color;

   4:     }

   5:     var digits = /(.*?)rgb\((\d+), (\d+), (\d+)\)/.exec(color);

   6:     

   7:     var red = parseInt(digits[2]);

   8:     var green = parseInt(digits[3]);

   9:     var blue = parseInt(digits[4]);

  10:     

  11:     var rgb = blue | (green << 8) | (red << 16);

  12:     return digits[1] + '#' + rgb.toString(16);

  13: };

Happy new year!

Add New Iterations in TFS

Do you know how to add new iterations in TFS, than, when add new work items, we can select the newly created iterations.

image

Required Permissions

To perform this procedure, you must be a member of the Project Administrators security group for the team project. You cannot explicitly set the necessary security permissions for this procedure. For more information, see Team Foundation Server Permissions.

To modify the team project iterationsTFS_New_Iteration
  1. On the Team menu, point to Team Project Settings, and then click Areas and Iterations.

  2. On the Areas and Iterations dialog box, click the Iterations tab.

  3. Use the toolbar buttons to change the current iteration:

    • Click Add a child node to add a new node under the currently selected node.

    • Click Delete node to delete the currently selected node. In the Delete Nodes dialog box, select the new path for the items to reference, and then click OK.

    • Click Move a node up amongst its siblings to promote the currently selected node higher in the tree.

    • Click Move a node down amongst its siblings to demote the currently selected node lower in the tree.

    • Click Make the selected node a child of its preceding sibling to make the currently selected node a child of the node immediately above it.

    • Click Make the selected node a sibling of its parent to make the currently selected node a peer of the node immediately above it.

  4. Click Close.

 

At last, pay attentions, the name of an iteration node cannot:

  • Contain more than 255 characters.

  • Contain Unicode control characters.

  • Contain any one of the following characters: \ / $ ? * : " & > < # % |

  • Be a system reserve name such as prn, com1, com2, com3, com4, com5, com6, com7, com8, com9, com10, lpt1, lpt2, lpt3, lpt4, lpt5, lpt6, lpt7, lpt8, lpt9, nul, con, aux.

  • Be one of the following characters: “.” or “..

  • Be characters considered invalid by the local file system. For example, Windows-based desktop operating systems might consider the following characters to be invalid if used in the path name: ASCII/Unicode characters 1 through 31, quotation mark ("), less than (<), greater than (>), pipe (|), backspace (\b), null (\0) and tab (\t).

  • Have a total path length greater than 4000 characters.

  • Have a total hierarchy depth of more than 14 levels.

C# Code Snippet: List.ForEach method

Do you ever use the List(T).ForEach Method in C#, if never, you’d better read this.

The syntax is :
public void ForEach(Action<T> action)

   1: static void Main()

   2: {

   3:     List<long> myList = new List<long>();

   4:     foreach (string item in "269, 361, 347, 355, 352, 346, 351, 354".Split(','))

   5:     {

   6:         myList.Add(long.Parse(item.Trim()));

   7:     }

   8:     myList.ForEach(FA);

   9: }

  10:  

  11: static void FA(long num)

  12: {

  13:     Console.WriteLine(num);

  14: }

Is that cool? It gives lots of convenience in coding!

List(T).BinarySearch in C#

I think most of us are quite familiar with binary search. Binary search is an algorithm for locating the position of an element in a sorted list by checking the middle, eliminating half of the list from consideration, and then performing the search on the remaining half.If the middle element is equal to the sought value, then the position has been found; otherwise, the upper half or lower half is chosen for search based on whether the element is greater than or less than the middle element. The method reduces the number of elements needed to be checked by a factor of two each time, and finds the target value, if it exists in logarithmic time. A binary search is a dichotomy divide and conquer search algorithm.

In C#, .Net provide a List<T>.BinarySearch Method (T) which can be used easily. here is a demo:

   1: // Init a long type list

   2: var myList = new List<long>();

   3: foreach (var item in "269, 361, 347, 355, 352, 346, 351, 354".Split(','))

   4: {

   5:     myList.Add(long.Parse(item.Trim()));

   6: }

   7:  

   8: // sort

   9: myList.Sort();

  10: var ret = myList.BinarySearch(347);

  11: Console.WriteLine(ret);

Note, I have a ‘myList.Sort();’ before call the BinarySearch, why? It is because BinarySearch will Search the entire sorted System.Collections.Generic.List<T> for an element using the default comparer and returns the zero-based index of the element.

Yes, it is sorted list that binary search will operate on! So remember make your list sorted before call binary search function

Several issues in built in asp.net pager control

  • Paging Events
    In order to handle paging you have to deal with paging events. The events fire at specific time instances in the page pipeline and because of this you often have to handle data binding in a way to work around the paging events or else end up double binding your data sources based on paging. Yuk.
  • Styling
    The GridView pager is a royal pain to beat into submission for styled rendering. The DataPager control has many more options and template layout and it renders somewhat cleaner, but it too is not exactly easy to get a decent display for.
  • Not a Generic Solution
    The problem with the ASP.NET controls too is that it’s not generic. GridView, DataGrid use their own internal paging, ListView can use a DataPager and if you want to manually create data layout – well you’re on your own. IOW, depending on what you use you likely have very different looking Paging experiences.
  • DataSource Controls required for Efficient Data Paging Retrieval
    The only way you can get paging to work efficiently where only the few records you display on the page are queried for and retrieved from the database you have to use a DataSource control – only the Linq and Entity DataSource controls  support this natively. While you can retrieve this data yourself manually, there’s no way to just assign the page number and render the pager based on this custom subset. Other than that default paging requires a full resultset for ASP.NET to filter the data and display only a subset which can be very resource intensive and wasteful if you’re dealing with largish resultsets (although I’m a firm believer in returning actually usable sets :-}). If you use your own business layer that doesn’t fit an ObjectDataSource you’re SOL. That’s a real shame too because with LINQ based querying it’s real easy to retrieve a subset of data that is just the data you want to display but the native Pager functionality doesn’t support just setting properties to display just the subset AFAIK.
  • DataPager is not Free Standing
    The DataPager control is the closest thing to a decent Pager implementation that ASP.NET has, but alas it’s not a free standing component – it works off a related control and the only one that it effectively supports from the stock ASP.NET controls is the ListView control. This means you can’t use the same data pager formatting for a grid and a list view or vice versa and you’re always tied to the control.
  • Postback and JavaScript requirements
    If you look at paging links in ASP.NET they are always postback links with javascript:__doPostback() calls that go back to the server. While that works fine and actually has some benefit like the fact that paging saves changes to the page and post them back, it’s not very SEO friendly. Basically if you use javascript based navigation nosearch engine will follow the paging links which effectively cuts off list content on the first page. The DataPager control does support GET based links via the QueryStringParameter property, but the control is effectively tied to the ListView control (which is the only control that implements IPageableItemContainer).

Top 10 Favorite Visual Studio Shortcuts

Use shortcuts can increase your development speed and save you lots of times. These are some of my favorite and most used shortcuts in visual studio, I only list 10 of them, hope it is useful.

  1. Holding down the Ctrl key -  while the completion list is displayed makes the list partially transparent.

  2. Ctrl + – and the opposite Ctrl + Shift + -  To move the cursor back or forward to the last position without having to scroll or switch  files.

  3. Shift+Alt+Enter Switches to Full Screen Mode

  4. Ctrl+K, Ctrl+C Comment a block,   Ctrl+K, Ctrl+U Uncomment the block

  5. Shift + Alt + F10 then Enter expands the smart tag to insert a using statement or implement an Interface

  6. Ctrl+K, Ctrl+D Auto format the file (Source, xml or  html)

  7. Ctrl+M, Ctrl+M – Toggle Outlining Expansion—Based on the current cursor position in the editor window, hides or unhides the outline region.

  8. Ctrl+M, Ctrl+ O Collapses all outlining to  definition and then Ctrl+M, Ctrl+M to toggle the current block to expanded or collapsed.

  9. Shift+ F12, Finds a reference to the selected item or the item under the cursor

  10. Ctrl-Shift-F12, Moves to the next task in the TaskList window