While going through the checkBox I found there is written
CheckBox checkbox = (CheckBox)sender
on checkBox1_CheckedChanged
event.
Please explain what it means?
While going through the checkBox I found there is written
CheckBox checkbox = (CheckBox)sender
on checkBox1_CheckedChanged
event.
Please explain what it means?
The line simply casts sender
to a CheckBox
.
Why?
The event handler signature for the CheckedChanged
event is:
CheckChanged(object sender, EventArgs e)
So, you need to cast sender
back to a CheckBox
if you want to use any CheckBox
specific functionality - object
doesn't have much that you can use...
This way the checkbox
variable can be used to get the checkbox Id and operate on the checkbox.
I assume the method definition is something like this:
void checkBox1_CheckedChanged(object sender,EventArgs e){
CheckBox checkbox = (CheckBox)sender;
//....
}
Basically what they are doing here is casting the sender
variable which is declared as only a generic object into a (much more "useful") CheckBox
variable.
This is commonly done because you have to cast to access the "specific" properties of a CheckBox.
For instance
sender.Checked=true;
will not work. You would have to do this:
((CheckBox)sender).Checked=true;
which of course is very ugly, so it's much easier to declare a new CheckBox variable and then you can simply do
checkbox.Checked=true;
with no casting.
The sender
parameter (which is declared as plain Object
) is cast to CheckBox
as you apparently know that the sender of that event always is a CheckBox.
As Oded says, you need the cast because of the event handler signature.
You could use checkBox1
in the event handler instead of the the typecast, but dealing with the sender
, you can reuse the CheckChanged
logic for other controls as well - although in that case, it should be refactored into something along the lines of:
private void MyFancyCheckChanged(CheckBox sender, EventArgs e)
{
// do stuff
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
MyFancyCheckChanged((CheckBox) sender, e);
}
Hello,
Here is an example.Suppose in Gridview or Repeater we want select all option.As you have seen on your mail box.There a single checkbox if you click on it all will select.Code will better explain you.
protected void ChkAll_CheckedChanged(object sender, EventArgs e)
{
foreach (RepeaterItem rowItem in this.rptFriendsRecord.Items)
{
CheckBox chk = (CheckBox)rowItem.FindControl("cbFriend");
chk.Checked = ((CheckBox)sender).Checked;
}
}
I hope it works.
The CheckBox class represents a check box that users can select and clear. This topic introduces you to the CheckBox control in Windows Presentation Foundation (WPF) and describes how to create CheckBox elements in Extensible Application Markup Language (XAML) and C#, set event handlers in C#, create CheckBox controls that contain rich content such as images, and use styling to change the control's appearance. Here i have given a sample for you .
<asp:CheckBox ID="CheckBox1" runat="server" Text="www.google.com"
OnCheckedChanged="CheckBox1_CheckedChanged" />
<asp:CheckBox ID="CheckBox2" runat="server" Text="www.yahoo.com" />
The Code Behind code is
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Checked)
Response.Redirect("www.google.com");
else
Response.Redirect("www.yahoo.com");
}