Hello,
I am trying to read the values of a chackboxes that I have manually added
to a datagrid. I always get an System.NullReferenceException as an error
message.
The code that I use to read the value of the checkbox is:
Sub UpdateOrdersListDatabase()
Dim orders As OrdersDB = New ASPNetPortal.OrdersDB()
Dim i As Integer
For i = 0 To MyList.Items.Count - 1
Dim SendOrder As CheckBox = CType(MyList.Items(i).FindControl("SendOrder"),
CheckBox)
Dim MyReleaseOrder As CheckBox = CType(MyList.Items(i).FindControl
("ReleaseOrder"), CheckBox)
If MyReleaseOrder.Checked = True Then
If SendOrder.Checked = True Then
...
The interesting thing is, that the NullException just occurs with the field
I manually added to the datagrid. The field SendOrder is added in the html
template and no error occurs. In the html source of the loaded page, both
checkboxes are looking equal and both are having an id.
The code that I used to add the checkbox as a new column in the datagrid is
the sam as on the microsoft msdn page:
Sub CreateOrdersList()
Dim orders As OrdersDB = New ASPNetPortal.OrdersDB()
MyList.DataSource = orders.GetAllOrders()
Dim tcol As New TemplateColumn()
With tcol
.HeaderText = "Bestellung freigeben"
.ItemTemplate = New DynamicItemTemplate()
End With
MyList.Columns.Add(tcol)
MyList.DataBind()
If MyList.Items.Count = 0 Then
MyError.Text = "Es sind keine weiteren Bestellungen
eingegangen."
MyList.Visible = False
End If
End Sub
End Class
Public Class DynamicItemTemplate
Implements ITemplate
Public Overridable Overloads Sub InstantiateIn(ByVal container As
Control) Implements ITemplate.InstantiateIn
Dim oCheckBox As CheckBox = New CheckBox()
oCheckBox.ID = "ReleaseOrder"
AddHandler oCheckBox.DataBinding, AddressOf BindCheckBox
container.Controls.Add(oCheckBox)
End Sub
Public Sub BindCheckBox(ByVal sender As Object, ByVal e As
EventArgs)
Dim oCheckBox As CheckBox = CType(sender, CheckBox)
Dim container As DataGridItem = CType
(oCheckBox.NamingContainer, DataGridItem)
If container.DataItem("StatusID") = "1" Then
oCheckBox.Checked = False
Else
oCheckBox.Checked = True
End If
End Sub
End Class
You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.