Solved

How can I pass a value to an OnCheckedChanged event handler?

Posted on 2004-10-07
11
2,962 Views
Last Modified: 2008-01-09
I have a checkbox server control that is within a repeater. I want to fire an even that will update the database with either an active or inactive status based on the state of the checkbox.

How can I pass in the ID of the record along with the OnCheckedChanged event ?

Thanks,

b
0
Comment
Question by:WillyCornbread
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
If Me.mycheckbox.checked Then
'My Value was True
Else
'My Value was false
End If

You can either check on postback, or set autopostback to true and the expose the oncheckchanged event.

0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
CheckBox and CheckBoxList Controls
CheckBox controls add checkboxes to Web pages. Useful CheckBox properties include Text, TextAlign, and Checked. The following statement creates an unchecked checkbox:

<asp:CheckBox Text="Check me" RunAt="server" />

This one creates a checkbox that's checked and whose text is aligned to the left (rather than the right) of the checkbox:

<asp:CheckBox RunAt="server" Text="Check me"
  Checked="true" TextAlign="left" />

To check the state of a checkbox from a server-side script, read the Checked property, which is a Boolean:

<asp:CheckBox Text="Check me"
  ID="MyCheckBox" RunAt="server" />
  •••
// In a C# script
if (MyCheckBox.Checked)
    // Checked
else
    // Not checked

CheckBox controls fire CheckedChanged events when they're checked and unchecked. Use OnCheckedChanged to connect CheckedChanged events to a handler implemented in a server-side script. Also, don't forget to set AutoPostBack to True, as you can see in Figure 15.
The CheckBoxList control lets you display groups of checkboxes and exercise control over the group's layout. ListItem statements define the individual checkboxes within the group. Setting a ListItem's Selected property to True checks the corresponding checkbox:

<asp:CheckBoxList RunAt="server">
  <asp:ListItem Text="Checkbox 1" RunAt="server"
    Selected="true" />
  <asp:ListItem Text="Checkbox 2" RunAt="server" />
  <asp:ListItem Text="Checkbox 3" RunAt="server" />
</asp:CheckBoxList>

Two CheckBoxList properties affect the layout of the checkboxes: RepeatColumns and RepeatDirection. RepeatColumns specifies the number of columns the checkboxes are to be divided into (default=1); RepeatDirection specifies whether the checkboxes are to be laid out in row-first order (RepeatDirection="horizontal") or column-first order (RepeatDirection="vertical"). Vertical is the default. Use the CellPadding and CellSpacing properties to fine-tune the spacing between checkboxes.

http://msdn.microsoft.com/msdnmag/issues/01/05/webforms/

Aeros
0
 
LVL 17

Expert Comment

by:AerosSaga
Comment Utility
If Me.mycheckbox.checked Then
Session("Mycheckbox") = True
Else
Session("Mycheckbox") = False
End If

Then just check like so
If Session("MyCheckbox") Is True Then
'Do whatever with my checkbox or id
Else
'Do whatever with my checkbox or id
End If
0
 

Author Comment

by:WillyCornbread
Comment Utility
Okay, I think my question was not clear.

I am familiar with the checkbox control and how to read it's properties...

The difficulty I am having is that this checkbox is inside repeater. So each checkbox corresponds to a record in my database. Now, when the user changes the checked status of one of these checkboxes I fire the OnCheckedChanged event handler to update the database with the new status.

The checkboxes are named ckActivateArticle, and I have no troubles getting the status of these by casting the sender to a checkbox in the event handler...

SOOO.. The one missing piece of info I need is the ID of the record to update. Obviously I have access to this via databinding in the repeater, but I can't pass it like I can a CommandArgument to my event handler.

My thoughts were to write a non-visible label inside the repeater bound to this ID value, then try to get a reference to this Parent repeater in the event handler and fidn that label to extract the ID. This does not seem like a very elegant solution.

How would you pass an argument to the OnCheckedChanged handler?

Thanks,

b
0
 

Author Comment

by:WillyCornbread
Comment Utility
Okay, I solved my problem.

Basically, I added a label control to the repeater, bound the text to the current recordID and made it non-visible.

Then inside my OnCheckedChanged event handler, I can get a reference to this label like this...

CheckBox ckActivateArticle  = (CheckBox)sender;
Label lblRecordID            = (Label)ckActivateArticle.NamingContainer.FindControl("lblRecordID");

Thanks ,

b
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 2

Expert Comment

by:diablocute
Comment Utility
hi, there
What is the signature of your OnCheckedChanged handler?
Is it OnCheckedChanged(object sender, System.EventArgs e)
or
OnCheckedChanged(object sender, RepeaterItemEventArgs e)?
0
 

Author Comment

by:WillyCornbread
Comment Utility
How can I close this question without awarding points since the solution was found by me?

Thanks,
b
0
 

Author Comment

by:WillyCornbread
Comment Utility
It is SystemEventArgs...

b
0
 
LVL 2

Expert Comment

by:diablocute
Comment Utility
0
 

Accepted Solution

by:
RomMod earned 0 total points
Comment Utility
The question has been PAQ'd and the 500 points have been refunded.
RomMod
Community Support Moderator
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now