Sep 24, 2011

ASP.NET disable a button during postback

Postback Ritalin has been getting a lot of search hits intended to find a button disable technique for full .NET postbacks. So, this example is for all of you searching for a non-AJAX solution.

The trick is to use the OnClientClick and UseSubmitBehavior properties of the button control. There are other methods, involving code on the server side to add attributes, but I think the simplicity of doing it this way is much more attractive:

<asp:Button runat="server" ID="BtnSubmit" 
OnClientClick="this.disabled = true; this.value = 'Submitting...';"
Text="Submit Me!" />

OnClientClick allows you to add client side OnClick script. In this case, the JavaScript will disable the button element and change its text value to a progress message. When the postback completes, the newly rendered page will revert the button back its initial state without any additional work.

The one pitfall that comes with disabling a submit button on the client side is that it will cancel the browser’s submit, and thus the postback. Setting the UseSubmitBehavior property to false tells .NET to inject the necessary client script to fire the postback anyway, instead of relying on the browser’s form submission behavior. In this case, the code it injects would be:


This is added to the end of our OnClientClick code, giving us this rendered HTML:

<input type="button" name="BtnSubmit" 
onclick="this.disabled = true; this.value = 'Submitting...';__doPostBack('BtnSubmit','')"
value="Submit Me!" id="BtnSubmit" />

This gives a nice button disable effect and processing text, while the postback completes.

If you found this but are more interested in an AJAX solution to disable buttons during partial postbacks, check out either Postback Ritalin or CSS style as AJAX progress indicator.

Sep 22, 2011

Telerik Design-time Troubleshooting

Source: Telerik API Documentation

Starting from Q1 2009 (Version 2009.1.311) there is a brand new assembly installed in your GAC - Telerik.Web.Design.dll. We decided to separate the design-time code from the web control implementation code. The reasons are many - better separation of concerns, less assembly references for the main assembly (Telerik.Web.UI.dll). We now have the freedom to extend the design time capabilities of RadControls for ASP.NET Ajax without bloating the main control assembly.

Why didn't we make it that way from the beginning? The only reason is that Visual Studio 2008 had a problem with loading GAC-ed design-time assemblies of control libraries deployed in the BIN folder of a web site. Strangely enough the problem didn't appear in Visual Studio 2005. The good news is that this bug has been fixed in Visual Studio 2008 SP1! The problem does not exist in Visual Studio 2010 RC as well.

If you for some reason cannot upgrade to Visual Studio 2008 SP1 you might face the following exception after upgrading to RadControls for ASP.NET Ajax Q1 2009 or later:
Error Creating Control - RadControl
Failed to create designer ...
Below is a screenshot of two ways this error gets represented to the end user:

Important: The instructions below are not applicable if you used the RadControls for ASP.NET AJAX installer to setup RadControls on your machine and you have Visual Studio 2005 and/or Visual Studio 2008 SP1 and/or Visual Studio 2010. The design time should work properly out of the box with that setup.
The Telerik.Web.Design assembly contains the Visual Studio designers of the controls. You don't need to add a reference to it into your project.
There are three ways to solve this problem (apart from the obvious upgrade to Visual Studio 2008 SP1):
  1. Bin-deploy the design-time assembly
    This solution would work only in case you have not installed RadControls for ASP.NET Ajax using the provided setup program. Assemblies installed by a setup program cannot be removed from GAC until the program is uninstalled. This approach is suitable when using only the dll-s from a zip (be that a hotfix or latest internal build).
    • Remove Telerik.Web.Design.dll from the GAC (this would fail if you have installed RadCotnrols for ASP.NET Ajax via the installer)
    • Copy Telerik.Web.Design.dll to your project's BIN subfolder. Ensure that the version of the Telerik.Web.Design assembly is the same as the version of the Telerik.Web.UI assembly. Don't add a reference to the Telerik.Web.Design assembly, perform a simple Windows Explorer-copy.
    • Restart Visual Studio
    The Visual Studio Extensions for RadControls for ASP.NET AJAX help handle this approach.
  2. GAC-deploy the control library assembly
    • Add Telerik.Web.UI.dll in the GAC (see details in this topic)
    • Remove Telerik.Web.UI.dll from your bin folder
    • Make sure you have the following line in your web.config:
      <add assembly="Telerik.Web.UI, Version=2009.1.311.35, Culture=neutral, PublicKeyToken=121FAE78165BA3D4"/>

    • Restart Visual Studio

  3. Copy Telerik.Web.UI.dll to c:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies and restart Visual Studio.
    Using the third approach (copying the Telerik.Web.UI assembly to the [VisualStudioInstallationFolder]\Common7\IDE\PublicAssemblies folder) could itself cause the problem described above. If you open a project, using a different version of the Telerik.Web.UI assembly than the one in the PublicAssemblies folder, the IDE could display an InvalidCast error message. It is suggested that you ensure the version of the Telerik.Web.UI assembly used by your project and its version in the PublicAssembliesfolder are the same before loading the IDE.
    This instance of the error usually happens when upgrading RadControls for ASP.NET AJAX.
