Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Problem with CheckBox in a datagrid

Reply
Thread Tools

Problem with CheckBox in a datagrid

 
 
mike
Guest
Posts: n/a
 
      12-11-2003
Hi there:

I've read an excellent "how to"-article by Microsoft (no. 306227) - partly
cited cited at the end of this email).

I have implemented the code related to the part "How to Add a CheckBox
Programmatically" in my code. I have changed it to use a OLDDB.DBReader to
populate the datagrid and for the databinding. It works perfectly - also
when using the edit-mode in the datagrid.

However, the problem is: in "edit-Mode" in the Datagrid using some
update-coding of mine I cannot read the "value" of the CheckBox which was
added pogrammatically in case the user changed it. I tried using
"e.Item.FindControl("CheckBox1")" but it won't work, hoping that the ID of
the CheckBox was simply CheckBox1 (see code below) . I have also been trying
to read the value by using e.Item.Cells(whatever).Controls(whatever) but how
on earth can I now the correct position - that would involve guessing.

************my code snippet*********************
Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
DataGrid1.UpdateCommand

Dim ContactId As Integer, ContactPerson, email, phone, remarks As String
Dim tb As TextBox, cb As CheckBox, bolValue As Boolean

ContactId = e.Item.Cells(1).Text

'****THIS IS THE PART THAT DOES NOT WORK

cb = CType(e.Item.FindControl("CheckBox1"), CheckBox)
bolValue = cb.Checked, Boolean

'****END OF CODE THAT DOES NOT WORK


tb = CType(e.Item.Cells(4).Controls(0), TextBox)
ContactPerson = tb.Text
tb = CType(e.Item.Cells(5).Controls(0), TextBox)
email = tb.Text
tb = CType(e.Item.Cells(6).Controls(0), TextBox)
phone = tb.Text
tb = CType(e.Item.Cells(7).Controls(0), TextBox)
remarks = tb.Text

Dim str1 As String = _
"UPDATE tblContacts " & _
"SET ContactPerson='" & ContactPerson & "', ContactPerson_email='" & email &
"', " & _
"ContactPerson_phone='" & phone & "', Remarks='" & remarks & "' WHERE
ContactID=" & ContactId

RunSQLString(str1)
DataGrid1.EditItemIndex = -1
RefreshDataGrid()
************end of my code snippet***************

I still believe that "e.Item.FindControl("CheckBox1")" is the right way to
go. But how do I know the ID (name) of the CheckBox-control which is added
by your code ? Is it possible to assign the ID somewhere in your code ?

I'd appreciate your help and assistance if you do not mind. Thank-you.

Rgds,
Mikael

************************************************** *****
How to Add a CheckBox Programmatically
The following sample code adds the TemplateColumn and CheckBox controls to
the grid and binds the data programmatically. First, the sample code adds a
TemplateColumn control and then adds the check boxes to the Template column.
Finally, the code adds an event handler to bind the CheckBox control to the
database.
Type or paste the following code sample in the Page_Load event. The code
creates a TemplateColumn object and sets its header text.
SqlDataAdapter1.Fill(DsAuthors1)

'Create a new TemplateColumn object.
Dim tcol As New TemplateColumn()
With tcol
.HeaderText = "CheckBox Column"
' Call DynamicItemTemplate to add the child controls to the Template
' Column and bind them to the Data source.
.ItemTemplate = New DynamicItemTemplate()
End With

DataGrid1.Columns.Add(tcol)
If Not IsPostBack Then
DataGrid1.DataBind()
End If

Type or paste the following code after the Public Class WebForm1, End class.
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()

' 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").GetType.ToString = "System.DBNull"
Then
oCheckBox.Checked = False
Else
oCheckBox.Checked = CBool(container.DataItem("contract"))
End If

End Sub
End Class

NOTE: The binding code is commented to make the process easier to
understand.
Save and run the code. Notice that the Template Column and unbound CheckBox
controls were added.


How to Iterate Through the Control to Test the Value of the CheckBox
Uncomment the following line of code in the aforementioned code sample:
'AddHandler oCheckBox.DataBinding, AddressOf BindCheckBox

This line calls the event handler, Public Sub BindCheckBox, when the
DataBinding event of the CheckBox control executes. The BindCheckBox
procedure evaluates the data and properly sets the Checked property of the
current check box. Notice that the procedure is called for every row in the
grid as the DataGrid binds each row.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid and embeded Checkbox..How to find if checkbox clicked =?Utf-8?B?RG90TmV0RGV2?= ASP .Net 1 10-06-2006 04:11 PM
Howto bind CheckBox to the datagrid/ Then update the database field when the checkbox is clicked. Joey Pang ASP .Net Datagrid Control 4 06-13-2005 02:29 AM
Text on Checkbox below the checkbox tshad ASP .Net 0 04-14-2005 11:26 PM
Added CheckBox to a DataGrid Doesn't work with DataGrid.Enabled=False Amar ASP .Net 0 04-06-2004 10:14 AM
Added CheckBox to a DataGrid Doesn't work with DataGrid.Enabled=False Amar ASP .Net Building Controls 0 04-06-2004 10:14 AM



Advertisments