Read, With the Name of Your Lord Who Created

Archive for the 'Programming' Category


Four Ways Javascript Binding Event Listeners

Posted by triaslama on July 22, 2008

There is several ways in how to bind an event in Javascript. As far as I know four ways exist on behalf of binding a Javascript event. Here brief description of each way

1. Through inline HTML Code
The Javascript event declared inline in HTML code, for example:

<input type=”button” value=”Alert Message” onclick=”showAlert()” />

Then showAlert() is a Javascript function that merely show an alert message:

function showAlert()
{
      window.alert("Hello, World!!!");
}

This inline event binding works for all browsers.

2. Traditional Binding
I think this way is more elegance than the first one, but before we can do this we must get the desired element. We can use two useful methods of document object to get the element we want, there is getElementById() and getElementsByTagName(). Both method receive one string parameter which show the ID of an element (for getElementById()) or elements name (for getElementsByTagName()). Please aware that the result of getElementsByTagName() is always array of element.
Suppose that we have the following page:

Read the rest of this entry »

Posted in Javascript, Programming | Tagged: , , , , , | 3 Comments »

Javascript Event Phases: Capturing and Bubbling

Posted by triaslama on July 3, 2008

Events is something useful in programming world, because with events program can react based on user action. Javascript is no exception and I think one of Javascript strength is Events. There is many interesting things in Javascript events one of them are the Javascript event phases: Capturing and Bubbling.

Two Phases of Javascript Event: Capturing and Bubbling
Capturing phase is an event moving down the DOM tree to the element that instantiated event. Bubbling phase occuring after Capturing phase. Bubbling phase begins from the source of the event (e.g. button click) traverses up DOM tree to the root element.

Let’s see the following document structure:

<body>
<form>
<input type=”button” />
</form>
</body>

When we click the button, the event captured by the document first, then <body> element, <form> element, and finally the <input type=”button” /> element (capturing phase). After that the event moves back, started from button element (button click event handler fired), <form>’s click, <body>’s click, and document click event handler fired (bubbling phase).

For the complete example try the following (evtphases.htm):
Read the rest of this entry »

Posted in Javascript, Programming | Tagged: , , , , , | No Comments »

The Differences of ASP.NET Forms and Windows Forms in a Nutshell

Posted by triaslama on June 24, 2008

I think its quite interesting if we compare two things that has equalities and differences. Now we will talk about Forms (WebForms and WinForms).

As we already know WebForms uses browser as presentation (ASP.NET Forms is no exception) and Windows Forms has its own presentation. So we may agree that WebForms has the strength through its availability and WinForms through its rich user interface.

In this post I will talk about the differences, what I get from both forms (ASP.NET Forms and Windows Forms) at a glance. I hope this post not tedious for those who already familiar with ASP.NET and Windows Forms.

Base Class
An ASP.NET Form inherits Page class (contained in System.Web.UI namespace). Look at the following example:
Presentation page (Welcome.aspx):

    <%@ Page Language=”C#” debug=”true” CodeFile=”Welcome.aspx.cs” Inherits=”Welcome.HelloWorldClass” %>

    <html>
    <head>
    <title>Welcome To ASP.NET Form</title>
    <script lang=”C#” runat=”server”>
    </script>
    </head>
    <body>
    <form runat=”server”>
    <center><asp:Label id=”label” Font-Size=”25px” style=”font-weight:bold;” runat=”server” /></center>
    </form>
    </body>
    </html>

 

This is the code behind (Welcome.aspx.cs):

Read the rest of this entry »

Posted in .NET, ASP.NET, C#, Programming | Tagged: , , , , | No Comments »

Martial Arts and Computer Programming, The Equalities and Differences

Posted by triaslama on June 20, 2008

Computer Programming

Martial Arts

(Both images taken from sosamartialarts.com and degreedirectory.org)

First and foremost, this is only my own opinion, you not necassary agree with me or maybe you have your own opinion. What I write here maybe not reflect the reality of both (martial arts and computer programming). Second I write this with “As Far As I Know” assumption.

In this post I just tempted to unveil what is reside in my mind. By now I want to talk about equalities and differences between martial arts and computer programming.

Lets start with the equalities:

Sometimes it is suck, but it is rock!
Learning martial arts can be so suck, the same thing happen with computer programming! But best of all both of two things is rock! So your sacrifice is worth!

