Tuesday, June 19, 2007

Auto Fill up Leading Zero(s)

Scenario:
There came another requirement.
When the user didn't key in the max length in the first text box, leading zero(s) will automatically be added to fill up the space.
For example:
Telephone number: (12)-(12345678)
If the user only entered "12", it hasn't reached the max length (3) yet, then a leading zero will be added to become "012".

I'm still using the javascript here. Add the following script in the <head> block:

<script language=javascript>

function AddLeadingZero(currentField)
{
//Check if the value length hasn't reach its max length yet
if (currentField.value.length != currentField.maxLength)
{
//Add leading zero(s) in front of the value
var numToAdd = currentField.maxLength - currentField.value.length;
var value ="";
for (var i = 0; i
< numToAdd;i++)
{
value += "0";
}

currentField.value = value + currentField.value;
}
}


</script>

Copy this function in the code file:

public static void AddLeadingZeroAttribute(TextBox txt)
{
//Add leading zero when focus is lost
txt.Attributes.Add("OnBlur", "AddLeadingZero(this)");

}


"OnBlur" event will be triggered when the textbox loses the focus.
Whenever the user finished keying in values and left the textbox, the AddLeadingZero() function will be called.

Auto Set Focus to Next control

Scenario:
You may want to auto set the focus to the next textbox, when the user has typed the exact length in the current textbox.
For example:
Telephone number: (012)-(12345678)
The max length for the first textbox is 3, after user typed in "012" which already reached the max length, the the focus should automatically set to the next text box.

Thanks for my colleague, Carmen, who helped me to find the following solution.

Add the following script to the <head> block:

Copy this function in the code file:
public static void AddNextTabAttribute(TextBox txt1,
TextBox txt2)
{
//Set next tab attribute when key up
txt1.Attributes.Add("OnKeyUp", "Tab(this, '" +
txt2.ClientID + "')");
}


Whenever the "KeyUp" event has trigged in the first textbox, it'll call the Tab() function and check if it has reached its max length.

Then, call this function in Page_Load event to add the attributes to the textbox.
AddNextTabAttribute(txtPhoneNo1, txtPhoneNo2);

That's it!
I am not sure if there is a better way to do this, I find that it's very powerful to combine Javascript in ASP.NET.

Thursday, June 14, 2007

Update McAfee Virus Definition

I've learnt something new today, and I wish to jot it down, in case I will forget this later. (I'm sure I will forget. :p)

My colleague reminded us to update the virus definition today, but I was having problem in updating it.
Then, my colleague helped me to solve the problem.

1) re-register the ole32.dll in C:\Windows\System32 folder.
2) Start the McAfee Framework Service.
a. Go to Control Panel --> Administration Tools --> Services
b. Look for McAfee Framework Service, right click and select "Start".

That's it.

Tuesday, June 12, 2007

Bugfree application?

Will you promise to deliver a bug-free application?

My senior was saying this to me, hopefully by this week we can solve all the problems, and we'll get a PASS for next week UAT.
Well, I do not know what to respond.
Can I say, "I'll try my best"?
This is a very common answer, isn't it?
But how can I make a promise saying there won't be any bugs or problems during the UAT?
Even Microsoft's products are having bugs after they released and sold to thousands or millions of customers, that's why they came out with all these service packs, isn't it?
I dare not promise for a bug-free application. I just kept quiet and listen to my senior's plan. :p

I'm already trying to meet the deadline, the project period has been shorten, then many new requirements have been added, even until today. Well, I have a few more pending enhancements to be completed by the end of this week. Having to fix the errors, and to add the new features, there are three more days for me to go.

Although I might not be able to do the better for this project, there is something I need to cater for the future projects.
From the internal testing bug list, I realized that I've missed out something during my own testing. (Well, I didn't really have time for my own testing.)
I plan to make a checklist for my future projects, I need to fulfill the checklist before I release my application for others to test. Although I need to start another project before I complete this, I surely need to take at least half a day to make a draft checklist.
Completing the projects is important, learning from the mistakes is as important too.
Never learn from mistakes, no matter how many projects I have completed, I didn't learn a single thing. That's not good.

Keep going. Work happily and live happily. ^_^

Hit Enter Key in ASP.NET page

Scenario:
It's very common for the user to hit the Enter key after they have entered the required data in the text box.

For example, when you want to Login.
After keying in your user name and password, instead of clicking on the "Login" button, you may also hit the Enter key to login.

Solution 1:
In order to do this, ASP.NET 2.0 has provided a property in Form and Panel controls, which is "DefaultButton".
You can set the DefaultButton property to the button name, so that whenever user hits the Enter key in the form or panel, the button clicked event will be triggered.

Solution 2:
Well, I was having an issue with the DefaultButton.
I'm not using panel to group my controls, as panel is having some problem with IE7.
I've found a solution with using javascript, it's simple and nice to use. (You do not need to edit your current UI for this.)

Create this function:

public static void AddEnterKeyAttribute(TextBox txt, ImageButton iBtn)
{

txt.Attributes.Add("onkeydown",
"if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" +
iBtn.UniqueID + "').click();return false;}} else {return true}; ");
}


The keycode for enter key is 13.
When a key is pressed in textbox it will check the keycode is 13. If it is 13 the relevant button will be clicked.
event.keycode will not work with firefox, so we are using event.which.

You can create multiple similar functions for different web controls.
Perhaps you can also create a general function which accept any kind of web controls. I haven't try this yet.

You just need to add this in Page_Load event, for all the web controls that you want to trigger the button clicked event when user hits the Enter key:

AddEnterKeyAttribute(txtPassword, ibtnLogin);

It's done. It's just that simple. ^_^

Ref: The Code Project - Solving out ASP.NET enter key problem