K
kart
Hi,
I have a datagrid column with check box. The check box are added to
the datagrid at runtime using an Item Template and the check box are
checked depending on the value of a column in the datagrid.
On click of a button I want to get the list of check box that are
checked. I tried using the findcontrol of datagrid item but it
searches only datagrid columns that were from the dataset it does not
search the template column that was added during runtime and thus I am
not able to find the check box control.
If any one has faced this problem please help me out.
My code behind is as follows
------------------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
'Put user code to initialize the page here
con = New SqlConnection("Data Source=machinename;Initial
Catalog=pubs;user id=userid;password=password")
ad = New SqlDataAdapter("select * from Authors", con)
ds = New DataSet()
ad.Fill(ds, "Authors")
Dim tcol As New TemplateColumn()
With tcol
.HeaderText = "Select"
.ItemTemplate = New DynamicItemTemplate()
End With
DataGrid1.Columns.Add(tcol)
If Not (IsPostBack) Then
Dim i As Integer
For i = 0 To ds.Tables(0).Rows.Count - 1
ds.Tables(0).Rows(i).Item(0) = "<a href=''>" &
ds.Tables(0).Rows(i).Item(0) & "</a>"
Next
DataGrid1.DataSource = ds.Tables(0)
DataGrid1.DataBind()
DataGrid1.Columns.Add(hl)
DataGrid1.DataBind()
End If
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim i, j As Integer
Dim al As ArrayList
Dim iCheckCol As Integer
iCheckCol = 0
al = New ArrayList()
Dim ch As CheckBox
Dim dgitem As DataGridItem
Dim sitekeys As Object
Dim ctl As CheckBox
For Each dgitem In DataGrid1.Items
ctl = CType(dgitem.FindControl(dgitem.ClientID &
"_chkSelect"), CheckBox)
If Not (ctl Is Nothing) Then
If ctl.Checked Then
Response.Write(DataGrid1.Items(i).Cells(2).Text &
"Checked" & "<br>")
End If
End If
Next
End Sub
Public Class DynamicItemTemplate
' ITemplate - When implemented by a class, defines the Control
object
' to which child controls and templates belong. These child
controls
' are in turn defined within an inline template.
Implements ITemplate
Public Overridable Overloads Sub InstantiateIn(ByVal container As
Control) Implements ITemplate.InstantiateIn
' InstantiateIn - When implemented by a class, defines the
Control
' object to which child controls and templates belong. These
child
' controls are, in turn, defined within an inline template.
'
' Create an instance of a CheckBox object.
Dim oCheckBox As CheckBox = New CheckBox()
oCheckBox.ID = "chkSelect"
' When the DataBinding event of the CheckBox fires, call the
sub
' BindCheckBox to properly bind.
AddHandler oCheckBox.DataBinding, AddressOf BindCheckBox
'Add the CheckBox to the controls collection.
container.Controls.Add(oCheckBox)
End Sub
Public Sub BindCheckBox(ByVal sender As Object, ByVal e As
EventArgs)
'Create a new instance of a CheckBox.
Dim oCheckBox As CheckBox = CType(sender, CheckBox)
Dim container As DataGridItem =
CType(oCheckBox.NamingContainer, DataGridItem)
'Evaluate the data from the Grid item and set the Checked
property
' appropriatly
If container.DataItem("contract") = "False" Then
oCheckBox.Checked = False
Else
oCheckBox.Checked = True
End If
End Sub
End Class
I have a datagrid column with check box. The check box are added to
the datagrid at runtime using an Item Template and the check box are
checked depending on the value of a column in the datagrid.
On click of a button I want to get the list of check box that are
checked. I tried using the findcontrol of datagrid item but it
searches only datagrid columns that were from the dataset it does not
search the template column that was added during runtime and thus I am
not able to find the check box control.
If any one has faced this problem please help me out.
My code behind is as follows
------------------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
'Put user code to initialize the page here
con = New SqlConnection("Data Source=machinename;Initial
Catalog=pubs;user id=userid;password=password")
ad = New SqlDataAdapter("select * from Authors", con)
ds = New DataSet()
ad.Fill(ds, "Authors")
Dim tcol As New TemplateColumn()
With tcol
.HeaderText = "Select"
.ItemTemplate = New DynamicItemTemplate()
End With
DataGrid1.Columns.Add(tcol)
If Not (IsPostBack) Then
Dim i As Integer
For i = 0 To ds.Tables(0).Rows.Count - 1
ds.Tables(0).Rows(i).Item(0) = "<a href=''>" &
ds.Tables(0).Rows(i).Item(0) & "</a>"
Next
DataGrid1.DataSource = ds.Tables(0)
DataGrid1.DataBind()
DataGrid1.Columns.Add(hl)
DataGrid1.DataBind()
End If
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Dim i, j As Integer
Dim al As ArrayList
Dim iCheckCol As Integer
iCheckCol = 0
al = New ArrayList()
Dim ch As CheckBox
Dim dgitem As DataGridItem
Dim sitekeys As Object
Dim ctl As CheckBox
For Each dgitem In DataGrid1.Items
ctl = CType(dgitem.FindControl(dgitem.ClientID &
"_chkSelect"), CheckBox)
If Not (ctl Is Nothing) Then
If ctl.Checked Then
Response.Write(DataGrid1.Items(i).Cells(2).Text &
"Checked" & "<br>")
End If
End If
Next
End Sub
Public Class DynamicItemTemplate
' ITemplate - When implemented by a class, defines the Control
object
' to which child controls and templates belong. These child
controls
' are in turn defined within an inline template.
Implements ITemplate
Public Overridable Overloads Sub InstantiateIn(ByVal container As
Control) Implements ITemplate.InstantiateIn
' InstantiateIn - When implemented by a class, defines the
Control
' object to which child controls and templates belong. These
child
' controls are, in turn, defined within an inline template.
'
' Create an instance of a CheckBox object.
Dim oCheckBox As CheckBox = New CheckBox()
oCheckBox.ID = "chkSelect"
' When the DataBinding event of the CheckBox fires, call the
sub
' BindCheckBox to properly bind.
AddHandler oCheckBox.DataBinding, AddressOf BindCheckBox
'Add the CheckBox to the controls collection.
container.Controls.Add(oCheckBox)
End Sub
Public Sub BindCheckBox(ByVal sender As Object, ByVal e As
EventArgs)
'Create a new instance of a CheckBox.
Dim oCheckBox As CheckBox = CType(sender, CheckBox)
Dim container As DataGridItem =
CType(oCheckBox.NamingContainer, DataGridItem)
'Evaluate the data from the Grid item and set the Checked
property
' appropriatly
If container.DataItem("contract") = "False" Then
oCheckBox.Checked = False
Else
oCheckBox.Checked = True
End If
End Sub
End Class