Wednesday, October 29, 2008

What's the difference between refresh and postback?

1) typing in a url is like going to the page for the first time so its not a postback. If you browser has cached the page, you may actually go nowhere.

2) clicking on an asp:button fires a javascript that submits your form and is detected at the server as a postback for the page.

3) hitting a browser refresh (F5) takes whatever post data is currently in the buffer (from the last time the page form was submitted) and resubmits it. It does not submit current form values.

Item 3 is usually the most problematic to deal with.

If you refresh the page and it has never been posted back, then its like you are hitting it for the first time on each refresh.

If, however, the page had already been posted back, then hitting refresh repeats whatever event cause it to be previously posted back. This repeated event may not be an issue, but there are defineated times when it would be disastrous to repeat a postback event.

There are mechanisms around to detect when a page that has already been posted back is being refreshed.


Monday, October 27, 2008

What kinds of paging are there?

(1) user interface paging
* It is easier to use,
* Lower performance because all data actually are loaded in the memory of the portal server.
* Support data source like Collection, DataSet, DataTable, DataView. Does not support DataReader. In my portal, the portal calls the web service to create a DataTable, which then is used to render a GridView. So this is a user interface paging. However, the web method can return only one page so as to simulate the paging behavior.

(2) data source paging
* Enable you to write custom logic to retrieve pages of database records.
* Can perform paging for store proc, Linq or a SQL query

Sunday, October 26, 2008

Should ViewState be on or off when displaying a large number of records?

ViewState should be turned off. This way not all data will be transferred via the wire.

Disabling ViewState does not prevent GridView from editting/deleting records, sorting, paging.

How to page in GridView without loading all data into memory?

Use objectDataSource

what is the difference between GridView, DataList, ListView, Repeater, ListControl, DetailsView, FormView?

(1)Can select one row to do something.
(3)update, delete records in database, but not insert
(4)Sort, paging
(5)Concurrency handling
(6)Display empty data
(7)Formatting (e.g. alternative row style)
(8)Customize field (e.g. use TemplateField to edit record)
(9)Extend (e.g. create LongTextField for GridView)

(1) template driven
(2) can add data to database
(3) can edit, page through, sort database data
(4) ListView is the only control implementing IPageableItemContainer interface, so it is the only one which can use DataPager control.

ListView is the most flexible List control in all. I can safely use ListView in place of GridView.

(1) display database records. It cannot edit/insert records
(2) entirely template driven

(1) display database records(can edit?)
(2) entirely template driven
DataList is very similar to Repeater except that DataList's default behavior is rending the content using a table. However, you can override the default behavior.

(1) Display/edit/delete/Insert records in database
(2) Render a table for a single record. always render each field in a seperate table row.

Exactly like DetailsView except that FormView is entirely template driven.

there are RadioButtonList, DropDownList, CheckBoxList, BulletedList, ListBox.

Refer: <>

Friday, October 24, 2008

The type System.Collections.Hashtable is not supported because it implements IDictionary..

My portal cannot consume a WCF web method which has HashTable as the input parameter.

The exception I got is:

exception: System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]: The type System.Collections.Hashtable is not supported because it implements IDictionary..

What made me desperate is that I changed the parameter from HashTable to ArrayList and even object[]. But I still got the same error when calling this web method.

...Eventually, I found at the end of this web method, there is "typeof(HashTable)". That means HashTable cannot be a parameter or exist anywhere in the web method!

The service cannot be activated due to an exception during compilation

The service 'myservice.svc' cannot be activated due to an exception during compilation. The exception message is: The HttpGetEnabled property of ServiceMetadataBehavior is set to true and the HttpGetUrl property is a relative address, but there is no http base address. Either supply an http base address or set HttpGetUrl to an absolute address..

The story is:
My portal cannot consume the web service, then I saw this event log in window log.

I want to check whether the WS deployment is good, so I changed HttpGetEnabled from false to true so that I can see the metadata in IE.

But setting HttpGetEnabled=true also need set HttpGetUrl as an absolute address. To avoid this, I changed the HttpGetEnabled back to false and then this error is gone.

Thursday, October 16, 2008

Could not establish secure channel for SSL/TLS with authority

My portal throws this exception when making a web method call to the web service.

Check list:

1. On server and client side, the server certs and their intermediate and root certs do not expire.

