Tuesday, December 30, 2008

Tip: CalendarExtender In Update Panel with ReadOnly TextBox

From time to time we want to build a date selection in a web form using the Ajax Control Toolkit's Calender Extender and to set a text box that accepts a new date everytime a user chooses a new date to ReadOnly mode so that user cannot modify the text box with unintended date format.  Most of the time, we want to put it inside an UpdatePanel so that only partial of the page is updated. The problem is, the TextBox's TextChanged event if you enable it, it will not fire if the user makes a date selection and changes the content of the text box.

The solution to this problem is to bind the UpdatePanel "Load" event to a function. In this case, any changes in the text box content will trigger the UpdatePanel event.
In the function what we can do is to check the "__EVENTTARGET" returned from the "Request" object. For example, the following code checks whether the postback is caused by the myTextBox text box.

        protected void UpdatePanel2_Load(object sender, EventArgs e)
            if (Request.Params["__EVENTTARGET"] == myTextBox.ID)
                  //DO something

Saturday, December 6, 2008

Tips: How to compress JSON response using GZIP

The main objective for this short article is to write out the method to compress JSON response(using Web Service) and Ajax response(using normal aspx page) using IIS6 Metabase Explorer.

I have been involving in a ASP.NET web project that makes heavy use of JSON technique and Ajax concepts. The web pools for new data from a Windows 2003 web server every 30 seconds. The size of the data can vary from 100KB to 1MB. This poses performance issue for customers that have slow Internet connection.

IIS6 provides the capability to Gzip the web content before sending to the clients. Figure 1 and Figure 2 show the typical setting that i uses in order to compress JSON and Ajax response.

Figure 1: Deflate settings

Figure 2: Gzip settings.

As you can see, the main setting that is related to the JSON and Ajax response GZIP is the HcScriptFileExtensions. I have included the "aspx" and "asmx" extensions in the "Data" column. 

Saturday, October 4, 2008

Tips: Anonymous access gets error 401.1 every time the Domain Controller pushes the domain policy to the web server.

This problem had been troubleing me for a month already. After searching through the web i've finally gotten some hints on the possibilities of what had gone wrong and was able to fix it.

The reason why i received the error 401.1 overnight after i had done an IISRESET to the web server was because of my domain controller was refreshing the web server group policy at a certain time. The refresh happened to remove the IUSR_SERVER anonymous account from the group policy, threfore any anonynmou account trying to browse the web site would get the 401.1 error. If i run the IISRESET again, the group policy will be reset to the web server's original configuration, thus allowing anonymous users to browse the web site.

The solution to the problem is to edit the Domain Controller Domain Policy. Edit the "Access this computer from the network" policy under the "Local Policies-->User Rights Assignment" section and add in the IUSR_SERVER account into the policy. The next time the DC refreshes the web server policy, the IUSR_SERVER is included in the web server policy also. THe picture below shows the actual setup panel.

Saturday, September 13, 2008

Tips: Regular Expressions for validating currency

Javascript Regular expressions for Currency Validation.

