?
Solved

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

Posted on 2004-10-07
11
Medium Priority
?
3,180 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12255399
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
ID: 12255404
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
ID: 12255509
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:WillyCornbread
ID: 12258431
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
ID: 12259525
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
 
LVL 2

Expert Comment

by:diablocute
ID: 12259547
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
ID: 12259694
How can I close this question without awarding points since the solution was found by me?

Thanks,
b
0
 

Author Comment

by:WillyCornbread
ID: 12259704
It is SystemEventArgs...

b
0
 
LVL 2

Expert Comment

by:diablocute
ID: 12260220
0
 

Accepted Solution

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 23 hours left to enroll

777 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