Welcome to MSDN Blogs Sign in | Join | Help

Syndication

News

Localized Javascript Resources with MVC
We needed to have a handful of localized strings available to javascript. A simple but effective approach we're taking with asp.net mvc is adding a route such as this:
routes.MapRoute("ScriptResources", "res/resources.js", new { controller = "System", action = "ScriptResources" });

The controller simply calls View("Resources") and the View creates a simple json object like this
ForumsRes = 
{
    prompt_clearAbuseReason: <%= AntiXss.JavaScriptEncode(Resource.prompt_clearAbuseReason) %>,
    prompt_deleteReason: <%= AntiXss.JavaScriptEncode(Resource.prompt_deleteReason) %>,
    prompt_pageExit: <%= AntiXss.JavaScriptEncode(Resource.prompt_pageExit) %>,
    prompt_reportAbuseReason: <%= AntiXss.JavaScriptEncode(Resource.prompt_reportAbuseReason) %>,
    prompt_stickyUntil: <%= AntiXss.JavaScriptEncode(Resource.prompt_stickyUntil) %>,
    prompt_undeleteReason: <%= AntiXss.JavaScriptEncode(Resource.prompt_undeleteReason) %>,
    rte_alreadyOpen: <%= AntiXss.JavaScriptEncode(Resource.rte_alreadyOpen) %>
}

The web page can include it like any other script and reference a loc string like ForumsRes.rte_alreadyOpen. Another nice thing is you can easily OutputCache this too.

Simple idea, but thought it was worth sharing.

by RobJ | 2 Comments

Filed under: , ,

Next Generation Forums are live

We've been busy the last few months working on the next generation of Forums and Community. All of the hard work from everyone involved has paid off and the beautiful new sites are live. Sam, the dev lead for Forums, has a great post and video covering all of the features in detail.

 

Please check them out and provide feedback, the community products can not grow and improve without your participation.

 

Here are the direct links:

http://forums.msdn.microsoft.com

http://forums.technet.microsoft.com

http://forums.community.microsoft.com

Update, Expression forums are now live too!

http://forums.expression.microsoft.com

 

by RobJ | 7 Comments

AJAX Client Side "OnLoad" event

One of the neat things I came across while working on my current project is an ajax shortcut for hooking in to the Sys.Application.load event on the client. As it turns out you can declare simply declare a pageLoad function which AJAX will automatically call. Further details can be found here in the AJAX documentation.

function pageLoad(sender, args)
{
     //called on Sys.Application.load
}
 
function pageUnload(sender, args)
{
     //called on Sys.Application.unload
}
Note: You can also do the same for the Sys.Application.unload event.

by RobJ | 1 Comments

Filed under: ,

Cool New Microsoft Learning Site

The team I work on has just finished and released a major upgrade of the Microsoft Learning Site.

 

Highlights include:

  • Totally new UI
  • Enhanced filter/tag based navigation through the offerings
  • RSS Feeds from stock categories as dynamic feeds based on your selected set of filters/tags
  • Ajax-ified for a much nicer user experience
  • More web-aligned urls so you can bookmark things much more effectively than in the past
  • Multiple view and sorts
  • Localized UI and Localized Offerings
  • Updated My Learning to make it easier to navigate through and manipulate your learning items
  • ... and a whole bunch more!

So check it out. Comments and suggestions are much appreciated.

by RobJ | 2 Comments

Filed under:

Why don't my Page Methods work anymore in the RTM? The answer is...

If you run into this problem, like I just did when updating from the RC to RTM, here is what you need to know. You'll need to set the new "EnablePageMethods" attribute on the ScriptManager element to true. Pretty simple, but I don't think this was made obvious in the migration guide.

It is in the documentation which you can find here:

http://ajax.asp.net/docs/tutorials/ExposingWebServicesToAJAXTutorial.aspx 

 

 

by RobJ | 0 Comments

Filed under: ,

PageMethods in ASP.Net AJAX 1.0 RC
Since it was surprisingly difficult to find sample code, I decided to kick off my first post with a little bit about PageMethod's in ASP.Net AJAX 1.0 RC. A PageMethod is basically just a method that is exposed and callable from client script. The method must be defined as a public static and can be located in the aspx page or in the code behind (in the beta it could not be in the code behind).
public partial class _Default : System.Web.UI.Page
{
   [System.Web.Services.WebMethod]
   [System.Web.Script.Services.ScriptMethod]
   public static string ReverseString(string data)
   {
      if (data == null || data.Length == 0)
         throw new ArgumentNullException("data", "data param can not be null or empty string");

      char[] reversed = new char[data.Length];
      for (int i = 0, j=data.Length- 1; i < data.Length; i++, j--)
      {
         reversed[i] = data[j];
      }

      return new string(reversed);
   }
}

To then call the method via client script you would use the PageMethods object and using the method name as defined in the page class, like PageMethods.ReverseString("some string").

<body>
   <script type="text/javascript" language="javascript">
      function ReverseString()
      {
         var control = $get("InputString");
         PageMethods.ReverseString(control.value, ReverseSucceeded, ReverseFailed, control);
      }

      function ReverseSucceeded(results, ctl)
      {
         ctl.value = results;
      }

      function ReverseFailed(results, ctl)
      {
         alert(results.get_message());
      }

   </script>

   <form id="form1" runat="server">
      <asp:ScriptManager ID="ScriptManager1" runat="server" />

      <asp:TextBox ID="InputString" runat="server" />
      <asp:Button ID="ReverseIt" runat="server" OnClientClick="ReverseString(); return false;" />
   </form>
</body>

When calling the method, the first <n> parameters in the PageMethods call match the parameters to the class method. In this case its only one parameter, the string to be reversed. Optionally, the next parameter is the callback for success, followed by the callback for failure. Then is the context data, in this case I'm just sending the input control but it can be anything.

If you are curious about what happens next, here is some key info from Fiddler about the HTTP Request and Response.

The request looks like this minus some of the standard http stuff:

POST /AJAXCTPTest/Default.aspx/ReverseString HTTP/1.1
Referer: http://localhost:36993/AJAXCTPTest/Default.aspx
Content-Type: application/json; charset=utf-8
Host: localhost:36993
Content-Length: 37

{"data":"See the four horsemen ride"}

And the response:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 28

"edir nemesroh ruof eht eeS"

As you can see, PageMethods are pretty light and extremely easy to implement. I hope this helps save the next person a few minutes of searching.

by RobJ | 15 Comments

Filed under: ,

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement  
Page view tracker