Initial Impressions: Visual Studio for Mac

While there was a premature leak earlier this week in the news, you can now officially download Visual Studio for Mac. While there isn’t much you can necessarily say in one day of poking around, in addition to the fact that this is a “preview” app, here are a few initial impressions on it. Continue reading “Initial Impressions: Visual Studio for Mac”


Quick Tip: How to use OnClick and OnClientClick events to Prevent Double Clicking on your ASP.Net Buttons

Quick Tip

While ASP.Net provides an excellent validator set of controls, sometimes you need to “roll your own” so to speak when creating your controls. In addition, if you have some AJAX processing with your buttons behind the scenes (for example, saving a record in a popup window) you run the risk of having “double saves” occurring when the user clicks on the button and nothing happens immediately.

Having struggled with this issue myself lately, it took a little more work than anticipated to get this to work properly. Here’s how you get everything to play nicely together. Continue reading “Quick Tip: How to use OnClick and OnClientClick events to Prevent Double Clicking on your ASP.Net Buttons”

Windows Server 2003 64bit, IIS 6 32bit, ASP.Net 4 Framework, and Targeting

This article is nothing but a hat tip (HT) to my buddy Ben at work for his awesome work.

Here’s the rundown. There is a server at work that is running Windows Server 2003 x64 with IIS 6 and and .Net 4 Framework. Due to an existing application on the server, IIS has to run in 32bit mode. Ben had recently put a new web application on the server, which needed to use the .Net 4 Framework. Since the server was initially setup with the .Net 2.0 Framework, the new application defaulted to the 2.0 Framework. Typically you can go to the IIS settings for the application and use the ASP.Net tab to re-target the appropriate Framework. But for some reason, when he went into IIS to set the properties on the Virtual Directory/Web Application, the ASP.Net tab was missing.

After a LOT of digging, head beating against the wall, and some good thinking, he wasn’t able to get the tab to appear, but he was able to get the application to retarget the proper framework by doing the following:

1. Open up the command prompt on the server and figure out how IIS is running by executing the following command:

cscript C:\inetpub\adminscripts\adsutil.vbs GET W3SVC/AppPools/Enable32BitAppOnWin64

2. You should see ‘Enable32BitAppOnWin64 : (BOOLEAN) True.’

3 Is the server setup for 32-bit compatibility mode for legacy apps? If not execute the following command:

cscript C:\inetpub\adminscripts\adsutil.vbs SET W3SVC/AppPools/Enable32BitAppOnWin64 True

4. Assuming it has been installed, make sure .Net 4 Framework is registered for ASP.Net by executing:

aspnet_regiis -i

5. Find out what IIS extensions are enabled (0) and disabled (0) by executing teh following command:

C:\Windows\System32 “cscript iisext.vbs /ListFile

6. If the .Net 4 Framework is disabled you will need to run the following command:

cscript iisext.vbs /EnFile C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll

and verify that you now see a ‘1’ in the status of the desired Framework.

7. Now navigate to your Framework version folder and run the following command:

aspnet_regiis -lv

to list the status and installation path of all versions of ASP.NET that are installed on the computer. Notice the (Root) note that specifies this is the default Framework.

8. Execute the following command:

aspnet_regiis -lk

to list the path and version of all IIS metabase keys where ASP.NET is mapped.

9. Note in the attached image that the desired application DSMapApp is still targeting the 2.0 framework EVEN THOUGH running aspnet_regiis -i is supposed to switch all apps to the specified Framework.

10. To switch a particular application’s targeted Framework we’ll need the -s switch to Update scriptmaps and application-pool assignments for the specified application and for all sub-applications.:

aspnet_regiis -s W3SVC/1/Root/[AppName]

…and that should do it. The following screen shot below traces the commands executed.

Mad props to you Ben!


IIS Configuration Script Log
Transaction Log
IIS Configuration Screen Results
Final Verification in IIS Screen


Quick Tip: Persisting Dynamic Controls in ASP.Net

All of the fundamental pieces to this process are out there on this topic:

  • You can dynamically create controls (drop down lists, textboxes, AJAX Editors) on the fly using the friendly Controls.Add() command to any objects that will support it (Panels, PlaceHolders, Page).
  • The ViewState is enabled by default, and any controls that are rendered have their property details stored there (hence why it can be evil if abused).
  • When a post back occurs, the dynamic controls are lost because that’s just it, they were created dynamically. You have to recreate them during every post back in order to interact with them.
  • The ViewState identifies controls by their Id.

After running into numerous issues, and finding all these pieces lying around the Internet, I was finally able to get my dynamic controls to survive the trip to the server and back. Here’s the quick rundown:

  1. When creating your dynamic controls, make sure to set the ID property to something that is unique.
  2. Use the Controls.Add() method to add your controls to the page within the appropriate container.
  3. Use the PreLoad() event in your page to call the proper method(s) to generate your dynamic controls.

