[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-10-07
11
Medium Priority
?
3,295 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
10 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

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.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 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