Event in ASP.NET AJAX lib Explained

In ASP.NET AJAX lib, there're 2 important types of events, event for Component (Sys.Component) and event for DOM Element.

Let's revise our old knowledge first.

Component (So, its derived class obtains these characteristics)

  • Has to relation with DOM at all.
  • Has no UI representation (not like Control (derived from Component base class) that has).
  • Encapsulates client codes to reuse it across applications *** this is the purpose of Component that exceeds the original DOM elements. In AJAX lib, DOM elements are represent(encapsulate and enrich, indeed) by Sys.UI.Control base class, which actually derive from Sys.Component. So, Component is not Control, but its super class. Component means AJAX lib can manage that things. Some components you wouldn't see with bare eyes (like timer,etc.). Did you get it?
  • Has Handler (cross-browser) that can bind with client object events.
  • Implement Sys.IDisposable.
  • Can raise the propertyChanged notification event when object property changed.
  • You can access events of that Component using get_ and set_ events.

To manage events of Component, we use Sys.EventHandlerList class. It's a dictionary of client events for a component, with event names as keys and the associated handlers as values. It has 3 methods addHandler, clearHandler, and getHandler. For example, when binding event to Component:

   add_tick: function(handler) {
this.get_events().addHandler('tick', handler); }, remove_tick: function(handler) {
this.get_events().removeHandler('tick', handler); }

 this means anything (any class,whatever) that's derived from Component base class. The very important thing is here:

var h = this.get_events().getHandler('tick');
if (h) h(this, Sys.EventArgs.Empty);

This will execute the handler function h. Remember that this handler belongs this Component. For example, Handler 'tick' (this handler) might be the event that is specified from a page developer outside our authority. We, a component developer, may not know this handler at all. He may want to do some special effects presenting to the user on top of UI layer after the time ticked. Page developer doesn't have an idea what's inside our Component too, he just uses it cluelessly.

On the other hand, event for DOM element, when we attach it to that element we call it event binding to DOM element. For example, I bind 'click' event to my button on the page. When it's clicked, it'll do my handler function. Don't forget that, DOM is not the same as Component. It's just a small subset. Here, it is (complete list of its methods): http://msdn.microsoft.com/en-us/library/bb383775.aspx. I think you're familiar with it, no need to explain much:

//register handler for click event on the expand control
$addHandler(yourElement, 'click', yourHandlerFunction);

$clearHandlers(yourElement);

Note that if this.get_element() that you love returns AJAX lib wrapped up DOM element. So, you can attach it an event like this example as well.

Stay tuned for the very near article. Thanks.

Categories:   AJAX Technology | Javascript | Tip(s)
Actions:   E-mail | del.icio.us | Permalink | Comments (2) | RSS


AJAX Autocomplete (Suggestion) on ASP.NET from dictionary file

Hi, It's been a long time since my last post about google map. This time I'm going to talk about AJAX autocomplete function. Let's see a sample from the most famous website i've known.

Good News is, It's very easy and you can do it. Just follow my step. First, I'll explain how it works. Most AJAX Libraries require response from server. Every information you want to retrieve from server must be in form of "response" or "JSON" (JavaScript object just like XML for JavaScript). Our Application here is one of examples that i'm going to show you how to retrieve information from server by Response object. Either ASP.NET or PHP can deal with response. So AJAX Library can be applied to those language. All you need to do's write a JavaScript to retrieve response from the server side

Here's an step of how this "Autocomplete" Thing work

  1. Load JavaScript library, initialize and wait for input from user.
  2. User type in letter. Value's passed by "FORM" request to a page named "Default.aspx".
  3. Default.aspx read dictionary file and response to client in a form of unordered list text.
  4. Browser show suggestion to user. User can choose and the text in textbox's completed.

How Simple! here my example

Start!

1. Declare input and output, input is for input from user(Form Request). And output's string output (<ul><li>first item</li><li>second item</li></ul>).

1 protected void Page_Load(object sender, EventArgs e)
2 {
3 String output = "";
4 String input = Request.Form["paramX"];
5 }

Declare it in Page_Load event of Default.aspx

2. Deal with string with space. I'll split the line into array of word. and suggest only the last word. Always response the former text.