By the time the Page_Load event fires, your dynamic controls will exist in the page structure, and since the controls have the same Id as they did during the previous rendering of the page, the ViewState will be able to properly apply the properties to the control, allowing them to persist it’s state between post backs.

Voila! Dynamic Controls that persist their properties after a post back.

Quick Tip: Taking your ASP.Net Application Offline

ASP.Net is friendly enough that if you have to make a change to your application, you can simply copy the files up to the server location and be done with it (the noted XCopy method). A user currently using the application will work with an old copy of the DLL until their session is finished, whereas the new users will get a new copy of the DLL.

There are times, however, where a more robust update process is involved (adding some database entities, making sure session data is completely flushed, adding resource files) and you need to take the user offline. If you are running your own server, or have a single application on the server, this is a pretty simple process. You can simply shut down IIS altogether, or have the IIS entry redirect to a different page at the domain level, that way users can’t get into the application through any vector.

If you’re on a shared host, or have multiple applications using the same application pool, or just want a really quick way to take your application offline, there is a simple method by using one single file. I discovered this trick about a year and a half back while working on my netrep project and we just used it again the other day here at work.

Starting with .Net 2.0, if you need to take a web application offline, you simply need to upload a properly formatted HTML document named app_offline.htm and IIS/ASP.Net Worker Process handles the rest. Any users, current or new, reaching the application will be presented with your offline HTML page, as well as send a Server 200 response back to the browser. When you’re ready to bring the application online again, simply remove the file, or better yet, rename it to app_offline_disabled.htm or something else and save it for the next time you need to take things offline.

That’s it! No mess, no fuss, IMMENSE benefit! There were even some delusions of grandeur in my group about making a simple web application for our server admins to to allow them to simply check the web apps that need to go offline when an update is being made and it would automatically process these files.

The one small caveat to this is that you have to make your HTML file greater than 512 bytes in size. Otherwise, Internet Explorer (and potentially other browsers) will attempt to display a “friendly” error message to you, like they would on other sites when encountering 400, 200, 500, and other server return codes.


Accessing AJAX Rendered Controls via JavaScript

The AJAX library and Control Toolkit provided for ASP.Net helps you do some really cool things that previously would have taken a lot of work. AutoComplete Textboxes (like when you start typing into Google’s search box), rounded corners, post backs without the flicker, and even more become something you can be easily added into basic line of business apps that would often be reserved for “front facing” apps due to the time or complexity involved with them.

However, there is one “gotcha” with the way that the dynamic controls are created. In order to preserve the uniqueness of control Ids, the AJAX library will put a prefix in front of the Id you assign to a control, to make sure duplicates do not occur. Go ahead and take a peek at your AJAX enabled application, you’ll find a lot of controls named


when you named your control


as its name. Sometimes you’ll also run into this issue when you’re dealing with a lot of master/child templates, especially when tossing in user controls into the mix.

Normally this isn’t much of a problem. Most of the client side interaction is already handled by the dynamic JavaScript code AJAX generates for you. In addition, when you go to access the controls through your code behind page, you can get to them just fine using their original names.

The problem arises when you need to do some additional magic through JavaScript with these dynamically created control. For example, I created a simple employee search box for our internal applications that leverages the AutoCompleteExtender control. When you type in a partial last name, like “Patt”, it returns all employees in the list that match that last name. In my particular case, I had additional textboxes that contained the employee’s first name and Id that I wanted populated into additional textboxes on the page.

The mechanics behind making this work isn’t hard at all. The AutoCompleteExtender has an event called OnClientItemSelected that allows you to write your own client side code to handle what happens once the user selects an item. A little bit of work later and I had the following client side code:

function SetEmployeeDetails(source, eventArgs)
   var RawData = eventArgs.get_value();
   var RawDataRegex = /(\d{5}):\s(.+),\s(\w+)/;
   var EmployeeInfo = RawData.match(RawDataRegex);

   $get('txtSearchEmployeeId').value = 'c' + EmployeeInfo[1];
   $get('txtSearchEmployeeLastName').value = EmployeeInfo[2];
   $get('txtSearchEmployeeFirstName.ClientId').value = EmployeeInfo[3];

The basic interpretation of the code is that the selected value is split into the individual fields and put into the textboxes.

The problem we run into is that when the AutoComplete controls are rendered, they are given the ctl00_ type markup in front of the code. This breaks the JavaScript code listed above. One way to resolve this issue is to peek at your rendered page and then hard code the JavaScript code to the “extended” names. I’ve done this. It works. However, once you move this control to another page, or if you want to wrap your AutoComplete controls within a user control, the name changes and you are back to square one.