_en_US = /(?=\d)^(?:(?!(?:10\D(?:0?[5-9]|1[0-4])\D(?:1582))|(?:0?9\D(?:0?[3-9]|1[0-3])\D(?:1752)))((?:0?[13578]|1[02])|(?:0?[469]|11)(?!\/31)(?!-31)(?!\.31)|(?:0?2(?=.?(?:(?:29.(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|(?:0?2(?=.(?:(?:\d\D)|(?:[01]\d)|(?:2[0-8])))))([-.\/])(0?[1-9]|[12]\d|3[01])\2(?!0000)((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?!\x20BC)))\d{4}(?:\x20BC)?)(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$/;

_ms_MY = /^(?=\d)(?:(?!(?:(?:0?[5-9]|1[0-4])(?:\.|-|\/)10(?:\.|-|\/)(?:1582))|(?:(?:0?[3-9]|1[0-3])(?:\.|-|\/)0?9(?:\.|-|\/)(?:1752)))(31(?!(?:\.|-|\/)(?:0?[2469]|11))|30(?!(?:\.|-|\/)0?2)|(?:29(?:(?!(?:\.|-|\/)0?2(?:\.|-|\/))|(?=\D0?2\D(?:(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|2[0-8]|1\d|0?[1-9])([-.\/])(1[012]|(?:0?[1-9]))\2((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?:$|(?=\x20\d)\x20)))\d{4}(?:\x20BC)?)(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$/;

_zh_CN = /^(?=\d)(?:(?!(?:1582(?:\.|-|\/)10(?:\.|-|\/)(?:0?[5-9]|1[0-4]))|(?:1752(?:\.|-|\/)0?9(?:\.|-|\/)(?:0?[3-9]|1[0-3])))(?=(?:(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:\d\d)(?:[02468][048]|[13579][26]))\D0?2\D29)|(?:\d{4}\D(?!(?:0?[2469]|11)\D31)(?!0?2(?:\.|-|\/)(?:29|30))))(\d{4})([-\/.])(0?\d|1[012])\2((?!00)[012]?\d|3[01])(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$/;

_zh_HK = /^(?=\d)(?:(?!(?:1582(?:\.|-|\/)10(?:\.|-|\/)(?:0?[5-9]|1[0-4]))|(?:1752(?:\.|-|\/)0?9(?:\.|-|\/)(?:0?[3-9]|1[0-3])))(?=(?:(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:\d\d)(?:[02468][048]|[13579][26]))\D0?2\D29)|(?:\d{4}\D(?!(?:0?[2469]|11)\D31)(?!0?2(?:\.|-|\/)(?:29|30))))(\d{4})([-\/.])(0?\d|1[012])\2((?!00)[012]?\d|3[01])(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$/;

Thursday, July 3, 2008

Tips: Session is lost when using showModalDialog.

This is a known problem of the IE when one is calling the "showModalDialog" in his/her JavaScript codes. The workaround for this problem is to add "<base target="_self" />" to the head section of the dialog page.

Monday, June 23, 2008

Tips: How to create an animation using the MS AjaxToolkit's JavaScript

First of all, install the MS Ajax Extension 1.0, then add the following ScriptManager into your aspx page.

<asp:ScriptManager ID="ScriptManager1" runat="server" LoadScriptsBeforeUI="false">


<asp:ScriptReference Assembly="AjaxControlToolkit" Name="AjaxControlToolkit.Common.Common.js" />

<asp:ScriptReference Assembly="AjaxControlToolkit" Name="AjaxControlToolkit.Compat.Timer.Timer.js" />

<asp:ScriptReference Assembly="AjaxControlToolkit" Name="AjaxControlToolkit.Animation.Animations.js" />



Add the following JavaScript codes into your aspx page.

<script type="text/javascript">

//Get a handle to the animation Target. The target can be anything like a 'div', 'table', etc

var target = $get("animationTarget");

//Create an instance of the FadeAnimation .

var fadeAnimation = new AjaxControlToolkit.Animation.FadeInAnimation();

//Set the Duration

fadeAnimation.set_duration( 1.0 );

//Set the Animation Target as a Sys.UI.Control object

fadeAnimation.set_target( target );

//Set the Maximum Opacity Value

fadeAnimation.setValue( 50 );

//Play the Animation



Monday, June 9, 2008

My ISP TM Streamyx Sucks

Today is not a happy day for me. I have been arguing with my local ISP about chaning my faulty modem. I have no idea that changing faulty modem requires a lot of energy and patience. I am out of breath about this matter. Conclusion is it's sucks. Hey i am a techie guy, internet is utmost important to me when come to work.


Sunday, May 18, 2008

Tip: How to solve "__pendingCallbacks[...].async is null or not an object"?

Recently i came across this error "__pendingCallbacks[...]।async is null or not an object " when creating a callback function for a particular web page. After searching the web for a while, I solved the problem by re-creating the "WebForm_CallbackComplete" function in my callback page. The culprit to the problem is the variable "i" in the loop. Just change the "i" to "var i".

function WebForm_CallbackComplete() {
for (var i = 0; i < __pendingCallbacks.length; i++) {
callbackObject = __pendingCallbacks[i];
if (callbackObject && callbackObject.xmlRequest && (callbackObject.xmlRequest.readyState == 4)) {
if (!__pendingCallbacks[i].async) {
__synchronousCallBackIndex = -1;
__pendingCallbacks[i] = null;
var callbackFrameID = "__CALLBACKFRAME" + i;
var xmlRequestFrame = document.getElementById(callbackFrameID);
if (xmlRequestFrame) {

Saturday, April 19, 2008

ASP NET: How to secure a web page in a sub directory using role for your web site?

In ASP.NET 2.0, we can use the built-in role-based security feature to control who can access certain directories within our web site. It's quite straight forward to setup some configurations in the Web.config file that define the roles that can access a direcotry. For example,

1 <location path="MemberDirectory">
2 <system.web>
3 <authorization>
4 <allow roles="MEMBER" />
5 </authorization>
6 </system.web>
7 </location>

But to control the role that can access a specific file in a sub-directory within our site requires us to set it differently. The following example shows how it can be done:

1 <location path="Admin/UserManagement">
2 <system.web>
3 <authorization>
4 <deny users="*" />
5 </authorization>
6 </system.web>
7 </location>
9 <location path="Admin/UserManagement/users.aspx">
10 <system.web>
11 <authorization>
12 <allow roles="ADMIN" />
13 <deny users="*" />
14 </authorization>
15 </system.web>
16 </location>

The 1st line shows the setting to deny all users to access the particular sub-directory.
The 9th line shows the setting to allow access to individual web page for the ADMIN role.
In order for any page in the UserManagement sub-directory to be shown in a MS ASP web menu, you need to include all the individual pages in the setting.

Sunday, March 23, 2008

ASP.NET: Object Reference not set to an instance of an object.

I encountered the folliwing error when trying to run a web site hosted under the IIS in anew machine. Basically in order to solve the problem, run aspnet_regiis -i to install ASP.NET 2.0.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] System.Web.Hosting.ISAPIWorkerRequestInProc.GetServerVariable(String name) +1618 System.Web.Security.WindowsAuthenticationModule.OnEnter(Object source, EventArgs eventArgs) +593 System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64

Wednesday, February 20, 2008

Tip: Securing ASP pages in menu.

It comes to me recently that i have to control access a bunch of web pages for a new web site based on a set of roles that i have defined using the ASP.NET 2.0 Web Administrator Tool. ASP.NET 2.0 has provided the Authorization and SiteMap components that i can use to assign roles to a particular web page. Following are the steps to assign role to a particular directory and web page.

1. Add a new Web.config file into the directory that you want to control access. For example, I
put a config file into the Admin folder.
2. Modify the Web.config file to include the following statements:

1 <?xml version="1.0"?>

2 <configuration>

3 <system.web>

4 <authorization>

5 <allow roles="Administrator"/>

6 <deny users="*"/>

7 </authorization>

8 </system.web>

9 </configuration>

3. In the Web.siteMap file, add the following line. By adding "Administrator" as the roles for that particular web page, an admin login will display the "Admin Page" but not the "Game Page".

1 <?xml version="1.0" encoding="utf-8" ?>

2 <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

3 <siteMapNode url="default.aspx" title="Home" description="" roles="Administrator, Agent">

4 <siteMapNode url="~/Admin/test1.aspx" title="Admin Page" description="" roles="Administrator"/>

5 <siteMapNode url="~/Game/test2.aspx" title="Game Page" description="" roles="Agent"/>

6 </siteMapNode>

7 </siteMap>

Thursday, January 31, 2008

Tip: How to enable post back from a custom web control?

In many scenarios I came across the need to use the __doPostBack function to do a post back for my web control or asp page. In order to do so, I have to inherit the control or page from the IPostBackEventHandler interface. However, this is not sufficient because the __doPostBack function will not be generated by the .NET FW. I have to add the "GetPostBackEventReference" function inside my control or page. The sample code below shows a simple implementation:

public classs MyControl: WebControl, IPostBackEventHandler
protected override void OnInit(EventArgs e)
PostBackOptions options = new PostBackOptions(this);
options.ClientSubmit = true;

This enables the implementation of the following user functions:
1. Pass back key press from user computer back to server.
2. Any scenarios that require __doPostBack to be called manually in code.

Tuesday, January 29, 2008

Tip: Embed an Autocomplete Ajax Control into a Web Control

In order to embed an Autocomplete Ajax control in a custom web control, there is a rule that one needs to follow:

1. Place the Ajax control instance after the base.OnPreRender() function. The reason
is that Ajax control needs to get an instance of the Target Control when it's

Tip: Closing an IE Modal Dialog

It's a tedious and time consuming task to find out actually how to close an IE modal dialog generated by calling the ShowModalDialog Javascript function. To make things short, i present only the codes:

//To show the modal dialog.
function DisplayMenu()
var url = "page.aspx";
var winName = "ProblemReportActions";
var height = 280;
var width = 500;
var returnedValue = '';
var params = '';
var returnedValue = ShowModalWindow( url, winName, width, height,params);
if(returnValue != null)
postBack(returnValue); //Javascript Post Back for the main window

//In the C# aspx page add the onclick attributes to the button to close the dialog.
//the first button actualy close the dialog and returns something to the main window.
//the second button basically closes the dialog like a cancel button.
this.bt_ok.Attributes.Add("onclick", "closeDialog('" + this.txtOption.UniqueID + "');return false;");
this.bt_cancel.Attributes.Add("onclick", "window.close();return false;");