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/

Monday, April 27, 2009

Restoring master and msdb Database

Restoring master database requires sql server running in single-user mode. We can do the following:
1. Start a cmd window
2. Stop SQL Server
net stop mssqlserver
3. Start SQL Server in single user mode
sqlservr -m
4. Start another cmd window
5. Restore the master database
osql -E -Q"restore database master from disk = 'D:\SQL2000\MSSQL\BACKUP\master-2007-03-12.bak'

NOTE: after executing this command you should see the following output:
The master database has been successfully restored. Shutting down SQL Server.
SQL Server is terminating this process.

In step 5, we can also restore the database in Query Analyzer by running: restore database master from disk = 'D:\SQL2000\MSSQL\BACKUP\master-2007-03-12.bak'


To restore msdb database, we should run sql server in multi-user mode. If there is an error indicating exclusive access, then try to stop sqlserveragent.

Friday, April 10, 2009

Color of Source Code in Blog

Paste the code in Word, copy it from there and paste here (in Compose mode). Then we'll have the source code color preserved!


function sayAlice() {

var sayAlert = function() { alert(alice); }

// Local variable that ends up within closure

var alice = 'Hello Alice';

return sayAlert;

}

Tuesday, March 24, 2009

javascript code not working when using updatepanel?

I have some javascript code gets executed when window.onload is fired. It basically modifies the appearance of my server controls. Everything works fine before I placed my server controls in an updatePanel(ASP.NET).

When the page got partially rendered(i.e. I clicked the button which is inside the updatePanel to cause a postback), the javascript code didn't work anymore.

I haven't delve into this issue to completely understand this, but I found a solution. It is to use PageRequestManager to register the javascript function I'd like to call each time there is a postback(async or sync). The code is:
function load() {
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(javascriptFuncName);
}
Then add onload="load()" to body tag.

For reference:http://msdn.microsoft.com/en-us/library/bb311028.aspx(Sys.WebForms.PageRequestManager Class)

Wednesday, March 18, 2009

Web page gets messy in IE6



The problem: Using background inside frames.
The solution: Use img instead of background and set its z-index properly.