It is good when you enjoy it!
What is better than do thing that we like? So its really good for us especially when we enjoy doing it!

Read the rest of this entry »

Posted in Martial Arts, Programming, Thoughts and Opinions | Tagged: , , , , , , | 3 Comments »

Cursor Positions, Selected Text, and A Simple HTML Editor

Posted by triaslama on June 6, 2008

It would be nice if we have a HTML text editor that doing a simple task of HTML editor for us. It works just like HTML view of WordPress admin page when we write a post. We select the text press specific button and then the selected text will be enclosed with appropriate format. I Test the code in Mozilla Firefox and Internet Explorer, so in both browsers this code should works fine. Here the screenshoots:

Screenshoots page.

We need to prepare the prerequisites.

As prerequisites, at least we need two things. First, we need to know how to define the cursor position inside a <textarea> element, second we need to know how to retrieve a selected text inside a <textarea> element. So, for the first step, I will talk about the both things shortly.

Every browser brings the different behaviour. The browser quirk will make our code a little bit longer because we need to specify different code for different browser. Defining cursor position (current position) is trivial in Mozilla Firefox, but we need a little trick in Internet Explorer. If we want to retrieve selected text in a <textarea> element, both (Forefox and IE) has their own way too.

Defining Cursor Position Inside a <textarea> Element
In Firefox we just need the following code to determine the current position of cursor in a <textarea> element:

var currentPosition = txtArea.selectionStart;

txtArea is a <textarea> element.
Internet Explorer (I use IE 6) doesn’t have selectionStart and selectionEnd property, so we need a little more effort to determine start and end position of the cursor (this is a tricky way).

var range = document.selection.createRange();
var drange = range.duplicate();
drange.moveToElementText(txtArea);
drange.setEndPoint(”EndToEnd”, range);

var currentPosition = drange.text.length - range.text.length;
var endPosition = drange.text.length - currentPosition;

Read the rest of this entry »

Posted in HTML / CSS, Javascript | Tagged: , , , , , , , | 6 Comments »

C# 3.0: Inferred Type Variables, Extension Methods, and Lambda Expressions

Posted by triaslama on May 27, 2008

C# 3.0 brings many of new features. Some of features developed from the existing one (such as lambda expressions that provides more concise syntax than anonymous methods). Other features can be considered as totally new (such as LINQ). In this post together we will learn about Inferred type variables, extension methods, and lambda expressions.

By reading this article I assume that you are familiar with C#, knowing the previous features of this language is a plus.

Inferred Type Variable
Inferred type variable presented through ‘var’ keyword. If you are a Javascript programmer you must be already familiar with ‘var’ keyword. But, ‘var’ in C# 3.0 has different meaning with ‘var’ keyword in Javascript. In C# 3.0 when we use ‘var’, we tell the compiler that the type of variable should be inferred from the assignment. In Javascript var means that a variable can hold any kind of type.
Consider the following Javascript file:

    var myVariable = 5;
    window.alert(myVariable);
    myVariable = “I change the type of myVariable, now myVariable is a string!”;
    window.alert(myVariable);

 

At the beginning I assign myVariable with 5 (an integer), but later, I assign myVariable with a string (so myVariable can hold any kind of value). But its not the case with ‘var’ in C# 3.0.

using System;

class InferredTypeVar
{
   static void Main()
   {
      var myVariable = 12.5;
      Console.WriteLine("myVariable: "+myVariable);
   }
}

 

Because I assign 12.5 to myVariable then type of myVariable will be double. If we try to fake the compiler and add the following code (hoping that it will the same as in Javascript):

Read the rest of this entry »

Posted in C# | Tagged: , , , | 4 Comments »

Mono Winforms Became Feature Complete

Posted by triaslama on May 24, 2008

This news is a bit too late, but I think it better than nothing at all! Jonathan Pobst has annouced in his blog that Mono Winforms now is API complete. This is what Jonathan said about that:
“we hit a very important milestone in our support for Winforms. We are API complete, which means that our public API is exactly the same as .Net’s (all 12,776 methods)”

Beside that Atsushi Enomoto has annouced that the next release of Mono will include XIM support on Windows Forms. Look at what he said about this:
“So if you are living in multi byte land, try the next release or daily build version of mono and feel happy”
Hmm, I think it really a good news for one of my friend.

Congratulations for Mono team, especially for Mono Winforms team!

Posted in Mono, News | Tagged: , , , | No Comments »