2. On server and client side, the server certs have right permission setting (e.g. giving permission to Network Service).

3. On the client side, the client cert used by WCF web method call and its intermediate and root certs do not expire. This is required because the server IIS SSL setting can be "requiring client cert".

4. On the client side, the client cert used by WCF web method call has right permission setting (e.g. giving permission to Network Service). This is required because the server IIS SSL setting can be "requiring client cert".

5. On server and client side, check the server certs are used by IIS.

6. Another experience: I found when an authenticated page expired, the WLID sign in page shows the sign in dialog in the left.htm frame. I figured out it is because the SSL cert is wrong (not for my portal's url). Then I made the IIS SSL cert to the one using the right portal url. After I changed the SSL cert for the machine where both the portal and the web service are deployed, I verified all above 5 points successfully but still got the error as the subject. Finally, I found the portal's web.config should change the WCF endpoint address from https:\\mymachinename\endpoint.svc to https:\\\endpoint.svc.

7. Another experience: when my portal and the web service is deployed to 1box using installserver.bat, IIS uses SSL cert Usually I use URL and SSL cert for my portal when it is deployed seperately. I always thought that to establish SSL channel between portal and WS, both side need to have the URL match the SSL cert. However, this is wrong! To establish SSL channel, only server side (WS side)'s SSL cert is verified.
My collegue help me address the issue eventually.
First, he pointed out that client side (portal side) does not need have a SSL cert which matches the portal URL. So I can get out of the trap of trying to make cert and URL match on both server side and client side.
Second, by the exception message: "The remote certificate is invalid according to the validation procedure.", he asked me to try to access web service using IE to see if there is any SSL warning. Under his expectation, IE showed SSL warning when I access the endpoint address.
Third, he logged in my machine and clicked the certificate error icon besides the IE url column. He viewed the https cert by clicking the "View certificate". He found the cert name is something like authmod, which is another site on the 1box. By this clue, he found site authmod also uses port 443 which is used by my portal site. After he removed https binding of authmod site, my portal start working.
I learned a lot from this lesson.

Wednesday, October 15, 2008

How to restore the machine after I ruined an 1box so that a new 1box can be installed on this machine?

I installed an 1box. I copied and deleted some files/folders to try something out. Then I used the uninstall script to uninstall the 1box. When I ran the install script to reinstall the 1box, I found a database cannot be installed. How can I restore my machine so that I can install a new 1box correctly?

I got a solution for this case. When I ran "msiexec /x mydb.msi" to uninstalled this db component, I got error. Then I copied mydb.mdf and mydb.ldf from another machine over and installed this db manually. After this, "msiexec /x mydb.msi" works correctly. After uninstalling mydb, I copied the commandline from the 1box installation script and ran it in a command window. Then this db is installed correctly by this commandline. The commandline for installing coredb is:
"start /wait msiexec /i MyDB.msi SEV_COREDB_NAME=MyDB /l*v installMyDB.log"

Sunday, October 12, 2008

Why RadioButton OnCheckedChanged is not fired?

I added two radio buttons with the corresponding OnCheckedChanged event handlers to my portal. However, when I switched the radio buttons, OnCheckedChanged event was not fired and the event handlers were not called.

The reason is that these event handlers need a postback to be triggered. After I added AutoPostBack="true", it worked.

Why Response.Redirect does not work in Global.asax.cs:Application_Error()?

My portal uses Application_Error() to redirect to my ErrorPage.aspx. But Response.Redirect() does not work. I debugged to know that this statement was executed but redirect did not happen.

Then I created a sample website to find out that Server.ClearError() is critical. Without this statement, Response.Redirect does nothing. On the other hand, customError section is totally irrelevant.

What is the difference between Response.Redirect and Server.Transfer?

Server.Transfer transfers page processing from one page directly to the next page without making a round-trip back to the client's browser. This provides a faster response with a little less overhead on the server. Server.Transfer does not update the clients url history list or current url. Response.Redirect is used to redirect the user's browser to another page or site. This performas a trip back to the client where the client's browser is redirected to the new page. The user's browser history list is updated to reflect the new address.

You can have Response.Redirect("") but you cannot have Server.Transfer("");

Wednesday, October 8, 2008

[ConfigurationErrorsException]: '.', hexadecimal value 0x00, is an invalid character. Line 2, position 1.

I got this error after I used powershell script to change debug="false" to debug="true" for all web.config.

I found nothing wrong by comparing the deployed web.config and the web.config in the source tree.

It turned out an encoding issue after I re-saved the web.config using UTF-8 in notepad.

I modified the string replacing script to control the encoding.

$pattern ='debug="false"'
$replacement ='debug="true"'
$encode="default" # can be utf8 unicode default (it is actually ANSI)

foreach ($file in (gci . -name $targetFile -rec)) {

if($file -match "PortalUI")
write-host process $file ...
write-host resave file using $encode
$text = get-content $file
$text | out-file $file -encoding $encode

$text = get-content $file

if ($text -match $pattern) {
write-host modifying $file
$text -replace $pattern, $replacement | out-file $file -encoding $encode

How to debug into web service?

Sometime, I need to debug into web service from my portal. It is easy to get an error dialog telling something like "cannot automatically debug into server" when you step in the web method. After I make 100% sure that the pdbs are absolutely in sync with the dlls, I did two things to make it work:

(1) enable debug in all web.config files.
(2) specifiy the symbols path in the project options.

Another time, when I debug from my portal to web service A and to web service B, service B prompt the same error dialog. I thought it is unsync PDB problem again. But after checking the event log, I found the cert expired for service B. After renewing the cert, I can debug into B.

How to replace strings in multiple files using powershell?

I want to replace string debug="false" with debug="true" for all web.config files in the deployment dir. Here is how to do it:

$pattern ='debug="false"'
$replacement ='debug="true"'
foreach ($file in (gci . -name "web.config" -rec)) {
$text = get-content $file
if ($text -match $pattern) {
$text -replace $pattern, $replacement > $file

Thursday, October 2, 2008

How to do buddy build using a batch file?

This is my ApplyDPK.cmd:

echo sd revert ...
call sd revert ...

echo delete .\portals\
call rd /s/q .\portals

echo sd sync -f ...
call sd sync -f ...

echo sdp apply %1 -a
call sdp apply %1 -a

echo delete ..\target\debug\i386
call rd /s/q ..\target\debug\i386

echo pushd portals\source
call pushd portals\source

echo build -cZP
call build -cZP

I can quickly do buddy build by running "ApplyDPK 519".

SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.

My portal tries to write an event log but got the error as the subject.

Reason: to enable event log for my portal, I need to register source in the regedit.
For example, to create a source for partner portal on Vista, import the reg file containing below content:

Windows Registry Editor Version 5.00


Wednesday, October 1, 2008

Why does the javascript registered by Page.ClientScript.RegisterStartupScript not execute?

On my portal, the javascript registered via Page.ClientScript.RegisterStartupScript does not execute, which I used [script]alert("something")[/script] to confirm.

Since I created my portal web app in VS2008 with AJAX enabled, I thought there is some incompatibility between Page.ClientScript.RegisterStartupScript and AJAX. After searching online, I found a lot of posts about how to use ScriptManager.RegisterStartupScript to replace Page.ClientScript.RegisterStartupScript to make the registered javascript run. But this solution did not resolve my problem - the javascript still did not work.

Then I found below code snippet which I think I can use to test whether my page is using AsyncPostBack or syncPostBack.

if (ScriptManager.GetCurrent(base.Page).IsInAsyncPostBack)
ScriptManager.RegisterStartupScript(this, this.GetType(), “Rating_Script_” + this.ClientID.ToString() , script.ToString(), false);
this.Page.ClientScript.RegisterStartupScript(this.GetType(), this.ClientID, script.ToString());

To my surprise, running this code snippet shows my page is using sync post back. So I suspect that my problem may have nothing to do with AJAX.

By looking the code, I found so called "AJAX enabled" web app actually just put [asp:ScriptManager ID="ScriptManager1" runat="server" /] in the master page and that's it. It does not mean you have used any AJAX functionality yet. So my pages are actually just "AJAX enabled" but having no any AJAX functionality (e.g. UpdatePanel).

I stepped back and thought the problem without AJAX. Eventually, I found the problem is with the javascript itself. After I corrected the javascript, the javascript began to execute.

Before correcting:
[javascript]function master_startup() {alert("something")}[/javascript]

After correcting: