Enable AJAX with Atlas

Start off by downloading and installing the latest CTP from the asp.net site. Watch the "Get Started with “Atlas”?" video on this page to install and setup Visual Studio to be Atlas ready. You can then watch the other videos that show how to use some neat tricks to avoid server postbacks.

For the view comments feature on my blog, I created the following:

  • A custom control (by extending System.Web.UI.WebControls.WebControl) that accepts a dataset as a property and loops through the dataset in the Render method to output the necessary html.
  • A webservice that talks to my business logic, gets the necessary data, passes it to the custom control and outputs whatever the control renders.

Let's look at a simple example:

HelloControl.cs

public class HelloControl : WebControl
{
      private string _text; 
      public string Text
      {
            get { return _text; }
            set { _text = value; }
      }
      protected override void Render(HtmlTextWriter writer)
      {
         writer.Write("<span style='font-weight:bold;color:#dd0000'>Hello "
                       + _text + "</span>");
      }
}

All this control does is given an input text, it outputs the text in bold and red.

HelloService.cs

class HelloService : WebService 
{ 
      [WebMethod] 
      public string SayHello(string name) 
      { 
            HelloControl c = new HelloControl(); 
            StringWriter writer = new StringWriter(); 
            HtmlTextWriter htmlWriter = new HtmlTextWriter(writer); 
            c.RenderControl(htmlWriter); 
            return writer.ToString(); 
      } 
}

That's it, you now have a webservice that spits out html which can then be used to fill certain sections of your web page. You can watch the "“Atlas” Enable an Existing Web Service?" video to see how to use webservices on the client side using Atlas.

Have fun!


No Responses

Leave a Reply

Please enter all required information

*

*

*