ajax

Hangman on Silverlight

หลังจากติดค้างพี่ป๊อกมานาน ก็ได้เวลาใช้หนี้ซักที

เรามาดูกันดีกว่าว่าไอ้เจ้า Hangman นี่มันเขียนยากมั้ย

Free ebooks from MS Press

As a new year gift (maybe), Microsoft Press is giving away its 3 ebooks freely included

Register today to download. Here

Flickr ASP.NET MVC app pt.2 - ajaxified

After we have finished the Flickr MVC app last time. This time will be sequel, ajaxified it.

Although the ASP.NET MVC do provide the abstract ajax helper class, only implementation exists publicly is Nikhil's. Certainly, ASP.NET Team will include the ASP.NET AJAX/Microsoft AJAX Library helper in the MVC framework for next CTP bits. As for this tutorial, I will continue with jquery instead of Microsoft AJAX Library because I'm more familiar with it.

The ajaxified instructions is mainly related to the view. In order to use jquery, put the library in ~/Content and add the reference to it in the layout.

<head runat="server">
    <title>My Sample MVC Application</title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../../Content/jquery.js"></script>
</head>

Next, Open the ~/Views/Home/Index.aspx. Then, add a placeholder div called "result" in the view. We'll use this div to display the response from the ajax call. Additionally, add a eye-candy loading indicator to let user knows that the page is being loaded.

<%Html.Form("Search", "Home", FormExtensions.FormMethod.post, new { ID = "theform" });%>
    <label for="tags">
        Tags:
    </label>
    <%=Html.TextBox("tags", 30) %>
    <%=Html.SubmitButton("find", "Find", "") %>
    <img src="../../Content/loading.gif" id="spinner" style="display:none;" />
    </form>
<div id="result"></div>

Last but not least, add some lines of script to make an ajax call once the form is goin to be submitted.

 $(document).ready(function() {        
        $('#theform').submit(function (){            
            $.post('/Home/Search', {'tags':$('#tags')[0].value}, completeRequest);
            $('#spinner').show();                        
            $('#result').slideUp(1500);
            return false;            
        });
    });
    
    function completeRequest(r){            
        $('#result').html(r).slideDown(1500);                            
        $('#spinner').hide();        
    }

The script will be invoked when the DOM is ready. It'll hook the form submit event, post the textbox value to ~/Home/Search, make the loading indicator visible, hide the result div and then, cancel the event to prevent the full post process. And then, after the request is completed, completeRequest function will be called. It will replace the div's content with the result from the response and then reveal the div by using slide down effect. And lastly, hide the indicator.

Try the page and Voila! The ajaxified is completed.

Compare to the traditional web form, MVC has a better degree to integrate with the javascript library. Though I use the web form view engine, extension method on the HTML Helper that makes the markup more flexible, no generated id. However, integration with the jquery may not be the easiest one for ASP.NET MVC as ASP.NET Team will deliver its own Ajax helper.

In conclusion, AJAX in ASP.NET MVC has a lot of room for improvement, either officially or unofficially. ASP.NET AJAX becomes useless when using it with ASP.NET MVC. However, using the alternative libraries in MVC require less effort than the traditional way. jQquery and ExtJs are the most notable. Besides, mootools and YUI are also gaining their share. Anyway, Microsoft will push ASP.NET AJAX little ahead those 3rd party library through their Ajax Helper once again.

ASP.NET MVC Ajax

After the long weekend, Scott announced the ASP.NET 3.5 Extension which includes the preview version of long-waited MVC Framework, ASP.NET MVC. Today, there are many websites and blogs regarding ASP.NET MVC. However, just a few of them are talking about AJAX.

In Web form model of ASP.NET, ASP.NET AJAX, along with AJAX Control Toolkit, is the major player for AJAX library. Since ASP.NET AJAX relies on Web form, migrating it to use with ASP.NET MVC which is just a beta bit is not really worth. But in the end, Microsoft will push its AJAX product towards ASP.NET MVC as soon as the latter is ready for production. This can be seen in Nikhil's post about AJAX in ASP.NET MVC. Though Nikhil uses his Script# to create the javascript library, the concept utilizes the behavior/component things which is the concept of ASP.NET AJAX/AJAX Control toolkit. No doubt that ASP.NET AJAX will be the one that shipped with ASP.NET MVC.

As for alternative, jQuery seems to be the winner at the time of writing. Moreover, The latest survey reveals that the most favorite alternative javascript library among ASP.NET developer is jQuery and I don't think ASP.NET MVC will differs from its counterpart. jQuery, in fact , is the fourth but the first two places are in paired, the second actually is not the javascript library so I count jQuery as the most favorite one. However, mootools, prototype/script.aculo.us will gaining their share in MVC as many people will go to ASP.NET for its MVC.

