Scenario
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Radio Button Spike - Scenario 2</title>
<style type="text/css" media="all">
div {padding-top: 10px;}
h3 {display: inline;}
</style>
</head>
<body>
<form id="RadioButtonSpikeForm" runat="server">
<div>
<asp:GridView ID="SurveyGrid" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Language">
<ItemTemplate>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Favorite">
<ItemTemplate>
<ccwc:RadioButton ID="FavoriteButton" runat="server" GroupName="FavoriteGroup"
Value='<%# Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<div>
<asp:Button ID="ForPostBackButton" runat="server" Text="for post back"
OnClick="ForPostBackButton_Click" />
</div>
<div>
<asp:Label ID="ResultsLabel" runat="server" />
</div>
</div>
</form>
</body>
</html>
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) {
base.OnLoad(e);
if (this.IsPostBack) return;
this.BindGrid();
}
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;
this.SurveyGrid.DataBind();
}
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);
break;
}
}
}
}
public class SurveyItem {
public SurveyItem(int id, string name) {
this.id = id;
this.name = name;
}
public int Id {
get { return this.id; }
set { this.id = value; }
} private int id;
public string Name {
get { return this.name; }
set { this.name = 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.
Thanks,
Jason Conway