Author Archive

Working Around the RegisterClientScriptBlock Problem

Microsoft ASP.Net 1.1 has a bug that will bite you when you least suspect it.
http://support.microsoft.com/default.aspx?scid=kb;en-us;817032

Basically, if your web application has more than 8 different pieces of client-side script registered using RegisterClientScriptBlock in a given web request, then the order in which they are registered gets lost.  It’s fine up through and including the 8th registration, but as soon as you register the 9th, the order of all of them becomes randomized. 

Anyone who has created a large complex website will surely ask, “ONLY 8?!”.  Yes, just 8, easily surpassed simply by having several web controls each with their own little piece of client-side script.  A clock control, a shopping-cart, some validation script, etc. and before you know it, it’s all jumbled.

Now, as long as each script is totally self-contained, you’re okay — nothing broken.  But, suppose one of the scripts relies on variables defined in one of the other scripts, or some other condition that makes it necessary to register the scripts in a specific order.  In that case, the sudden randomization when you hit #9 is a real problem. 

The MSDN article provides a hint at how to fix the problem.  However, it assumes you will have all of your client script registration happening in one place.  A better alternative is one that doesn’t require you to modify a dozen different places in the code.

The solution I will describe assumes that, with your large ASP.net 1.1 application, you are using a class derived from web.ui.page. From this class, you derive all other pages in your web site (this is the best way to build a complex site, after all).  We’re a little lucky that the RegisterClientScriptBlock function is overridable (YAY, some forward thinking at Microsoft). But, (Oh, No!) IsClientScriptBlockRegistered is not overridable (Boooo, these functions are a set. Why would they make one overridable and not the other?! *gasp*).

Anyway, in your base page, override RegisterClientScriptBlock and tack on all the script that gets registered into a single stringbuilder class level variable. Then, call mybase  RegisterClientScriptBlock with a dummy string of meaningless HTML comment text, so that the web.ui.page classes’ IsClientScriptBlockRegistered will return the proper true value when the caller checks.

Private sbRegisteredScripts As StringBuilder = New StringBuilder(“”)

 

Public Overloads Overrides Sub RegisterClientScriptBlock(ByVal key As String, ByVal script As String)

        Try

                  If IsClientScriptBlockRegistered(key) Then Return

                  sbRegisteredScripts.Append(script)

             mybase.RegisterClientScriptBlock(key, “<!— registered “ & key & “ à” )

        Catch ex As Exception

                     ‘ standard error handling for your app

        End Try

    End Sub

After that override the OnPreRender function and (after checking to make sure the string isn’t empty) call RegisterClientScriptBlock, passing it the concatenated string of all the registered scripts.

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)

        Try

              Dim sTemp As String = sbRegisteredScripts.ToString()

             If Len(sTemp) > 0 Then

                     MyBase.RegisterClientScriptBlock(“some unique value”, sTemp)

             End If

             sbRegisteredScripts = New StringBuilder(“”)

        Catch ex As Exception

            ‘ standard error handling for your app

        Finally

            MyBase.OnPreRender(e)  ‘ DON’t FORGET THIS LINE

        End Try

    End Sub

With this solution, you don’t have to change any of the individual controls or pages that register script, and all your script will be in one neat chunk all in the proper order.

Design with purpose.

I know you’re anxious to get online, and that’s a good thing. However, planning your approach to the web needs to be well thought-out and strategic.

If your website is new to the Internet, rather than a redesign, it may make sense to go with a clean and simple template design to communicate your message.

Some reasons for using a template (initially):

  • Lower cost design
  • Development time reduction
  • Easier maintenance for the content manager
  • Many design choices

The most important thing to spend time on is refining your message. Too much time spent on colors and dancing baloney may prove counter-productive in the end. Knowing what you want to say and how you say it are critical tasks to complete ahead of any design or development work.

Once you’re comfortable with the information you would like to deliver, it can then be situated in a template design that enhances the overall site experience.

Afraid that your site will look like all the others? Don’t be. Just take a look at Template Monster. This is just one example of an online template warehouse.

Don’t forget that your site design will evolve over time and, initially, the most important task is to get your information online with a clean presentation that won’t deter your target audience from visiting.

Local Event: Internet Safety Seminar

When: May 9, 2006 @ 6pm
Where: Westchester County Center, White Plains, NY
Fee: Free; Registration required
How to Register: www.westchestergov.com/cybernet or call JoMary Vieira at 995- 2912

For more information, visit Westchestergov.com.

Seminar Summary:

  • Discussion of “social networking” — what it is, examples of real profiles, how kids use it (both good and bad) and potential risks
  • Discussion for parents of general Internet risks, age-appropriate ways to protect kids and how to stay on top of new technologies
  • A panel discussion on how to use social networking safely

To the Internet, and beyond!

In December of 2005, we bookmarked an introductory web tutorial, brought to you by AARP. In this tutorial, the basics for browsing the Internet were explained beginning at square one: What is the Internet?

