Beef Up Your Buttons

Command events are the perfect vehicle for attaching additional information to button clicks.

Hot Tip

LANGUAGES: C#

TECHNOLOGIES: CommandEventArgs

 

Beef Up Your Buttons

Command events are the perfect vehicle for attaching additional information to button clicks.

 

By Jeff Prosise

 

Every ASP.NET developer knows that button controls (Button, LinkButton, and ImageButton) fire Click events when you click on them. But did you know buttons fire Command events as well? Command events, like Click events, occur in response to button clicks. But Command events differ from Click events in the parameters that they pass to event handlers. A Command event handler receives a CommandEventArgs object containing the button's CommandName and CommandArgument properties. ASP.NET preserves the values of these properties across postbacks, making Command events the perfect vehicle for attaching additional information to button clicks - information such as whether a button that performs alternating sorts should next perform an ascending or descending sort.

 

The following .aspx file demonstrates how you can use Command events to perform alternating sorts. It uses the "Titles" table of SQL Server's Pubs database as the data source:

 

<%@ Import Namespace="System.Data.SqlClient" %>

 

<html>

  <body>

    <form runat="server">

      <asp:Button Text="Sort" ID="SortButton" OnCommand="OnSort"

        CommandName="Sort" CommandArgument="Desc" RunAt="server" />

      <br><br>

      <asp:ListBox ID="Titles" Rows="18" RunAt="server" />

    </form>

  </body>

</html>

 

<script language="C#" runat="server">

void Page_Load (Object sender, EventArgs e)

{

    if (!IsPostBack)

        FillListBox ("Asc");

}

 

void OnSort (Object sender, CommandEventArgs e)

{

    if (e.CommandName == "Sort" &&

        e.CommandArgument.ToString () == "Asc") {

        FillListBox ("Asc");

        SortButton.CommandArgument = "Desc";

    }

    else if (e.CommandName == "Sort" &&

        e.CommandArgument.ToString () == "Desc") {

        FillListBox ("Desc");

        SortButton.CommandArgument = "Asc";

    }

}

 

void FillListBox (string sort)

{

    SqlConnection connection =

       new SqlConnection ("database=pubs;uid=sa");

 

    try {

        connection.Open ();

        SqlCommand command = new SqlCommand

             ("select title from titles order by title " + sort,

            connection);

        SqlDataReader reader = command.ExecuteReader ();

        Titles.DataSource = reader;

        Titles.DataTextField = "title";

        Titles.DataBind ();

    }

    finally {

       connection.Close ();

    }

}

</script>

 

The first time the Sort button is clicked, OnSort sees CommandArgument set to "Desc," so it performs a descending sort and toggles CommandArgument to "Asc." Next time, it performs an ascending sort and toggles CommandArgument to "Desc."

 

The combination of Command events and the button's CommandArgument property provides an easy solution to storing "state" - the next action the button should perform - in an application that utilizes a stateless protocol (HTTP). In effect, we let ASP.NET do the hard work of managing the state for us. And that's why we use a Web programming framework in the first place.

 

Jeff Prosise is author of several books, including Programming Microsoft .NET (Microsoft Press). He also is a co-founder of Wintellect (http://www.wintellect.com), a software consulting and education firm that specializes in .NET. Got a question for this column? Submit queries to [email protected].

 

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish