Solved

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

Posted on 2004-10-07
11
3,143 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on 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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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