Collapse imageUnknown server tag/Could not load file or assembly errors

Problem: When a project using RadControls is open and a page is loaded, a page displays the “Unknown Server Tag” or "Could not load file or assembly 'Telerik.Web.UI, version='XXXXX'..." error.
Unknown server tag
Reason: This error usually happens when the project does not contain a reference to the Telerik.Web.UI assembly, or the reference is broken.Possible causes for the errors:
  • There is a GAC-reference for the Telerik.Web.UI assembly in the project and the GAC has been updated (the “old” Telerik.Web.UI assembly is deleted and a “new” one is added to the GAC). As a result, the Telerik.Web.UI assembly, referenced by the WebSite, does not exist in the GAC.
  • There is a BIN-reference for the Telerik.Web.UI assembly in the project, which has been deleted for some reason. As a result, Visual Studio cannot find the Telerik.Web.UI assembly.
  • The reference to the Telerik.Web.UI assembly (either in Register directive or web.config, or Telerik http handlers/modules registrations in web.config) contains hard-coded version number which does not match the version of the Telerik assembly referenced in your project.
  1. Update the reference to the Telerik.Web.UI assembly in your project

    1. GAC Reference:

      1. Manually: By editing the web.config and/or the ASPX/ASCX/Master/UserControl files to reflect the assembly version change. You can also skip to specify the exact assembly version to prevent future conflicts.

      2. Automatically: By using the Property Pages. You will still need to update the <%@ Register Assembly=”Telerik.Web.UI”… %> directives in your pages manually.
        Property pages
        Telerik.Web.UI project reference
        Note: To avoid the need to manually modify the <%@ Register Assembly=”Telerik.Web.UI”… %> directives, we recommend that you register the tagprefix in the web.config instead, as shown in the screenshot below
        Telerik TagPrefix registration

  2. Bin reference:

    1. Browse to the Telerik.Web.UI assembly through the Property Pages
      Browser to reference
      Note: When browsing to the Telerik.Web.UI assembly in the context of a WebSite Project, Visual Studio will always add a GAC reference if it finds the same version of the Telerik.Web.UI assembly.

    2. Use the Convert To Telerik project menu item available under the Telerik -> RadControls for ASP.NET AJAX menu in Visual Studio*.
      *Available when the Visual Studio Extensions feature has been selected during RadControls for ASP.NET AJAX install.
Collapse imageAn unhandled exception has occurred. [A][control type] cannot be cast to [B][control type]
Error Message: Design time displays error rendering control (Error: "An unhandled exception has occurred. [A][control type] cannot be cast to [B][control type]. Type A originates from 'Telerik.Web.UI, Version=[Version], Culture=neutral, PublicKeyToken=121fae78165ba3d4' in the context 'LoadNeither' at location '[location1]'. Type B originates from 'Telerik.Web.UI, Version=[Version], Culture=neutral, PublicKeyToken=121fae78165ba3d4' in the context 'LoadNeither' at location '[location2]'.")
Error Rendering Control

Reasons: Using the Trial and the Dev variations of the RadControls assembly (having the same version) in a single instance of Visual Studio.
  • A typical scenario:
    1. Start Visual Studio.
    2. Open a project referencing the Trial variation of the Telerik.Web.UI assembly.
    3. Load a page with RadControls in design mode
    4. Close the project without closing Visual Studio.
    5. Open a project referencing the Dev variation of the Telerik.Web.UI assembly (versioned as the Trial one) in the same instance of Visual Studio.
    6. Load a page with RadControls in design mode
The reason for the error is that the .NET Framework cannot distinguish the Dev assembly from the Trial one. Once one assembly loaded in the Visual Studio AppDomain, it cannot be unloaded until the IDE gets closed.

Suggested solution:
Restart Visual Studio between the reload one of the two projects
Note: It is a good practice to use the Dev variation of the Telerik.Web.UI assembly in all your projects once purchased. This will ensure you avoid the design time error described above. It will also ensure you won't get undesired Trial messages when running your project.