ViewState again?

Although I don’t like the ViewState, I have to confess that I used Viewstate to solve many problems while I code in ASP.NET. Today, I found some interesting pieces about good side of ViewState that I never thought of; using it in AJAX to build a history management tool.

ViewState is a concept that storing the state across request. The idea is miraculously easy; persists the user state in a hidden field and re-post it every time you make a request to server. Microsoft implemented ViewState to make a new  web application user interface technique and called it Web Form which is similar to its Windows Application counterpart, Windows Form.

With ViewState you can build a web application in the same way you do in WinForms. Most of ASP.NET Server Controls in ASP.NET 1.x utilize ViewState as their state storing mechanic. For example, DataGrid uses it to store Page index, Edit index and Select index, Button and Textbox use it to store style and text value, etc. It states and values will be saved across the requests and let you treat them like no request at all.

Even though ViewState brings us many benefits. It also has an important downside, the size of state persists in a page. As I mentioned before, almost every controls create their own Viewstate data. Imagine you have a web with 2 DataGrids. Each of them consists of a collection of DataRow which are represents through 5-6 label controls and 2 LinkButtons; these will generate couple long lines of Viewstate. This usually adds 5-10 KB to HTML size. I ever saw a page that its page size was nearly 100 KB and half of it came from ViewState. And this is a reason why I dislike WebForm. Despite of its usefulness, many ASP.NET gurus recommend to disable Viewstate when you don’t use it and save bandwidth in exchange of some less important functions. However, in ASP.NET 2.0 the viewstate had been developed to ControlState which you can’t disable anymore.

Before we continue to AJAX I have something to tell you about ViewState. Last month I read a .NET blog, I found a posts that is an interesting quote of ViewState.

Nikhil has some valid points in defence of webforms:

Sooner or later you are going to need some state that’s why we have viewstate :) and you will typically maintain your own state in a hidden field. Webforms is a framework and you roll your own specific framework for every new site.  There is nothing you can with MVC that you can’t do with webforms. 

Ivan Corto Parrero’s Blog

At first, I think it can’t be true and he is too confident in ViewState. Nowadays, MVC already proofed itself how it fit in the Web Framework. Look at those MVC framework out there; Ruby on Rails, django, Symphony and etc. We have a long tails convoy in MVC. ViewState and ASP.NET WebForm is out. Even ASP.NET Team is announced a new MVC Framework for ASP.NET. Anyway, I have to think it again after I read this post.

We approach this problem by storing state information in a hidden form field because the value of form fields persists across pages during the entire browser session. If we set the value of our form field to the current state of the application every time the application changes state, we can then initialize our page appropriately when the user returns to our application after having interacted with it beyond its initial state.

Yahoo User Interface Blog

I found this when I was searching for how to make a cross-browser history management tool. After I read this sentence I was too shock to read on.  ViewState again? It seems to be true.. sigh

Client-Side Animation ใน ASP.NET

หลังจากที่นั่งเล่นกับ Animation Framework ไปนิดนึงใน ASP.NET แล้วก็มารู้เอาทีหลังว่ามันก็ไม่ได้ยากเท่าไหร่

คราวที่แล้วใช้ script.aculo.us ก็ทำงานได้แล้วก็เลยลองเปลี่ยนมาใช้ ASP.NET Animation Framework ใน AJAX Control Toolkit ดู ใช้โค้ดจากหน้านี้มาดัดแปลงหน่อยโดยการเพิ่มไฮไลท์เข้าไปตอนอัพเดทข้อมูลแล้ว

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="animation_act.aspx.cs" Inherits="animation_act" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Animation ACT</title>
    <style type="text/css">
    #result{
        width:200px;
        height:200px;
        background:#fefeff;
        border-top: #b9b95c 3px solid;
        background-color: #ffffcc;
        padding: 5px;
        margin-top:10px;
    }
    </style>

    <script language="javascript" type="text/javascript">
// <!CDATA[
var img;
function btnHello_onclick() {
    img = new Sys.UI.Control($get("loadingImg"));
    var name = $get("txtName").value;
    var wRequest = new Sys.Net.WebRequest();
    wRequest.set_url("Hello.ashx?Name=" + $get("txtName").value);
    wRequest.set_httpVerb("GET");
    wRequest.add_completed(OnWebRequestCompleted);
    wRequest.invoke(); 
    img.set_visible(true);   
}
function OnWebRequestCompleted(executor, args){
    var target = $get('result');
    target.innerHTML = executor.get_responseData();
    var par = new AjaxControlToolkit.Animation.ParallelAnimation();
    par.add(new AjaxControlToolkit.Animation.ColorAnimation(target, 0.5, 24, 'style', 'backgroundColor', '#FFFF00', '#ffffcc'));
    par.play();
    img.set_visible(false); 
}

