Wednesday, May 27, 2009

Enumerating enum

Probably all of us have encountered the situation where we need to enumerating a enum type. For example, we may want to put all the enum values in a listbox. The solution may not be so straight forward to come up with, but is simple enough: (RegistryValueKind is an enum type in Microsoft.Win32 namespace)

List<string> list = new List<string>();

foreach( RegistryValueKind kind in

Enum.GetValues(

typeof(RegistryValueKind)).Cast<RegistryValueKind>())

{

list.Add(kind.ToString() + ((int)kind).ToString());

}


Monday, May 4, 2009

Accessing Data Bound to ListView

If we try to access data that are bound to listview by the following code:
ListViewDataItem lvDataItem = ListView.Item[index];

We'll find that lvDataItem is null.

In fact, all the ListViewDataItem in listview's Item collection are null. The only time it is not null is in ItemDataBound event of ListView. I guess it is designed this way to save memory space. So what if we need to access the data outside ItemDataBound event?

The answer is simple. Just specify DataKeyNames property of ListView like this:
DataKeyNames="ProductName, UnitPrice"

Then, we can access any data:
ListView.DataKeys[index]["ProductName"]

Friday, May 1, 2009

Javascript Code Meets Postback(full and partial)

Code in $(document).ready is NOT executed in partial postback; pageLoad() function works for partial postback. Both work in full(standard postback).

For the javascript code register in ClientScript.RegisterClientScriptBlock, it is on the page for both full and partial postback. (Because in partial postback, Page_Load event on server side also gets called.)

http://encosia.com/2009/03/25/document-ready-and-pageload-are-not-the-same/