Pagination in ASP.NET

Posted by triaslama on May 18, 2008

ASP.NET has DataGrid control, and datagrid control has build in support for pagination. What I do in this post is just something like datagrid pagination, but I add two buttons each of them to navigate to previous and next page respectively.

I use ListOfPage class to produce list of page number, you can see more on this class in my previous post here. For simplicity I use array of string as data source, in more common scenario maybe we use data from database, represent it in DataTable then use the DataTable as a data source.

I do the manual pagination in ASP.NET with code behind style. There is I put the code that produce list of page in a separate file (I named it ManualPagination.aspx.cs). The presentation page laid in ManualPagination.aspx which refers to our code behind file.

Let’s start with presentation page! It’s quite simple, this is the source code (ManualPagination.aspx):

    <%@ Page Language=”C#” CodeFile=”ManualPagination.aspx.cs” Inherits=”ManualPagination.CodeBehindClass” %><html>
    <head>
    <title>Manual Pagination in ASP.NET</title>
    <style type=”text/css”>
    .clr
    {
    background-color:lightblue;
    }
    </style>
    </head>
    <body>
    <form runat=”server”>
    <asp:Label id=”lblPresentation” runat=”server” />
    <p />
    <asp:Button id=”btnPrev” text=”Prev” onclick=”btnPrev_Click” runat=”server” />

    <asp:Label id=”lblPages” runat=”server” />

    <asp:Button id=”btnNext” text=”Next” onclick=”btnNext_Click” runat=”server” />
    </form>
    </body>
    </html>

 

One of the important portion of our presentation page relies on the following code:

Read the rest of this entry »

Posted in ASP.NET, C#, Javascript | Tagged: , , , , , , | No Comments »

Belajar Dengan Mendengarkan

Posted by triaslama on May 16, 2008

Postingan ini saya tulis setelah saya secara tidak sengaja mengikuti MSDN Day. Kenapa tidak sengaja? Ya karena saya sebenarnya tidak pernah merencanakan untuk ikut dan malahan tidak tahu kalau pada tanggal 15 Mei kemaren ada acara itu.

Hal ini bermula dari saran salah satu teman, Anthony, supaya saya kirim e-mail ke Pak Risman Adnan salah satu lead developer Microsoft Indonesia. Intinya kami meminta waktu untuk membicarakan beberapa masalah teknis yang kami hadapi dan kemungkinan - kemungkinan solusi yang mungkin bisa dipakai. Email pun dikirim dan ini cuplikan jawabannya “Tri, kebetulan besok ada acara MSDN Day, silahkan datang untuk diskusi sebelum sesi saya dimulai”.

Sempat juga terlintas dalam hatiku “Kalau besok ada acara itu apa kami sempat untuk diskusi ya?” Tapi ya sudahlah coba saja dulu. Siapa tahu banyak hal yang bisa dipelajari lagi pula sedang ada migrasi server sehingga development harus dihentikan dulu, maka kami memang bisa datang.

Entah karena saya lagi mood, atau karena acaranya lumayan menarik, atau karena pesertanya yang tidak terlalu banyak sehingga saya merasa nyaman, atau karena sebab yang lain, ternyata sampai dengan break makan siang lumayan banyak hal menarik yang berhasil masuk ke kepalaku :)
Misalnya, aku ini termasuk sangat awam mengenai Zend Framework, karena salah satu materinya tentang itu dan ada orang dari Zend yang datang memberikan presentasi akhirnya sekilas aku mendapat gambaran lebih mendalam tentang Zend Framework.

Read the rest of this entry »

Posted in Miscellaneous, News, Programming | Tagged: , , , | 2 Comments »

Page Number Generator

Posted by triaslama on May 13, 2008

One of the way to prevent show all of data in one page is pagination. Pagination is the process organizing information in pages. For instance, the information appear in a web pages is paginated such that appear 10 lines for every page.
Pagination can be a cool way to manage the looks and feels of information. Because too many tabular data often tedious, hard, or we maybe easily miss something if all of information dropped in a page.

So, what is needed to do pagination? I think one of the most apparent is a list of page! I have write a program (a simple program) in C# that generates page number. The input parameters is current page, number of rows in a page, and the amount of rows.

Output of this progam is array of integer, every number reflected page number being showed. And this is the listing of program:

Read the rest of this entry »

Posted in C#, Programming | Tagged: , , , | 3 Comments »