Counter-attacking Lawak “Beli Satu Je”

Posted by Ikhwan on November 30, 2005

Pernahkah anda dikenakan dgn lawak tipikal seperti berikut:

Anda :

Wah, henfon baru! Mahal ni, berapa kau beli?

Opponent :

Aku beli satu je…

Niat ikhlas anda utk bertanyakan harga barangan tersebut dibalas oleh opponent dgn jawapan yang macam bagus. Cis, memang lah kau beli satu unit je. Lawak tersebut, walaupun dangkal, tetapi serba-sedikit dapat menjejaskan cool anda. Namun jangan terkesima, anda dapat meng”counter-attack” lawak macam bagus itu. Anda cuma perlu bertenang, tarik nafas, dan dgn secara bersahaja meneruskan perbualan seperti berikut:

Anda :

Oh, beli satu je. Apesal tak beli dua, boleh kasi aku satu!

Opponent :

Buat apa nak kasi kau. Pakcik angkat jiran sebelah rumah posmen aku pun aku tak belikan.

Anda :

Mahal sangat ke? Kalau beli dua berapa harganya?

Opponent :

Ringgit Malaysia dua ribu dua ratus.

Lihat, bukan sahaja anda dapat mengekalkan cool anda, namun persoalan yang bermain di benak pemikiran anda dapat terjawab. Malahan anda dapat mengumpul lebih banyak cool points!

IntelliSense Rulez!

Posted by Ikhwan on November 29, 2005

I got a lousy pair of hands for typing. Even though I know the theory of touch-typing, but I can’t seem to master it. I think I got some muscle coordination problem or something. If it’s not because of IntelliSense, I might have to go back at 11.00PM everyday.

AddHandler in Data Bound Controls

Posted by Ikhwan on November 28, 2005

I have a parent-child kind of data, and to list it I need a DataList with a DataGrid in it’s row. Get it? Okay, this looks like a good place to show-off my ASCII arting prowess.

+=DataList==================================+
|   Item1  [remove]                         |
|    +-DataGrid-------------------------+   |
|    |   Item1.1              [delete]  |   |
|    |   Item1.2              [delete]  |   |
|    |   Item1.3              [delete]  |   |
|    |   Item1.4              [delete]  |   |
|    +----------------------------------+   |
+===========================================+
|   Item2  [remove]                         |
|    +-DataGrid-------------------------+   |
|    |   Item2.1              [delete]  |   |
|    |   Item2.2              [delete]  |   |
|    |   Item2.3              [delete]  |   |
|    +----------------------------------+   |
+===========================================+

Actually making the data come out like this is not really that hard. Just make an event handler for the ItemDataBound of the DataList, and add codes to DataBind the child rows to the DataGrid. Like so:

Protected dlParent As DataList
Protected ParentTable As DataTable
Protected ChildTable As DataTable
.
.
Private Sub dlParent_ItemDataBound(ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) _
    Handles dlParent.ItemDataBound
    If (e.Item.ItemType = ListItemType.Item _
        OrElse e.Item.ItemType = ListItemType.AlternatingItem) Then

        Dim dgChild As DataGrid
        dgChild = DirectCast(e.Item.FindControl("dgChild"), DataGrid)
        Dim parentID As String
        parentID = DirectCast(e.Item.DataItem, DataRowView)("ParentID").ToString()

        Dim dv As DataView = ChildTable.DefaultView
        dv.RowFilter = "ParentID=" & parentID
        dgChild.DataSource = dv
        dgChild.DataBind()

    End If
End Sub

The confusing part is how to handle the event for the [delete] button in the DataGrid. The intention is to delete the particular child item from the ChildTable. The first thing comes into my mind is to dinamically attach an event handler ItemCommand to the DataGrid. So I just make an AddHandler in the dlParent_ItemDataBound, and write the appropriate event handler :

.
        ' in the dlParent_ItemDataBound
        AddHandler dgChild.ItemCommand, AddressOf Child_ItemCommand
.
.
' The event handler
Private Sub Child_ItemCommand(ByVal source As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)

    ' Deletion routines here

End Sub

