|
-
Feb 19th, 2004, 03:01 PM
#1
Thread Starter
New Member
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
-
Feb 19th, 2004, 11:55 PM
#2
I wonder how many charact
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:
Imports System.ComponentModel
Public Class CESICheckbox
Inherits System.Web.UI.webControls.CheckBox
Implements Web.UI.INamingContainer
Private _isreadonly As Boolean = False
Private _tagtext As String
Private _ischecked As Boolean = False
Protected WithEvents CheckBox1 As System.Web.UI.WebControls.CheckBox
Public Event CheckChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Public Property HiddenTag() As String
Get
Dim _temptext As String = ViewState("hiddentag")
If _temptext Is Nothing Then
Return String.Empty
Else
Return _temptext
End If
End Get
Set(ByVal Value As String)
_tagtext = Value
Me.ViewState.Add("hiddentag", Value)
End Set
End Property
<DefaultValue(True)> Public Property IsReadOnly() As Boolean
Get
Return _isreadonly
End Get
Set(ByVal Value As Boolean)
_isreadonly = Value
If Value = True Then
Me.Enabled = False
Else
Me.Enabled = True
End If
End Set
End Property
Private Sub CESICheckbox_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.CheckedChanged
RaiseEvent CheckChanged(sender, e)
End Sub
End Class
The ASPX page (HTML code) uses this inside the repeater template:
VB Code:
<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>
</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:
Public Sub TaskRepeater_CheckChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tempctrl As CESIControls.CESICheckbox = CType(sender, CESIControls.CESICheckbox)
'the tempctrl.Checked tells you if its checked
'the tempctrl.HiddenTag returns whatever
'value you use to uniquely identify that record in
'the repeater or datagrid. HiddenTag returns a string, so you may need to convert, as i did below.
With sqlcommand1.Parameters
.Item(0).Value = tempctrl.Checked
.Item(1).Value = Integer.Parse(tempctrl.HiddenTag)
End With
SqlConnection1.Open()
sqlcommand1.ExecuteNonQuery()
SqlConnection1.Close()
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|