Using the ConwayControls RadioButton - Scenario 2


Upon a simple asp:Button click event (postback), iterate the rows of a GridView finding the checked RadioButton


ASP.NET - RadioButtonSpike2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RadioButtonSpike2.aspx.cs" 
      Inherits="RadioButtonSpike2" %>

<%@ Register Assembly="ConwayControls" Namespace="ConwayControls.Web" TagPrefix="ccwc" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">

<html xmlns="" >
    <head runat="server">
        <title>Radio Button Spike - Scenario 2</title>
        <style type="text/css" media="all">
            div {padding-top: 10px;}
            h3 {display: inline;}
        <form id="RadioButtonSpikeForm" runat="server">
                <asp:GridView ID="SurveyGrid" runat="server" AutoGenerateColumns="False">
                        <asp:TemplateField HeaderText="Language">
                                <asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                        <asp:TemplateField HeaderText="Favorite">
                                <ccwc:RadioButton ID="FavoriteButton" runat="server" GroupName="FavoriteGroup" 
                                    Value='<%# Eval("Id") %>' />
                    <asp:Button ID="ForPostBackButton" runat="server" Text="for post back" 
                        OnClick="ForPostBackButton_Click" />
                    <asp:Label ID="ResultsLabel" runat="server" />

C# (code behind) - RadioButtonSpike2.aspx.cs

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using CCW = ConwayControls.Web;

public partial class RadioButtonSpike2 : Page {

    protected override void OnLoad(EventArgs e) {

        if (this.IsPostBack) return;


    protected void BindGrid() {
        List<SurveyItem> items = new List<SurveyItem>();

        items.Add(new SurveyItem(1, "C#"));
        items.Add(new SurveyItem(2, "VB"));
        items.Add(new SurveyItem(3, "Perl"));
        items.Add(new SurveyItem(4, "Java"));
        items.Add(new SurveyItem(5, "Ruby"));

        this.SurveyGrid.DataSource = items;

    protected void ForPostBackButton_Click(object sender, EventArgs e) {
        foreach (GridViewRow row in this.SurveyGrid.Rows) {
            CCW.RadioButton radioButton = row.FindControl("FavoriteButton") as CCW.RadioButton;

            if (radioButton == null || !radioButton.Checked) continue;

            //the following label demonstrates the ability of grabbing other controls in the row and using it...
            Label label = row.FindControl("NameLabel") as Label;

            if (label != null) {
                string format = "favorite language: <h3>{0} (Id: {1})</h3>";
                this.ResultsLabel.Text = string.Format(format, label.Text, radioButton.Value);

public class SurveyItem {
    public SurveyItem(int id, string name) { = id; = name;

    public int Id {
        get { return; }
        set { = value; }
    }    private int id;

    public string Name {
        get { return; }
        set { = value; }
    }    private string name;



It is a good practice when iterating over a collection to continue after an unwanted test and break once you have found the item you were looking for.

As noted in the iteration of the GridViewRows inside the ForPostBackButton_Click method, I tested to ensure the RadioButton was found, that it was checked, and continued if it wasn't by using the continue keyword. Once the checked RadioButton was found, I broke the iteration using the break keyword. Both statements/uses prevent any unnecessary code execution which is even more important during iteration of a collection.

Please let me know if you have any questions with this particular scenario.


Jason Conway

No Comments