The answer to this is to “escape” your JavaScript code with a little bit of .Net code. This is how classic ASP works and there is a lot of .Net code (particularly when it comes to data binding GridView controls) that still leverages this trick. I personally don’t like to escape code out of my ASPX pages, but I also code by the rule “the right tool for the job” and this is the case where it is the right tool. The other alternative would be to dynamically register a JavaScript block when the page (or control) is rendered, but in my opinion that would create more hassle than it is worth.

So in our case, we take advantage of the fact that each server control has a property called “ClientId” which contains the name of the control as it is rendered on the page. We can get at this value and “escape” it into our $get() function to get our extended control name. When we do things this way, the code looks like this on our ASPX page:

function SetEmployeeDetails(source, eventArgs)
   var RawData = eventArgs.get_value();
   var RawDataRegex = /(\d{5}):\s(.+),\s(\w+)/;
   var EmployeeInfo = RawData.match(RawDataRegex);

   $get('<%= txtSearchEmployeeId.ClientId %>').value = 'c' + EmployeeInfo[1];
   $get('<%= txtSearchEmployeeLastName.ClientId %>').value = EmployeeInfo[2];
   $get('<%= txtSearchEmployeeFirstName.ClientId %>').value = EmployeeInfo[3];

When the page is rendered to the user, the <%= %>sections will be processed before the HTML is written (just like our PHP/ASP works) and the server will inject the “extended” name of the control. From there, you can take this code to any page you need, or create as many controls as you need, and the page will make sure the proper Id is injected into the JavaScript code to be used with your method.

If you’’ve found any other clever ways around this issue (with jQuery or anything else for that matter), share it here! I’m always looking for new tricks to add to my toolkit. Enjoy!

Soup to Nuts: Sorting/Paging/Managing a GridView

As much as I love the latest and greatest stuff coming out of the .Net camp (still catching up on my MVC2 video tutorials) I work in a pretty standard “Line of Business” (LOB) environment. We use ASP.Net Webforms and I doubt things will change anytime soon.

One of the most used items when developing our applications is the GridView. Its a great server control. There are a lot of features built into it that make for a great control to manage data with. You can sort, page, update, delete, and even perform custom commands within it.

That said, I’ve never found a really nice comprehensive article/source to cover all of these features available, though the MSDN documentation helped a bit. Since I had just finished a project in which I was doing all of these features with the GridView, I thought now would be the perfect time to put together a “Soup to Nuts” solution that had all of these features. It would be packed up into a solution that you could actually run and interact with, instead of just looking at snippets and imagining how they came together.

So I humbly present to you my S2N (Soup to Nuts) GridView. Simply download the appropriate file, unzip it to the location of your choice, and open up the solution file in Visual Studio. The solutions have already been compiled and tested. They have also been configured to use the built-in web server with Visual Studio, so you don’t need to worry about having IIS installed on your machine if it isn’t already there. I’ve packed the following “features” into the solution and provided a little bit of documentation within the code outlining:

  • Sorting records in the GridView.
  • Paging records in the GridView.
  • Inline Edit & Delete of records in the GridView.
  • Using the footer of the GridView to add new records (a great space saver).
  • (Bonus) Using an XML/XSD file to function as a simple database (based on my article).

To some degree there is a lot to be said about the various features of the GridView, and maybe I’ll do some individual posts about the various features found within the samples.

A couple of disclaimers to note:

The data isn’t that robustly structured. You can have multiple folks with the same name (different departments). In addition, the department should have been a separate table, but hey, this is a demo. 8^D

You’ll notice on the .aspx page that there are no databinding <%= %> type elements within the template columns. I’ve always had a preference to do my custom column data binding through the RowDataBound event. I find this a bit cleaner to manage and gives you some extra options to work with. I guess I’ll have to write up an article on that at a later date. 8^D

If you’re looking for some extra magic to do with a GridView, especially with AJAX, I highly recommend Matt Berseth’s blog. He hasn’t done any updates in about a year now, but there are a lot of great articles to read through there.

Hopefully these code samples will give you an good overview of the GridView if you’re just getting started with it. If you’re a “veteran” of the control, hopefully these code samples will make for a nice template to use when rolling out a new GridView in your application.

As always, all comments/suggestions are greatly appreciated.

Visual Studio 2010 Solutions
Visual Basic: S2NGridViewVB_2010 (170 KB)
C#: S2NGridViewCS_2010 (76KB)

Visual Studio 2008 Solutions
Visual Basic: S2NGridViewVB_2008 (168KB)
C#: S2NGridViewCS_2008 (73KB)