I was talking with a co worker and we were discussing the need for knowing whether or not a radio button was checked or not when we got the Checked event called. I suggested we create our own event and he wondered how that would possible. In the end we decided that it was rather useless to write a whole event for such a thing but the discussion was good.
I thought the dialogue could be further elaborated on here in my blog.
you can't really 'uncheck' a radio button so i'll use a check box and set it to auto postback, like this:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RadioCheckedEventPage._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" />
</div>
</form>
</body>
</html>
Now let’s go into the code behind and create the event we were discussing… the one where we have no idea whether it’s checked or not without inspecting the checkbox itself.
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
bool isChecked = (sender as CheckBox).Checked;
Response.Write(isChecked.ToString());
}
So what my thought was, is to create an event on the page that represented when the check changed but passed the check state in the event args.
So first we need a new event args that has the check state.
public class CheckChangedEventArgs : EventArgs
{
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set { _isChecked = value; }
}
}
Then we need a delegate for the event handler.
public delegate void CheckChangedEventHandler(object sender, CheckChangedEventArgs e);
Then we need an event on the page.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
bool isChecked = (sender as CheckBox).Checked;
Response.Write(isChecked.ToString());
}
public event CheckChangedEventHandler CheckBoxCheckChanged;
}
Then we just hook up a handler to that event and move the response.write() into it:
public partial class _Default : System.Web.UI.Page
{
public _Default()
{
this.CheckBoxCheckChanged += new CheckChangedEventHandler(_Default_CheckBoxCheckChanged);
}
void _Default_CheckBoxCheckChanged(object sender, CheckChangedEventArgs e)
{
Response.Write(e.IsChecked.ToString());
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
bool isChecked = (sender as CheckBox).Checked;
}
public event CheckChangedEventHandler CheckBoxCheckChanged;
}
Lastly, we call the new event from the old event and pass the value. (this is the part where we realized we were just adding needless complexity, but still the lesson was good.
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
bool isChecked = (sender as CheckBox).Checked;
if (CheckBoxCheckChanged != null)
{
CheckChangedEventArgs changeEvent = new CheckChangedEventArgs();
changeEvent.IsChecked = isChecked;
CheckBoxCheckChanged(sender, changeEvent);
}
}