Results 1 to 2 of 2

Thread: CheckBox Click Event in DataGrid

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Location
    Bulgaria
    Posts
    12

    CheckBox Click Event in DataGrid

    web,Asp.net,Vb.net,VS.net

    Good day ,

    I have DataGrid and CheckBox Control placed in Template Column

    I like to capture Check Box On Click Event.
    I can capture Click Event in DataGrid ItemCommand with Button Control but I could not manage with the CheckBox Control.

    Any Help?
    Thank you
    Krasimir

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Checkbox controls don't subscribe to the OnItemCommand. Those are for buttons, either command buttons or submit buttons.

    You can implement an autopostback however, and assign OnCheckChanged to a function in your code-behind (aspx.vb)

    However, you will still need a way to determine which checkbox fired the event.

    What I did is create a class, that inherits Web.UI.WebControls.Checkbox.

    I add a hidden input field in the class's createchildcontrols, and then check which checkbox fired in the server side code using a inputfield_changed event.

    Here's the code for the class. (you can ignore or remove the IsReadOnly property, its specific for our project.)
    VB Code:
    1. Imports System.ComponentModel
    2. Public Class CESICheckbox
    3.     Inherits System.Web.UI.webControls.CheckBox
    4.     Implements Web.UI.INamingContainer
    5.  
    6.     Private _isreadonly As Boolean = False
    7.     Private _tagtext As String
    8.     Private _ischecked As Boolean = False
    9.     Protected WithEvents CheckBox1 As System.Web.UI.WebControls.CheckBox
    10.     Public Event CheckChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    11.  
    12.     Public Property HiddenTag() As String
    13.         Get
    14.             Dim _temptext As String = ViewState("hiddentag")
    15.             If _temptext Is Nothing Then
    16.                 Return String.Empty
    17.             Else
    18.                 Return _temptext
    19.             End If
    20.         End Get
    21.         Set(ByVal Value As String)
    22.             _tagtext = Value
    23.             Me.ViewState.Add("hiddentag", Value)
    24.         End Set
    25.     End Property
    26.  
    27.     <DefaultValue(True)> Public Property IsReadOnly() As Boolean
    28.         Get
    29.             Return _isreadonly
    30.         End Get
    31.         Set(ByVal Value As Boolean)
    32.             _isreadonly = Value
    33.             If Value = True Then
    34.                 Me.Enabled = False
    35.             Else
    36.                 Me.Enabled = True
    37.  
    38.             End If
    39.         End Set
    40.     End Property
    41.  
    42.     Private Sub CESICheckbox_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.CheckedChanged
    43.         RaiseEvent CheckChanged(sender, e)
    44.     End Sub
    45. End Class

    The ASPX page (HTML code) uses this inside the repeater template:
    VB Code:
    1. <cc1:CESICheckBox text="Completed" Autopostback=true Checked='<%# Databinder.eval(Container.dataitem,"IsCompleted")%>' Enabled='<%# IsShowTaskDelete()%>' Hiddentag='<%# Databinder.eval(Container.dataitem, "pk_ReportFollowUpTask").toString %>' OnCheckChanged="TaskRepeater_CheckChanged" runat=server visible=true>
    2.                                                         </cc1:CESICheckBox>

    Then, this is all the code you need on the code-behind (aspx.vb) to ascertain what checkbox in a repeater (template)was checked.

    VB Code:
    1. Public Sub TaskRepeater_CheckChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    2.         Dim tempctrl As CESIControls.CESICheckbox = CType(sender, CESIControls.CESICheckbox)
    3.        
    4.      'the tempctrl.Checked tells you if its checked
    5.      'the tempctrl.HiddenTag returns whatever
    6.      'value you use to uniquely identify that record in
    7.      'the repeater or datagrid. HiddenTag returns a string, so you may need to convert, as i did below.
    8.         With sqlcommand1.Parameters
    9.             .Item(0).Value = tempctrl.Checked
    10.             .Item(1).Value = Integer.Parse(tempctrl.HiddenTag)
    11.         End With
    12.  
    13.         SqlConnection1.Open()
    14.         sqlcommand1.ExecuteNonQuery()
    15.         SqlConnection1.Close()
    16.  
    17.  
    18.     End Sub
    Last edited by nemaroller; Feb 20th, 2004 at 12:01 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width