This is where I did wrong. I tried this one over and over. Everything seems fine, but the event handler is just not being invoked at all. After some Googling (like always), I came to a solution. What I should do is make the AddHandler in the ItemCreated event, not in the ItemDataBound event. It worked like a charm. Another job (almost) well done.

Thanks to this page, http://www.codecomments.com/archive289-2004-8-268077.html

ItemDataBound isn’t invoked on postback because the control isn’t databound
back to the source, the viewstate is used. If you put a breakpoint/trace in
ItemDataBound you’ll see it isn’t called. handlers added dynamically don’t
preserve their state on postback, so you need to add them somewhere around
the page_load event (not exactly sure what’s the latest you can get away
with).

You can either put the code in the ItemCreated or in Page_Load if
Page.IsPostBack is true.

Validator Controls in ASP.NET

Posted by Ikhwan on November 22, 2005

One thing that I really like in the ASP.NET framework is the availability of simple to use validation controls. ASP.NET provides several type of validator controls such as the RequiredFieldValidator, CompareValidator, RangeValidator, and RegularExpressionValidator. And if that’s not enough, there’s also a CustomValidator where you can make your own rules.

Back in my PHP days (not so far back), making a textbox to be mandatory filled up is quite a hassle. I need to write JavaScript to make sure it’s not empty before sending, and then check back at the server-side in case the user is a smarty-ass and manage to bypass the JavaScript. With ASP.NET (using Visual Studio), I can drag-drop a RequiredValidator, specify which control to validate (maybe a textbox, a dropdown list, or whatever), change the text to say “Hey dude, we need your name. Please type it in!” as the error message and we’re done!

JavaScript checking is done for you. At the server side, check the boolean value Page.IsValid to make sure all validations passed with flying colors before proceeding with inserts/updates routines etc. You can list all errors using ValidationSummary control, or even make it to popup an alert() box.

If you wanna be naughty and really get down and dirty, you can look at “C:\Inetpub\wwwroot\aspnet_client\system_web\1_1_4322\WebUIValidation.js”. This is the generic JavaScript file that supply all validation functions. Your custom control can also be made validatable (is that a legal word?). Put the ValidationPropertyAttribute in your custom control declaration so a validator can hook-up on it, like so :

[ValidationPropertyAttribute("Number")]
public class PhoneNumber : WebControl, INamingContainer
.
.

But this one is quite tricky (making a custom control is always tricky), especially for composite controls.

I don’t have time to tell more, but there should be abundant resources on the Internet that you can read up. Some links for you.
- Google result on “asp.net validator”
- MSDN page on “BaseValidator” (the parent of all validators)
- An article that I found

The Mythical Super Programmer?

Posted by Ikhwan on November 22, 2005

Eric Wise blogs about super heros that would exist in the software development world. What’s interesting is that, he said super doesn’t mean perfect. Every Superman has his Kriptonite.

Multiple Item for HTML Class

Posted by Ikhwan on November 18, 2005

I just knew that we can put multiple classes in the “class” attribute for a HTML tag. Observe…

<html>
<style>
.RedBox {
    background: mistyrose;
    border: 1px solid;
}

.AlignedRight   {
    text-align: right;
}

.Spaced {
    letter-spacing : 3px;
}
</style>
<body>
<input type="textbox" class="RedBox AlignedRight Spaced" value="0.00" />
</body>
</html>

p.s: Got this from some blog. I forgot to keep the URL. Credit for him/her.

Cash Machine Running Windows

Posted by Ikhwan on November 18, 2005

Beberapa hari lepas papit ada blog pasal Malaysian ATM running Windows OS. Asalnya k4ml yg nampak benda tu. Dan pada hari kelmarin, aku sediri menyaksikan fenomena ini dgn mata kepala aku sendiri. Tapi yang ni Cash Deposit machine.

WindowsAtm2
Ni dia machine tu. Maybank punya. Aku tak boleh nak tangkap gambar elok2, ramai korang kat situ.
WindowsAtm1
Ada message box error. Aku tak pasti error apa. Dari caption message box tu memberitahu ini Windows NT.