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)On the page itself, I'll define a default PageType.
{
// 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;
}
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"])) ? "" :Note that we force the value to lower case. Now, after all that, instead of matching strings I can say anywhere on my page:
Request.QueryString["type"].ToLower();
currentType = SetPageType(type);
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.
No comments:
Post a Comment