1 if (input != null)
2 {
3 String[] arrayS;
4 arrayS = input.Split(' ');
5 for (int i = 0; i < arrayS.Length - 1; i++)
6 {
7 output += arrayS[i] + " ";
8 }
9 input = arrayS[arrayS.Length - 1];

3. Open an fileStream and read "dict.txt". Compare words in the file with input (last word of array) and response in unordered list string

1 using (FileStream fs = new FileStream(Server.MapPath("dict.txt"), FileMode.Open))
2 {
3 using (StreamReader reader = new StreamReader(fs))
4 {
5 String tmp;
6 Response.Write("<ul>");
7 while (!reader.EndOfStream)
8 {
9 tmp = reader.ReadLine();
10 if (tmp.Length >= input.Length && tmp.Substring(0, input.Length) == input || tmp == input) Response.Write("<li>" + output + tmp + "</li>");
11 }
12 Response.Write("</ul>");
13 }
14 }

4. Now, we've done with Default.aspx. This page'll act as service that response text from dictionary when an AJAX Library tried to request the list of suggested word. Next thing, we'll build a page that'll use an AJAX library called "Script Aculo" which has an Autocomplete built-in function. Here's the source code

1 <html xmlns="http://www.w3.org/1999/xhtml" >
2 <head runat="server">
3 <title></title>
4 <script src="js/prototype.js" type="text/javascript"></script>
5 <script src="js/scriptaculous.js" type="text/javascript"></script>
6 <script src="js/controls.js" type="text/javascript"></script>
7 <style type="text/css">
8 /*
9 div.autocomplete {
10 position:absolute;
11 width:500px;
12 background-color:white;
13 border:1px solid #888;
14 margin:0;
15 padding:0;
16 }*/
17 ul {
18 list-style-type:none;
19 margin:0;
20 padding:0;
21 }
22 ul li:hover { background-color: #ffb;}
23 ul li {
24 list-style-type:none;
25 display:block;
26 margin:0;
27 padding:2px;
28 cursor:pointer;
29 }
30 </style>
31
32 </head>
33 <body>
34 <form id="form1" runat="server">
35 <div>
36 <input type="text" id="autocomplete" name="autocomplete_parameter"/>
37 <div id="autocomplete_choices" style=" height:150px; overflow:auto;"></div>
38 <script type="text/javascript">
39 new Ajax.Autocompleter("autocomplete", "autocomplete_choices", "Default.aspx", {
40 paramName : "paramX" 41 });
42 </script>
43 </div>
44 </form>
45 </body>
46 </html>

First thing, include an AJAX library in <head>. Load Script Aculo From www.script.aculo.us It require PROTOTYPE.JS (another javascript library)

put this into your <head>

<script src="js/prototype.js" type="text/javascript"></script>
<script src="js/scriptaculous.js" type="text/javascript"></script>
<script src="js/controls.js" type="text/javascript"></script>

Include downloaded script into your <head>. Then put input box into your <body>

<input type="text" id="autocomplete" name="autocomplete_parameter"/>
<div id="autocomplete_choices" style=" height:150px; overflow:auto;"></div>

Last thing to do! Put javascript after that.

<script type="text/javascript">
new Ajax.Autocompleter("autocomplete", "autocomplete_choices", "Default.aspx", {
    paramName : "paramX"
});
</script>

As usual! Compare your result here.

Find Direction From Your home to AnyWhere? Put in action

Now we've learned how to put google map into your website. Next big step is how to use Google API efficiently. In this example, i'm going to show you guys how to find direction, show it on map and literary detail about how to get to that position.

Our scenario is : I want to find the way from my home(fake location of cos lol) to Salaya, Central Pinkrao And Siam Centre.

1. From last example see it here or you can find example code and view it source.

2. Let's edit it a little bit. Put new Table. Then put 3 button, new div Tag called "detail".

<table style="width:100%;"> 
<tr>
<td width="800"> 
<input type="button" value="Salaya, Mahidol" /> : <input type="button" value="Central Pinkrao" /> : <input type="button" value="Siam Center" />
</td>
<td>&nbsp;</td> 
<tr> 
<td width="800"> 
<div id="mapCanvas" style="width: 800; height: 600"></div> 
</td> 
<td> 
<div id="detail"></div></td> 
</tr></tr> 
</table> 

I'll give action of these three buttons later.

3. Edit javascript as the example. Put map and direction as global variable. Then add new object called direction = new GDirections(map, detail); To set map path to div called "map", and Detail in div Called "detail".

1 <script language="javascript">
2 var map, direction;
3 function initialize() {
4 map = new GMap2(document.getElementById("mapCanvas"));
5 map.setCenter(new GLatLng(13.734132, 100.411992), 13);
6 map.setUIToDefault(); 7 direction = new GDirections(map, detail);
8 }
9 </script>
4. Now, put new function to control the map. I'll add setLocation() that take an integer as argument, it's used to indicate destination. add following function to your javascript part.
1 function setLocation(x) {
2 direction.clear();
3 var str = "from:13.715539,100.408602 to:";
4 if (x == 1) str += "13.793635,100.326054";
5 else if (x == 2) str += "13.778776,100.47658";
6 else str += "13.747014,100.53443"; 7 direction.load(str);
8 }
notice that i clear the Direction drawed on map first. Then i called method load that take str as argument. str is called query word which must be understood by google API. You can put it in form of "from:" and "to:" follow by other query word such as "New York"(name of places in Thailand usually not working with query, so i put lat and long instead).
5. i modified the buttons to let it pass argument to invoke setLocation(x) as following
<input type="button" onclick="setLocation(1)" value="Salaya, Mahidol" /> : <input type="button" onclick="setLocation(2)" value="Central Pinkrao" /> : <input type="button" onclick="setLocation(3)" value="Siam Center" />

6. Compare your result with this

Wow!!! If you like this one or if you have any question, leave your comment. Thank you! i hope that i'll update this one again soon! haha

Categories:   AJAX Technology | Javascript
Actions:   E-mail | del.icio.us | Permalink | Comments (4) | RSS


CSS3 Preview

Today, I'm going to talk about CSS3 (Cascading Style Sheet) new features. You see, CSS3 isn't finished yet. There'll be many more features when it's released. The most awesome feature is transformation. You can modify color, rotation, border width, color or even very complicate animation like pulse on your style sheet. 

At the present time, there are some browsers that supported CSS3.  (No IE as usual)

 ·      Chrome 2 beta (some version)

·      firefox 3.5 beta

·      Safari/ webkit (3.1 or better)

·      Konquerer

·      Opera 10

Here are new features from CSS3.

Borders

·         border-color : You can add gradient border

·         border-image : Use image as edge and customize the property (Stretch, tile).

·         border-radius : Round corner (by radius).

·         box-shadow : Drop shadow (div)

Backgrounds

·         background-origin and background-clip

·         background-size

·         multiple backgrounds

Color

·         HSL colors

·         HSLA colors

·         opacity

·         RGBA colors

Text effects

·         text-shadow

·         text-overflow

·         word-wrap

User-interface

·         box-sizing

·         resize : resize textbox

·         outline : border outline beyond regular border

·         nav-top, nav-right, nav-bottom, nav-left

Selectors

·         attribute selectors

Basic box model

·         overflow-x, overflow-y

Generated Content

·         content

Other modules

·         media queries

·         multi-column layout : Divide text into multiple columns

·         Web fonts : display custom font from server

·         Speech : Speech command from Opera user

And many cool stuff such as rotate or pulse animation by webkit SDK. For more information http://webkit.org/

http://webkit.org/blog/324/css-animation-2/

http://webkit.org/blog/324/css-animation-1/

Visit http://www.css3.info/preview/ for more previews.

 P.S. I'm very busy right now. So there may be very long time waiting. Sorry you guy!

 

มารู้จักกับ AJAX กันดีกว่า

AJAX ย่อมาจาก Asynchronous Javascript and XML ครับ มันคืออีกหนึ่งวิธีใ นการสื่อสารระหว่าง Server กับ ตัวเว็บเบราเซอร์ครับการที่เราเข้าหน้าเว็บไซต์แต่ละครั้ง ถ้าเป็น dynamic page หรือแบบที่มีการตอบสนองกับผู้ใช้ อย่างเช่น การส่งใบสมัคร กรอกฟอร์ม หรือแบบสอบถามหรือบอร์ดต่างๆ ภาษาที่ใช้พัฒนาก็เช่น PHP, ASP, ASP.NET ครับ ซึ่งกลักการทำงานก็คือ server รับคำขอ แล้วก็ตอบรับกลับไป

การที่จะรับส่งข้อมูลแบบ ธรรมดา server จำเป็นต้องทำการ refresh หน้าตลอดเวลาที่มีการตอบสนองกับทางผู้ใช้ ทำให้การตอบสนองระหว่างทั้งสองฝ่ายเชื่องช้า ไม่มี ประสิทธิภาพ AJAX มีส่วนช่วย เพราะมันจะช่วยให้มีการรับส่งข้อมูลระหว่าง server กับผู้ใช้โดยตรง ผ่านทางรูปแบบ ไฟล์ XML ตอบสนองทันทีผ่านjavascript ไม่ต้องมีการ Refresh หน้าใหม่ 

ขอแบ่ง ajax เป็นสองประเภทครับ

1. ASP.NET AJAX เป็นส่วนเสริมของฝั่ง asp.net ซึ่งมีความสะดวกสบายในการเขียนมาก ผู้ใช้สามารถลาก control ที่มีมาให้ในโปรแกรม Visual Studio ลงบนพื้นที่ที่เป็น update panel และใส่ script manager อีกตัวลงไปในหน้า ก็เป็นอันเสร็จ visual studio จะใส่ control เชื่อมกับ javascript ที่เตรียม มาให้เอง โดยที่ไม่ต้องปรับแต่งอะไรมากมาย (รวมไปถึง ajax control toolkit หรือ plugin เพิ่มเติมอื่นๆ)

2. AJAX Libraly อื่นๆ อย่างเช่น JQuery, Prototype, Script Aculo, Mootools, YUI, ExtTool จะเป็น script ที่ส่วนใหญ่จะมีให้โหลดไปใช้ฟรี รองรับทั้ง asp.net และ PHP ครับ หลักการใช้ง่ายๆ ก็แค่ โหลดไฟล์ .js มา แล้วก็ใส่เข้าไปใน <head> ของหน้า หลังจากนั้นก็ใช้คำสั่งตามแต่ที่มีในแต่ละ libraly ซึ่ง แต่ละตัวจะมีลูกเล่น ความสวยงาม ประสิทธิภาพแตกต่างกัน

ajax มีส่วนช่วยในการสร้างเว็บไซต์ 2.0 ที่สวยงามและสมบูรณ์แบบ เป็นอีกแขนงที่น่าเรียนรู้มากครับ

Categories:   AJAX Technology | Javascript
Actions:   E-mail | del.icio.us | Permalink | Comments (13) | RSS


 

Tag Cloud