shared checkbox_CheckChanged event

Discussion in 'ASP .Net Web Controls' started by David A. Beck, Dec 8, 2003.

  1. I have several checkboxes in a group. Only two can be checked at any one
    time. I made one _CheckChanged event to handle all of these checkboxes. By
    setting the AutoPostback = True on them all I can total up the number of
    boxes that are checked and set a nastygram if more than two are checked. I
    would also like to uncheck the offending checkbox (the third one). Can
    anyone tell me how to get the ID of the checkbox that raised the event?

    David A. Beck
    Private Sub cbxOBJ_Preserve_CheckedChanged(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles cbxOBJ_Preserve.CheckedChanged,
    cbxOBJ_Investment.CheckedChanged, cbxOBJ_CapitalAppreciation.CheckedChanged,
    cbxOBJ_TradingProfit.CheckedChanged, cbxOBJ_Speculation.CheckedChanged
    David A. Beck, Dec 8, 2003
    #1
    1. Advertising

  2. Hi David,

    Thanks for posting in this group.
    In the shared CheckChanged event, I think there are 2 ways to determine the
    sender checkbox:
    1. Convert sender object to CheckBox, then refer to its ID property, like
    this:
    CheckBox cb=(CheckBox)sender;
    Response.Write(cb.ID.ToString());

    2. Compare the reference pointer, like this:
    //CheckBox1, CheckBox2, CheckBox3, CheckBox4 are the 4 checkbox references.
    if(sender.Equals(CheckBox1))
    {
    Response.Write("CheckBox1");
    }

    if(sender.Equals(CheckBox2))
    {
    Response.Write("CheckBox2");
    }

    if(sender.Equals(CheckBox3))
    {
    Response.Write("CheckBox3");
    }

    if(sender.Equals(CheckBox4))
    {
    Response.Write("CheckBox4");
    }

    Then, you can setup a static checkbox counter, if this counter exceeds 2,
    then you can uncheck this checkbox.

    Hope this helps,

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Dec 9, 2003
    #2
    1. Advertising

  3. Jeff, thanks so much. Your help let me put this code into the VB code-behind
    module:

    Dab
    Private Sub cbxOBJ_Preserve_CheckedChanged(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles cbxOBJ_Preserve.CheckedChanged,
    cbxOBJ_Investment.CheckedChanged, cbxOBJ_CapitalAppreciation.CheckedChanged,
    cbxOBJ_TradingProfit.CheckedChanged, cbxOBJ_Speculation.CheckedChanged

    Dim nCnt As Int32 = 0
    Dim sID As String = ""
    Dim cb As CheckBox = sender
    '
    sID = cb.ID
    If cbxOBJ_Preserve.Checked Then nCnt += 1
    If cbxOBJ_Investment.Checked Then nCnt += 1
    If cbxOBJ_CapitalAppreciation.Checked Then nCnt += 1
    If cbxOBJ_TradingProfit.Checked Then nCnt += 1
    If cbxOBJ_Speculation.Checked Then nCnt += 1
    If nCnt > 2 Then
    lError.Text = "You are only allowed 2 Investment Objectives"
    Session("Focus") = sID
    cb.Checked = False
    Else
    lError.Text = ""
    Session("Focus") = "rbAINC_Refused"
    End If
    End Sub

    ""Jeffrey Tan[MSFT]"" <> wrote in message
    news:...
    >
    > Hi David,
    >
    > Thanks for posting in this group.
    > In the shared CheckChanged event, I think there are 2 ways to determine

    the
    > sender checkbox:
    > 1. Convert sender object to CheckBox, then refer to its ID property, like
    > this:
    > CheckBox cb=(CheckBox)sender;
    > Response.Write(cb.ID.ToString());
    >
    > 2. Compare the reference pointer, like this:
    > //CheckBox1, CheckBox2, CheckBox3, CheckBox4 are the 4 checkbox

    references.
    > if(sender.Equals(CheckBox1))
    > {
    > Response.Write("CheckBox1");
    > }
    >
    > if(sender.Equals(CheckBox2))
    > {
    > Response.Write("CheckBox2");
    > }
    >
    > if(sender.Equals(CheckBox3))
    > {
    > Response.Write("CheckBox3");
    > }
    >
    > if(sender.Equals(CheckBox4))
    > {
    > Response.Write("CheckBox4");
    > }
    >
    > Then, you can setup a static checkbox counter, if this counter exceeds 2,
    > then you can uncheck this checkbox.
    >
    > Hope this helps,
    >
    > Best regards,
    > Jeffrey Tan
    > Microsoft Online Partner Support
    > Get Secure! - www.microsoft.com/security
    > This posting is provided "as is" with no warranties and confers no rights.
    >
    David A. Beck, Dec 9, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DJ Dev
    Replies:
    3
    Views:
    16,098
    Gandalf
    Feb 8, 2004
  2. Joe Fallon
    Replies:
    3
    Views:
    754
    =?Utf-8?B?Z3V5?=
    Jul 16, 2004
  3. ben
    Replies:
    3
    Views:
    504
    Kevin Spencer
    Nov 15, 2004
  4. tshad
    Replies:
    11
    Views:
    809
    tshad
    May 27, 2005
  5. Imran Aziz
    Replies:
    1
    Views:
    531
    Stefan
    Aug 22, 2005
Loading...

Share This Page