By now, we hope you’ve been practicing and are ready for the next level. Take a look at Intermediate Browsing, a follow-on also presented by AARP.

It’s important to understand all the tools your browser has for securing your Internet browsing experience. These tutorials are a great beginning or even as a refresher and are worth a skim, at least.

Don’t forget that ResQTek offers in-home, one-on-one computer tutorials as part of ResQTek Deskside Support. Maybe Mom would like to learn how to use the computer? :)

Center a DIV Container with CSS

Center your content horizontally and vertically!

First, put your content inside a <DIV> container.

<DIV class=”container”>
All My Content Goes Here
</DIV>

Then, establish the style properties for the container.

<STYLE>
div.container {
   position: absolute;  /* goes hand-in-hand with top and left parameters to position with the BODY tag */
   left: 50%; /* relative to the BODY tag */
   width: 600px; /* specific container width */
   margin-left: -300px; /* works with left parameter to center the container horizontally */
   top: 50%; /* relative to the BODY tag */
   height: 380px; /* specific container height */
   margin-top: -190px; /* works with top parameter to center the container vertically */
   text-align: left; /* I want the DIV content to be flush left */
   overflow: visible; /* in case the container is not the exact size of the content block */
}
</STYLE>

Here’s my test sample. View the source code to see how it works.

More information about CSS properties can be found in the MSDN Developer Center.

Dynamic Expression Evaluation in .Net using XSLT

Parsing and evaluating a mathematical or logical expression at runtime isn’t as easy as most people would like to think. There’s no built in .Net functionality to do it.  So, if you want to include dynamic evaluation, there are several options.

  1. Find an already-built third-party component or ActiveX control.
  2. Build your own formula string parser ala Compiler Theory and Design.  (This is something many of us did during our college days; doable but very time consuming.)
  3. Dynamically generate and compile .Net code inserting the mathematical expression into it.
  4. Last but not least, you can use XSL’s built-in parsing and expression evaluation functionality.

The fourth option has the luxury of being cheap, relatively efficient, and available in .Net today. With just a function or two, you can be using it in less than an hour.

To implement an XSLT solution, you need some XML.  In this case, the XML is meaningless and is only defined so that we have something to apply the XSL to.  For example, take the following super-simple XML:

<?xml version=”1.0″ encoding=”UTF-8″?><Root></Root>

It doesn’t get much simpler than that. Now that we have our XML, we need the XSL — also very simple.

<?xml version=”1.0″ encoding=”UTF-8″?>
<xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xml:space=”default” >
          <xsl:output method=”xml” version=”1.0″ encoding=”UTF-8″ indent=”yes”  standalone=”yes” omit-xml-declaration=”yes” />
          <xsl:param name=”Formula” select=”0″ />
          <xsl:template match=”/”  >
                   <xsl:value-of select=”$Formula”/>   
          </xsl:template>
</xsl:stylesheet>

Looks easy, doesn’t it?  As long as the parameter passed in as “Formula” is a valid XSL expression, the XSL will calculate it and the result will be returned as text containing the value. You can even pass in a Boolean expression, like “3=4,” which will return the value “false.”  As shown in bold above, the omit-xml-declaration is important. We want the result to contain the value of the calculation without the XML header.

So how does this look inside a function?  Let’s assume we already defined a function, called “ApplyXSL,” that is designed to: accept XML and XSL strings, transform them, and return the result in a string. (In your own code, you can call your favorite XSLT functionality instead.)

Further, we will forego passing in parameters and using separate XML & XSL files in  favor of putting everything in one self-contained function (example in VB, but easily ported to C#) — like this:

Public Shared Function EvalAnyExpression(ByVal psExpression As String, ByVal psDefault As String) As String
            ‘   returns the String evaluation of the passed string
        Dim sRes As String = psDefault
        Dim sXSL As String = “”
        Const csXML As String = “<?xml version=”1.0″ ?><Root></Root>”
        Const csXSL As String = “<?xml version=”"1.0″” encoding=”"UTF-8″” ?>” & vbCrLf & _
                “<xsl:stylesheet version=”"1.0″” xmlns:xsl=”"http://www.w3.org/1999/XSL/Transform”" >” & _
                “<xsl:output method=”"xml”" version=”"1.0″” encoding=”"UTF-8″” indent=”"yes”"  standalone=”"yes”" omit-xml-declaration=”"yes”" />” & vbCrLf & _
                “<xsl:template match=”"/”" xml:space=”"default”" >” & _
                “<xsl:value-of select=”"EXPRESSION”"/></xsl:template></xsl:stylesheet>”

        Try
            sXSL = Replace(csXSL, “EXPRESSION”, psExpression)
            sRes = ApplyXSL(csXML, sXSL)
        Catch ex As Exception
            ‘ Log the error
            ‘ and any other Error trapping functionality your application needs

        End Try
        Return sRes

    End Function

Notice how the XML and XSL are inline constants, then the EXPRESSION string is replaced with the value of the actual expression to be evaluated.

Once a formula is passed into the function, it’s inserted into the XSL, parsed, and the results are returned as a string. The resulting string can then be converted to an Int, Single, Boolean, or whatever you might be expecting from the passed formula.

This is just an example, of course, meant as a quick-start for your code.  It can be optimized in several ways, like pre-parsing the XSL and storing it in an XPath object somewhere. It could also be made more flexible, like putting the XML/XSL into files or fields in a database.

Buying a computer… again.

Time goes on, the computer gets older, slower, less productive… now what? Upgrade old? Buy new? Just deal?

Every person or business has a different set of requirements for their computer. The first step in determining what to buy (or not buy) is: Make a list.

Here’s how the list should flow:

  1. What do I do with my computer now?
    Note all the programs you use and how often you use them.
    If more than one person uses the computer, include their tasks.
  2. What would I like to do with my computer later?
    Note all the tasks you would like to perform with your computer in the future.
  3. How much money can I spend?
    This needs to be greater than zero in order to proceed, so be specific. :)

