Usman ur Rehman Ahmed's blog

Data Grid Row Command Comparison using Enumerations

I actually was explaining a fresh into development guy how row command events of data elements work within ASP .NET. Once he was able to complete a test page, I came across this check,

    protected void grdGlassThickness_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString() == “Delete”) {

        }
    }

To explain him that there are commands which will always remain as it is throughout the application and in most of the applications; I encouraged him to use enumerations for comparison purposes rather than sticking with string. For other young fellows, this post will explain this very basic concept.

Considering the declaration of LinkButton within grid control item template in .ascx control as following,

    <asp:LinkButton ID="lnkBtnDelete" runat="server" Text="Delete"  CssClass="GridColumn"
        CommandArgument='<%# Eval("GlassId") %>' CommandName="<%= CommandTypes.Delete.ToString() %>">
    </asp:LinkButton>

For comparison purposes, An enumeration is declared as following,

    public enum CommandTypes
    {
        Delete,
        Edit,
        Insert
    }

And the comparison finally takes place as following,

    protected void grdCustomers_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName.ToString() == CommandTypes.Delete.ToString()) {
                  
            }
        }
    }

11
To Posterous, Love Metalab