// ]]>
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
        <Scripts>
          <asp:ScriptReference Assembly="AjaxControlToolkit" Name="AjaxControlToolkit.Common.Common.js" />
          <asp:ScriptReference Assembly="AjaxControlToolkit" Name="AjaxControlToolkit.Compat.Timer.Timer.js" />
          <asp:ScriptReference Assembly="AjaxControlToolkit" Name="AjaxControlToolkit.Animation.Animations.js" />
        </Scripts>
        </asp:ScriptManager>
        <div>
            <input id="txtName" type="text" />
            <input id="btnHello" type="button" value="Say hi" onclick="return btnHello_onclick()" /><img
                src="24-1.gif" id="loadingImg" style="display: none" />
        </div>
        <div id="result" runat="server" style="display:block;">
        </div>
    </form>
</body>
</html>

ตรงส่วนที่ทำตัวหนาไว้คือส่วนที่เพิ่มมา เพื่อใช้ Animation Framework สังเกตว่าต้อง reference ไปที่ javascript ของ Ajax Control  Toolkit ด้วย วิธีใช้ก็แกะๆเอาจาก Reference จริงๆตรงส่วนที่เป็น JavaScript จะใช้ ColorAnimation อย่างเดียวก็ได้ ไม่ต้องใช้ PararellAnimation

ถ้าให้เทียบกับ script.aculo.us แล้ว ก็ยังห่างชั้นอยู่มากเลยทีเดียว จำนวน effects น้อย ต้องนั่งทำ effect เอง คู่มือกับวิธีใช้ก็ไม่ค่อยมี จริงๆแล้ว Animation Framework อันนี้เหมือนจะให้เป็นพื้นสำหรับสร้าง Ajax Control ซะมากกว่าจะหยิบมาใช้แบบเป็นครั้งคราว กรณีหลังนั้นให้หันไปใช้ Declarative Animation น่าจะเหมาะกว่ามั้ง

Mix 06 &gt; Mix 07 &gt; Mix 08

ปีที่แล้ว ธีมหลักของ  Mix 06 เป็นเรื่องของ WPF, ASP.NET มี AJAX แล้วก็เสนอ WPF/E เข้ามาหน่อยนึง

ปีนี้ ธีมหลักของ Mix 07 เป็น Silverlight แล้วแซม VS Orcas มาด้วย (แล้วก็มี Don Box มาพูดถึง REST หน่อยนึงด้วย)

ปีหน้า Mix 08 คาดว่าก็คงเป็นเรื่อง Silverlight vNext, VS Orcas ตัวเต็ม, Windows 2008 + IIS 7 , Media Support แล้วก็น่าจะแซมด้วย WPF vNext กับ ASP.NET AJAX vNext + Astoria แน่ๆเลย

คนที่อยากดู video mix นี่ก็เข้าไปที่ http://visitmix.com ได้ เค้า เปิดให้ดูตั้งแต่งาน mix ยังไม่จบด้วยซ้ำ

หวังว่าถึงตอนนั้นจะมีพลังพอนั่งเรียนรู้ไอ้พวกนี้นะ

f*cking ASP.NET AJAX Animation

ใน ASP.NET AJAX ไม่มีความสามารถด้าน Animation และ Effect เลย ตรงนี้ต้องไปใช้ AJAX Control Toolkit แทน แต่ว่า Animation ที่มีให้ก็เป็นแบบง่ายๆ ไม่ค่อยสวย ใช้ยาก แล้วส่วนใหญ่จะเอาไปประกาศใน Server Side แถมเอามาใช้ผ่าน javascript ก็ลำบาก นั่งเล่นมันตั้งกะบ่ายเมื่อวาน ป่านนี้ยังใช้ไม่ได้เลย โชคดีที่สามารถเอา prototype กับ script.aculo.us มาใช้แทนได้เลย

แต่ขนาดของ prototype + script.aculo.us มันใหญ่นี่สิ จะทำไงดี ก็ได้แต่หวังว่า MS และทีม AJAX Control Toolkit จะยอมเพิ่ม animation กับวิธีการใช้ หรือไม่ก็ทำให้มันใช้ง่ายๆด้วยเท้อออ