Once this list is defined in detail, it’s time to determine next steps. The solution is not always going to include buying a new computer, so it’s important to define the need thoroughly. From here, we compare your current and future needs with the available options.

Option 1: Upgrade Your Computer
Depending on your existing computer’s overall capability, it may be feasible to improve it and meet your needs in the process. This is usually the case with custom-built computers, as they are typically more upgradable than a pre-built/pre-packaged machine. A careful review of the specifications along with pricing research on new hardware will reveal this option’s practicality. This option saves money, usually.

Option 2: Buy a New Computer (or build one)
After determining that an upgrade is out of the question, we move on to the “new machine” category. The decision tree splits again and we have to decide on whether to buy a pre-built machine or a custom-built machine. And note: When a computer seller allows you to “customize” your computer purchase is does not necessarily mean that you are custom-building your machine. It often means you are selecting from a limited set of choices that are predetermined.

There are MANY computers out there and research is key. With any component of the machine we need to know these three things:

  1. Does it meet my requirements?
  2. Does it have an reputable warranty and service program?
  3. Is it within my budget?

The most difficult part of the research is answering question #1. A thorough understanding of how a computer works will serve you well here. An incorrect answer here will cause problems later. Especially when building a machine, the details are very important. As an example of how many details there are, just take a peek at this hardware tutorial! You may want a translator to help you decipher it.

Option 3: Just Deal
Okay, so you’re not ready for a new machine. That’s okay! There are plenty of ways to keep your machine up and running so you can continue your daily routine. Here are the big things:

  • Defragment your hard disk regularly
  • Update virus and spyware scanners and scan daily
  • Install all appropriate Windows updates
  • Back-up your data regularly

Consensus:
This is a lot of information to absorb, but it should give you a starting point for your computer-buying adventure. Don’t forget that you can look on the Internet for buying guides, reviews and other sources that will help you make a decision.

Note: The information above is specific to PCs with Microsoft Windows.

Why do I need a website?

Today’s information directory is online. Everything on the Internet is quickly becoming a mirror image of what you’ll find driving through town.

In addition to providing information about you and/or your organization, websites let others interact with you before they have to get in the car and visit — and sometimes the need to visit is eliminated, saving everyone time and money.

The relationship begins here. Websites are increasingly a first impression, and we all know how important that is. Here are some of the helpful things a website can provide:

  • contact information (i.e., phone numbers, mailing address, e-mail address)
  • driving directions
  • hours of operation
  • description of services
  • connections to related websites

And, those are just the basics!

It’s your virtual calling card and can be as simple or as complex as you choose. As you can see from this overview of Internet usage in the United States, over 68% of the population uses the Web. That’s a lot of clicks, and a really good reason to make sure your organization is on the other end of them.

Spyware Everywhere!

There are actually quite a few names for the things that can infest your computer and render it inoperable, but we just like to call it all “spyware” and get rid of it as quickly as possible.

Since there are so many different types of malicious software, naturally there are many different tools for removing them. We like to start with what costs the least, and for home users we mean $0.00!

Your best chance for survival is to start with Windows XP SP2. From there, the items below (when used together) help to protect your machine. Many websites and downloads transfer harmful things to your computer, and even with the best tools you can still do a lot of damage by visiting questionable sites and running downloaded programs of unknown origin.

Here’s our hotlist of helpful tools:

We’ve noted that older versions of Windows are particularly vulnerable and, if you can, an upgrade to XP is the best way to start fresh with the best chance for survival.

Internet Newbie? Start here.

There are a gazillion tutorials online that explain how to use the Internet. I stumbled across one called Basic Web Lessons that looks organized, so I thought I would share it.

It happens to be on the AARP website along with many other ‘how-to’ articles.

Take a read and see what you think!

ResQ Features