Anthem.NET – An AJAX Library for ASP.NET

December 7th, 2006 Comments

A coworker introduced me to Anthem.NET, which is an AJAX library for the ASP.NET platform. Within the last year or so, quite a few AJAX libraries have surfaced, including Microsoft’s ASP.NET AJAX (formerly known as ATLAS), and other free and commercial products. Some of these are pretty decent, while others are not even worth investigating. Anthem.NET, with it’s impressive functionality, belongs to the former group.

Some of the features of Anthem.NET include:

  • Free and open source
  • Support for .NET 1.x and 2.0
  • Seamless integration with Visual Studio 2005
  • Broad browser support (IE, Firefox, and Safari)
  • Support for Mono
  • Familiar ASP.NET postback style functionality
  • Support for ASP.NET ViewState
  • Support for web user controls

You can download Anthem.NET from SourceForge.

.NET 2.0: On-demand Configuration Encryption

October 2nd, 2006 Comments
Binary Code

In .NET 2.0, apsnet_regiis can encrypt and decrypt sections of web.config and machine.config using RSA, DPAPI, or any other custom encryption provider. However, how do we encrypt/decrypt a custom application configuration (e.g. app.config of a WinForms application)?

We are in luck, since the System.Configuration namespace provides everything we need to accomplish this task. The following class, for example, can be used to encrypt/decrypt any .NET configuration file:

///
/// Implements a utility class that enables encrypting / decrypting
/// sections in .NET configuration files.
///
internal static class ConfigEncryptionHelper
{
    #region Encryption Methods

    ///
    /// Encrypts a section in a .NET configuration file using a named provider.
    ///
    /// Physical path of configuration file.
    /// Section name to encrypt.
    /// Name of the encryption provider.
    public static void EncryptConfigSection(string configPath, string sectionName, string protectionProviderName)
    {
        // Map the configuration file
        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
        fileMap.ExeConfigFilename = configPath;

        // Open configuration file
        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

        // Get the desired section
        ConfigurationSection section = config.GetSection(sectionName);

        // Encrypt the section
        if (!section.SectionInformation.IsProtected)
            section.SectionInformation.ProtectSection(protectionProviderName);

        // Save configuration file
        config.Save();
    }

    ///
    /// Encrypts a section in a .NET configuration file using DataProtectionProvider.
    ///
    /// Physical path of configuration file.
    /// Section name to encrypt.
    public static void EncryptConfigSection(string configPath, string sectionName)
    {
        EncryptConfigSection(configPath, sectionName, "DataProtectionConfigurationProvider");
    }

    #endregion

    #region Decryption Methods

    ///
    /// Decrypts a section in a .NET configuration file.
    ///
    /// Physical path of configuration file.
    /// Section name to encrypt.
    public static void DecryptConfigSection(string configPath, string sectionName)
    {
        // Map the configuration file
        ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
        fileMap.ExeConfigFilename = configPath;

        // Open configuration file
        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

        // Get the desired section
        ConfigurationSection section = config.GetSection(sectionName);

        // Decrypt section
        if (section.SectionInformation.IsProtected)
            section.SectionInformation.UnprotectSection();

        // Save configuration file
        config.Save();

    }

    #endregion
}

Adding Controls To ASP.NET GridView Pager Row

September 14th, 2006 3 comments

ASP.NET 2.0 GridView control contains the AllowPaging property. Setting this property to a value of true enables the paging feature of the GridView and displays data split across multiple pages if the number of rows in the underlying data source is greater than the PageSize property. By default ASP.NET automatically displays numeric paging controls at the bottom of the GridView.

While automatic paging is a very handy feature of GridView, if you want to display any additional paging controls than those inherently supported by automatic paging, you must resort to specifying your own PagerTemplate and write some additional code to preform the same functionality that GridView would otherwise handle for you.

In certain situations, a better option, however, is to simply inject your custom control(s) into the pager generated by the GridView. The following example shows how we can inject a “View All” LinkButton into GridView’s pager row that allows users to view all rows in the underlying data source at the same time:

protected void MyGridView_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Pager)
    {
        LiteralControl space = new LiteralControl(" ");

        LinkButton lb = new LinkButton();
        lb.ID = "ViewAllLinkButton";
        lb.Text = "View All";
        lb.SkinID = "ProfessionalGridViewPagerViewAll";
        lb.Click += new EventHandler(ViewAllLinkButton_Click);

        // Pager is rendered in a single cell as a table;
        // each page # is in a cell by it's own
        Table table = e.Row.Cells[0].Controls[0] as Table;

        // Add ViewAll linkbutton to the last cell
        TableCell parentCell = table.Rows[0].Cells[table.Rows[0].Cells.Count - 1];
        parentCell.Controls.Add(space);
        parentCell.Controls.Add(lb);
    }
}

protected void ViewAllLinkButton_Click(object sender, EventArgs e)
{
    MyGridView.AllowPaging = false;
}

ASP.NET GridView And Conditional Images

September 14th, 2006 3 comments

Depending upon a value in the data source, we can conditionally display images in GridView columns. For example, the following TemplateField declaration conditionally displays approved.gif or unapproved.gif based on IsApproved flag in the DataSet that the GridView is bound to:

<asp :Image ID="ApprovalImage" runat="server" ImageUrl="" />