March 2010
M T W T F S S
« Dec    
1234567
891011121314
15161718192021
22232425262728
293031  

Call webservice from javascript

1.  VS 2005  – create new website with ASP.NET Ajax-Enabled Website , then create   WebService.asmx

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]  // <== [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

public WebService () {
//Uncomment the following line if using designed components
//InitializeComponent();
}[WebMethod]

public string HelloWorld() [...]

JavaScript dateTime function

<!– Paste this code into an external JavaScript file named: dateFormat.js –>

/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by: Oded Arbel :: http://geek.co.il/wp/ */

/**
* Extension of the JavaScript internal Date object to allow various formatting of
* date/time values.
* This implementation was designed to be compliant with the [...]

Using firebug in IE

add this script in html page

<script type=’text/javascript’
src=’http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js’></script>

then run
——————–

JavaScript : Ignore Case Array Sort

When you sort an array in Javascript the array gets sorted into dictionary order. This means that ‘A’ is less than ‘a’ and gets sorted [...]

Short circuit logic operator

ในภาษาโปรแกรมมิ่งที่มีคนใช้กันมากๆเกือบทุกตัว จะต้องมีโอเปอเรเตอร์ในการดำเนินการทางตรรกะแบบลัด ซึ่งมีชื่อว่า Short-Circuit Operator ซึ่งเราสามารถที่จะใช้เจ้าตัว Short-Circuit Operator ให้เป็นประโยชน์ได้

ทำได้อย่างไร?
กฏของ Short-Circuit Operator อยู่บนกฏพื้นฐานของ && และ || นั้นคือ
สำหรับ && แล้วถ้ามีตัวใดตัวหนึ่งในนิพจน์เป็นเท็จค่าของนิพจน์ && จะเป็นเท็จทันที
ตัวอย่างที่แสดงได้ดังต่อไปนี้
Code Sample
bool p = false;
bool q = true;
if (p && q) // ที่บรรทัดนี้โปรแกรมสามารถสรุปได้ทันที่ว่าเงื่อนไขนี้เป็นเท็จ
   ;        // โดยไม่ต้อง evaluate(คำนวณ)ค่าของ q แต่กลับกันหาก
            // p เป็น true q ต้องถูก evaluate ค่า

ตัวอย่างในสถานการณ์จริงเช่นเราต้องการ เรียกใช้งาน Method ที่อยู่ใน class
แต่ตัวแปรที่ใช้เก็บ instance ของคลาสนั้น อาจมีค่าเป็น null ได้
เพื่อหลีกเลี่ยง NullException เราจึงต้อง Check ค่าของตัวแปรว่ามีค่าเป็น Null [...]

ทำไมจึงควรใช้ Asychronous script callback และ Page Method แทน Update Panel

การใช้งาน Asynchronize Feature ใน Ajax นั้นสามารถทำได้หลายวิธีด้วยกัน วิธีที่งานและเป็นที่นิยมมากคือการใช้ Update Panel ซึ่งเป็น Control พื้นฐานที่ติดมากับตัว Ajax Extension Module สำหรับ ASP.NET

UpdatePanel นั้นสามารถใข้งานได้งาน เพียงแค่นำส่วนของ Page ที่ต้องการให้เป็น Asynchronous Postback ใส่ไว้ใน Update Panel ก็สามารถทำงานได้แล้วแต่ ตัว UpdatePanel นั้นมีความสามารถในงาน call กลับไปยัง server ได้โดยที่ วงจรชีวิตในการทำงานของ server-side จะเกิดขึ้นปกติเหมือนกับการทำ postback ปกติ ตรงจุดนี้เองทำให้การใช้งาน Updatepanel นั้นจะได้ Performance ที่ต่ำกว่าการเรียกใช้ Webmethod หรือการใช้ Asynchronous layer เรียกไปยัง web service โดยตรง เนื่องจาก WebMethod นั้นจะทำงานโดย [...]

javascript

Observer Design Pattern Using JavaScript
http://www.codeproject.com/KB/scripting/Observer_Pattern_JS.aspx

Handy XP Style Menu
http://www.codeproject.com/KB/scripting/leftmenu.aspx

AJAX For Beginners
http://www.itechcollege.com/courses/AJAX/

http://www.codeproject.com/KB/scripting/jsbeginner.aspx

http://secure.codeproject.com/KB/scripting/cookies_intro.aspx

http://www.codeproject.com/KB/scripting/Javascript_PageCookie.aspx

JavaScript Convert String number

Question: How do I convert numbers to strings in JavaScript?

Answer: The simplest way to convert any variable to a string is to add an empty string to that variable, for example:

 

number -> string
a = a+” // This converts a to string
b += ” // This converts b to string
string -> number
eval(a);

javascript replace string

/**
*
* Javascript string replace
* http://www.webtoolkit.info/
*
**/

// standart string replace functionality
function str_replace(haystack, needle, replacement) {
    var temp = haystack.split(needle);
    return temp.join(replacement);
}

// needle may be a regular expression
function str_replace_reg(haystack, needle, replacement) {
    var r = new RegExp(needle, ‘g’);
    return haystack.replace(r, replacement);
}

Object serialization คืออะไร?

คือ การแปลงออปเจคให้อยู่ในรูปของไบต์สตรีม (byte stream) 

1. มีไว้เพื่อใช้ในการเก็บออปเจคไว้ในไฟล์ เพื่อเรียกอ่านและแปลงกลับ (deserialization) เป็นออปเจคในภายหลัง เช่น เราเซฟออปเจคที่เก็บข้อมูล application parameter บางตัวไว้ลงในไฟล์หลังจากโปรแกรมจบ เพื่อว่าในการรันโปรแกรมครั้งต่อไปจะได้อ่านพารามิเตอร์เหล่านั้นกลับเข้ามา วิธีนี้ไม่ค่อยเป็นที่นิยมนัก เนื่องจากมีทางเลือกอื่นๆ เช่น เก็บข้อมูลลงในฐานข้อมูล หรือ ใช้ System.Properties หรือใช้ XMLEncoder (ในจาวา 1.4 ขึ้นไป) 

2. มีไว้สำหรับเป็นวิธีส่งออปเจคนั้นไปยังเครื่องอื่น เนื่องจากออปเจคถูกแปลงเป็นไบต์สตรีม จึงสามารถส่งออปเจคนั้นไปที่เครื่องอื่น (object distribution) ในกรณีผ่านเป็นพารามิเตอร์ในการ invoke ออปเจคข้ามเครื่อง (เช่นที่ใช้ใน RMI) หรือเป็นการ migrate object ไปรันที่เครื่องอื่น เป็นต้น

———

Stream มีโครงสร้างพื้นฐานเป็นสายของ Byte ที่วิ่งมาเรื่อย ๆ เหมือนกระแสน้ำ นั่นหมายความว่าคุณสามารถเก็บข้อมูลจาก Stream หรือเขียนอะไรลงไปใน Stream คุณต้องทำงานเป็น Byte เสมอ คือก้อนข้อมูลขนาด 0-255 [...]