Thursday, May 29, 2008

Custom formatting HTML in Visual Studio 2005

Sometimes the aspx source file(html) can be very hard to read. Luckly, the VS2005 included build in format html tool. Just select the portion of the aspx source page, right click the mouse, and choose the "format selection". The aspx source file will look much better with consistent indenting.

For more details, please visit Scott Guthrie's blog

Wednesday, May 28, 2008

UpdatePanel & UpdateProgress in ASP.net Ajax

When adding an UpdatePanel-control to your page, you can achieve a partial-update of your page on a postback. Only the content in the UpdatePanel is refreshed, the other parts of the page remain unchanged.

To let the user know that the network conversation is taking place, the ASP.NET AJAX UpdateProgress control provides immediate feedback by displaying an animated image, ‘loading’ text, or other HTML that disappears once the network response is received.

For more details on how to apply them to the asp.net application, please reference at: http://www.codegod.de/webappcodegod/updatepanel-tutorial-aspnet-ajax-AID281.aspx

Tuesday, May 27, 2008

Dynamic Control (textbox) Creation

One problem I had with the dynamic control creation is the controls are not saved in the viewstate after postback unless you put the code of creating the controls in the Page_Load or Page_Init event.

One of the recent project I worked with required the application to create multiple labels & textboxes on the page according to user's interaction(button-click). I didn't want to create all the controls in the Page_Load and put them to different panels and hide them. One interesting thing I found (tip from co-worker) is that all the information in the textbox are saved in the viewstate no matter is pre-defined or dynamic created. In order to retreive the dynamic created textbox value, Request.Form need to be used.

According to MSDN, "The Form collection retrieves the values of form elements posted to the HTTP request body, with a form using the POST method."

The solution I used is to save the textbox.uniqueid to a hashtable and call the Request.Form[textbox's uniqueid].tostring() to retrevie the value of the textbox.

here is the link to Bilal Haidar's blog of "Loading control dynamically in ASP.net"

Tuesday, May 20, 2008

Dynamic SQL in stored procedure

By using sp_executesql (system stored procedure), you can write the dynamic SQL inside the stored procedure.

sp_ExecuteSql accepts a list of parameters, the first two of which MUST be either ntext, nchar or nvarchar. The first is your sql statement and the second is the parameter names. The third part is the list of parameter values.

Example:
DECLARE @SqlString nvarchar(4000)
SET @SqlString = N'SELECT * FROM ' + @TableName + ' WHERE ID = @ID';
EXECUTE sp_executesql
@SQLString
,N'@ID int'
,@ID

The @TableName and @ID are parameters passed into the stored procedure.

for more info, check:
http://doc.ddart.net/mssql/sql2000/html/acdata/ac_8_con_04_9uek.htm
http://msdn.microsoft.com/en-us/library/ms175170.aspx

Friday, May 02, 2008

Basic C# Generic Predicate Delegate

Find a very useful (practical) example of combining generic, predicate, and delegate together on the List (removing items in the list).

//create a list of candidates
List candidates = new List();
for (int i=2; i <= 100; i++) { candidates.Add(i); } //use predicate and delegate to remove the items on the list candidates.RemoveAll (delegate(int x) { return x>2&& x%2==0; }
);

//or use the predicate only
candidates.RemoveAll(RemoveUnWantedItems);

static bool RemoveUnWantedItems(int x)
{
return (x>2&& x%2==0;);
}

Thursday, May 01, 2008

Passing Value-Type parameters vs Passing Reference-Type parameters

In the real world coding, I rarely think about this issue much. The debugger always shows the values for you when you debug the application. However, when taking an exam (or interview), you will get a question about it and you are asked what is the output look like on the blank paper.

Again, the MSDN reference helped a lot. It listed all the examples with output. Here is the link:
http://msdn.microsoft.com/en-us/library/0f66670z(VS.71).aspx