Sunday, December 27, 2009

JQuery Script to Display the Currently Selected Text in a Web Page

I needed to demonstrate the ability to detect and use the selected text in a web page.  It was surprising difficult to find a simple example to do this.

The code displays the currently selected text in a web page when either an uppercase or lowercase Q is pressed. It uses JQuery 1.3.2. I have tested it in Internet Explorer 8 and Firefox 3.5.6.

<script src="/Scripts/jquery-1.3.2.min.js" 
type="text/javascript"></script>
<
script type="text/javascript">
$(document).keypress(function(e) {
// alert("Key Pressed of " + e.which);
if (e.which == 81 || e.which==113) { // Q=81, q=113
// Get the selected text
var selected_text;
if (document.getSelection) {
selected_text = document.getSelection();
}
else if (document.selection.createRange) {
var range = document.selection.createRange();
selected_text = range.text;
}
else {
selected_text = "";
}

// Show the selected text to the user
if (selected_text != "") {
alert("Selected Text = '" + selected_text + "'");
}
else {
alert("No text selected");
}
}
return false;
});
</script>



Hope that helps.

Joe Kunk

Microsoft MVP

Okemos, MI USA

12-27-2009

Tuesday, December 22, 2009

A Global.Asax example to use the Spark View Engine in Microsoft ASP.Net MVC

To help those getting started using Microsoft ASP.Net MVC, I have copied my exact Global.asax file below. 

Note that I am using the Spark View Engine and clear the default view engine before adding Spark. Telling someone how to change global.asax file to use Spark is fine, but I think this "here it is" example will be very helpful to many starting to use the Spark View Engine.

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Spark;
using Spark.Web.Mvc;

namespace MySystem
{
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("mvcroute", "{controller}/{action}/{id}"
, new { controller="MyHome",
action = "Index", id = "" }
, new { controller=@"[^\.]*"});
}
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
ViewEngines.Engines.Clear();
SparkEngineStarter.RegisterViewEngine();
}

protected void Application_BeginRequest(object sender, EventArgs e)
{
string path = Request.AppRelativeCurrentExecutionFilePath;
if (string.Equals(path, "~/default.aspx",
StringComparison.InvariantCultureIgnoreCase) ||
string.Equals(path, "~/"))
{
Context.RewritePath("~/MyHome");
}
}
}
}


Hope that helps.


Joe Kunk

Microsoft MVP


Okemos, MI USA


12-22-2009


 

Spark View Engine not compatible with Microsoft MVC 2.0 RC

I was getting the following error when I tried to run a MVC 2 RC project with the Spark View Engine.

Method not found: ‘Void System.Web.Mvc.ViewContext..ctor(System.Web.Mvc.ControllerContext, System.Web.Mvc.IView, System.Web.Mvc.ViewDataDictionary, System.Web.Mvc.TempDataDictionary)’.

The solution was to uninstall the MVC 2 RC and the Spark View Engine, then install the MVC 2 Beta and Spark View Engine.  At that point the project worked fine.

It may be fine to skip the uninstalling and re-installing the Spark View Engine.  I did it just to ensure the best possible outcome, but I suspect it was not really necessary.

By the way, I highly recommend using the Spark View Engine in your MVC projects.  The Spark View markup is much cleaner, easier to write, and easier to maintain.  I am a big fan of this alternative view engine.

Hope that helps.

Joe Kunk
Microsoft MVP
Okemos, MI USA

Sunday, December 20, 2009

Quick list of the new features in Silverlight 4 Beta

Here is a quick list of the new features that Microsoft lists for the Silverlight 4 Beta.  Quiz yourself and see if you fully understand each of the new features.  I have included links to the MSDN documentation on each feature so your convenience.

ADO.NET Data Services
Applying Multiple Transforms is Simplified
Collection Sets
Data Binding Improvements
Drag-and-Drop and Clipboard Scenarios
DRM for Offline Scenarios
Dynamic Language Runtime
Elevated Trust
Implicit Styles
Navigation Extensibility
Networking with Multicast
New Out-of-Browser Features
Parsing and Formatting Time Intervals
Printing
RichTextArea Control
Right to Left Flow Direction
Right-click Mouse Events
Silverlight Designer Improvements
Support for Late Binding
Tuples
ViewBox Control
WCF RIA Services
WebBrowser and HtmlBrush Controls
Webcam and Microphone Support

Hope that helps

Joe Kunk
Okemos,  Michigan
12-20-2009

Quick List of the Silverlight 4 Beta Namespaces

Here is a quick list of all the namespaces present in the Silverlight 4 Beta .NET Framework for Silverlight class library. 

 

Microsoft.CSharp.RuntimeBinder

Microsoft.Internal

Microsoft.VisualBasic

Microsoft.VisualBasic.CompilerServices

Microsoft.Win32.SafeHandles

System

System.CodeDom.Compiler

System.Collections

System.Collections.Generic

System.Collections.ObjectModel

System.Collections.Specialized

System.ComponentModel

System.ComponentModel.DataAnnotations

System.Configuration.Assemblies

System.Data.Services.Client

System.Data.Services.Common

System.Diagnostics

System.Diagnostics.CodeAnalysis

System.Diagnostics.SymbolStore

System.Dynamic

System.Globalization

System.IO

System.IO.IsolatedStorage

System.Json

System.Linq

System.Linq.Expressions

System.Net

System.Net.Browser

System.Net.NetworkInformation

System.Net.Sockets

System.Reflection

System.Reflection.Emit

System.Resources

System.Runtime.CompilerServices

System.Runtime.ConstrainedExecution

System.Runtime.InteropServices

System.Runtime.Serialization

System.Runtime.Serialization.Json

System.Runtime.Versioning

System.Security

System.Security.Cryptography

System.Security.Cryptography.X509Certificates

System.Security.Permissions

System.Security.Principal

System.ServiceModel

System.ServiceModel.Channels

System.ServiceModel.Description

System.ServiceModel.Dispatcher

System.ServiceModel.Security

System.ServiceModel.Syndication

System.Text

System.Text.RegularExpressions

System.Threading

System.Windows

System.Windows.Automation

System.Windows.Automation.Peers

System.Windows.Automation.Provider

System.Windows.Browser

System.Windows.Controls

System.Windows.Controls.Primitives

System.Windows.Data

System.Windows.Documents

System.Windows.Ink

System.Windows.Input

System.Windows.Interop

System.Windows.Markup

System.Windows.Media

System.Windows.Media.Animation

System.Windows.Media.Effects

System.Windows.Media.Imaging

System.Windows.Media.Media3D

System.Windows.Messaging

System.Windows.Navigation

System.Windows.Printing

System.Windows.Resources

System.Windows.Shapes

System.Windows.Threading

System.Xml

System.Xml.Linq

System.Xml.Resolvers

System.Xml.Schema

System.Xml.Serialization

 

Hope that helps.

Joe Kunk
Okemos, Michigan
12-20-2009