Solved

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

Posted on 2004-10-07
11
3,082 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
Technology Partners: 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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