Wednesday, May 14, 2008

Controlling Which Button Fires When the User Hits Enter in ASP.NET Web Apps

If you need to control which button fires when a user hits Enter on the keyboard in an ASP.NET web application, there are two simple ways to do this.

On an ASPX page with no MasterPage and only one Button control to worry about, set the DefaultButton property of the form tag to the ID of the Button. Nice and simple.

In my specific situation, I had a MasterPage and 3 different buttons, 1 for quick search at the top, 1 for a short signup form, and 1 for a login form if the user had already signed up. Depending on which group of fields had focus, I wanted the Enter key to fire the right Button event. The solution? Wrap each in a Panel control and the set the DefaultButton property on each Panel. Again, nice and simple.

More from Bean Software where I originally saw this hint.

Here's the MSDN documentation for it as well.

Friday, May 9, 2008

Get a List of Embedded Resource Names Within a .NET Application

I have a .NET Windows application and I wanted to embed an XSL file into it permanently and then reference it in code. Embedding it is just a quick setting change, but figuring out how to reference it stumped me. Here's how you can loop through all your embedded resources and see how you should reference them.
Assembly myAssemblyList = Assembly.GetExecutingAssembly();
string[] myResources = myAssemblyList.GetManifestResourceNames();
foreach (string resource in myResources)
txtMessage.Text += resource + "\r\n";
Once I had that information, here's how I could call the embedded XSL file.
Assembly embeddedXsl = Assembly.GetExecutingAssembly();
Stream myEmbeddedXslFile;
myEmbeddedXslFile =
embeddedXsl.GetManifestResourceStream("MyApp.xsl.MyXslDocument.xsl");
XmlDocument myStylesheet = new XmlDocument();
myStylesheet.Load(myEmbeddedXslFile);
It seems obvious now, but "MyApp" was the assembly name. "xsl" was the directory I had the file stored in. "MyXslDocument.xsl" was the file name.

Using an Enum Instead of "Magic Values" in an ASP.NET Website

I have a nasty habit of using "magic values" in some complex web pages in ASP.NET sites ... you know, passing type=document or type=legal or type=address in the URL string and then having the page react to it. I've grown to dislike it because it can be a pain to debug on a complex page and I always have to remember the values I'm expecting. I'm experimenting with using a enum instead. Here's what I'm trying.

At the top of the page or in a base page class, I'll setup my enum with the valid page types defined.
enum PageType { address, legal, document, undefined };
I'll also put a method in there that will map a string to one of the predefined page types. I suppose I could do this as a property instead of a method.
protected PageType SetPageType(string thisType)
{
// Is the value defined in the enum?
if (Enum.IsDefined(typeof(PageType), thisType))
{
// Convert the string value to a defined PageType enum.
return (PageType)Enum.Parse(typeof(PageType), thisType, true);
}
// Let's make sure we always have something let.
else return PageType.undefined;
}
On the page itself, I'll define a default PageType.
protected PageType currentType = PageType.undefined; // The default.
In Page_Load, I'll get the query string parameter value and convert it to a valid PageType.
type = (String.IsNullOrEmpty(Request.QueryString["type"])) ? "" : 
Request.QueryString["type"].ToLower();
currentType = SetPageType(type);
Note that we force the value to lower case. Now, after all that, instead of matching strings I can say anywhere on my page:
if(Enum.Equals(currentType, PageType.document))
It may seem like a lot of work, but it truly does save development time and debug time in complex situations.

Accessing XML Elements with Namespaces using the ASP.NET XPathNavigator

If you need to access XML elements that have a namespace associated with them using the XPathNavigator object, here's how you can create an XmlNamespaceManager and use it. This is part of a bit of code where I was processing an RSS post.

StringBuilder myString = new StringBuilder();
XPathDocument myDoc = new XPathDocument(myDocumentUri);
XPathNavigator myNav = myDoc.CreateNavigator();
XmlNamespaceManager myManager = new XmlNamespaceManager(myNav.NameTable);
// Here you add the name and URI for all the namespaces you need to handle.
myManager.AddNamespace("content", "http://purl.org/rss/1.0/modules/content/");
myManager.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");

XPathNodeIterator myPosts = myNav.Select("//post");
if (myPosts.Count > 0)
{
while (myPosts.MoveNext())
{
// Add the post author.
// You're passing the XmlNamespaceManager to SelectSingleNode
XPathNavigator author = myPosts.Current.SelectSingleNode(
"descendant::dc:creator[1]", myManager);
if (author != null)
myString.AppendLine("by " + author.ToString());
}
}

Thursday, May 1, 2008

NATO Phonetic Alphabet


Letter Phonetic Equivalent

A Alpha
B Bravo
C Charlie
D Delta
E Echo
F Foxtrot
G Golf
H Hotel
I India
J Juliet
K Kilo
L Lima
M Mike
N November
O Oscar
P Papa
Q Quebec
R Romeo
S Sierra
T Tango
U Uniform
V Victor
W Whiskey
X X-ray
Y Yankee
Z Zulu

More.